2013-03-01 70 views
0

我已经搜索了四处寻找正确的方法来杀死以前的VB6应用程序实例无济于事。如果我们的主要服务死亡,我们需要终止正在运行的应用程序的前一个实例,然后当服务恢复时,只应加载新实例。我想在下面的函数来退出这个代码:杀死VB6应用程序的以前的实例

Private Sub g_cServerInterface_FatalError(Error As enSvrReturns, ErrorString As String) 

Dim sMsg As String 
Dim result As VbMsgBoxResult 

m_bFatalError = True 

UnFreeze 

If m_cLanguageText Is Nothing Then 
    GoTo TheEnd 'Form not yet loaded - not yet logged on 
End If 

' m_NumFatalErrors = m_NumFatalErrors + 1 
' If m_NumFatalErrors > 5 Then 
'  Functions.DevInfo "Unable to restart Manitou.", g_cLangText_General 
'  End 
' End If 

If Error <> SVRERR_NOT_CONNECTED Or RunningInDebugger() Then 
    sMsg = g_cLangText_General.GetText("A system error has occurred") 

    If ErrorString <> "" Then 
     sMsg = sMsg & ":" & vbCrLf & vbCrLf & ErrorString & vbCrLf & vbCrLf 
    Else 
     sMsg = sMsg & ". " 
    End If 

    sMsg = sMsg & g_cLangText_General.GetText("Press OK to attempt to restart or Cancel to quit.") 

    result = DevAskOkCancel(sMsg, Nothing) 
Else 
    ' Since we've been disconnected, attempt immediately to reconnect 
    result = vbOK 
End If 

If (result = vbOK) Then 
    On Local Error Resume Next 
    If InStr(g_CommandLine, "-U") = 0 Then 
     g_CommandLine = g_CommandLine & " -U" & g_cUser.id 
    End If 
    If InStr(g_CommandLine, "-P") = 0 Then 
     g_CommandLine = g_CommandLine & " -P" & g_cUser.Password 
    End If 
    Shell App.Path & "\" & App.EXEName & " " & g_CommandLine & " -X", vbNormalFocus 
    DoEvents 
End If 

TheEnd: 
If (Not RunningInDebugger()) Then 
    ' Running as compiled executable 
    ' Specifies the exit code for the process, and for all threads that 
    ' are terminated as a result of this call. Use the GetExitCodeProcess 
    ' function to retrieve the process's exit value. Use the GetExitCodeThread 
    ' function to retrieve a thread's exit value. 

    CoUninitialize 
    ExitProcess 0 
Else 
    ' Running from the IDE 
    End 
End If 

End Sub 

请注意,我说的CoUninitialize和ExitProcess的0 API调用此。如何在服务恢复时正确终止以前加载的实例?感谢拉里

+0

当你调用这个服务,你的意思是它运行作为Windows服务? – jac 2013-03-01 23:37:19

+0

是的,我作为一个Windows服务运行。 – codeg 2013-03-04 14:07:29

+0

试试这个答案,http://stackoverflow.com/q/220382/17776。 – jac 2013-03-04 16:13:28

回答

1

私人声明函数FindWindow函数库 “USER32” _ 别名 “FindWindowA”(BYVAL lpClassName,作为字符串_ BYVAL lpWindowName作为字符串),只要

私人声明函数PostMessage的库 “USER32” _ 别名“PostMessageA”(BYVAL HWND只要,_ BYVAL WMSG只要,_ BYVAL wParam中只要,_ lParam的作为不限),只要

私人CONST WM_CLOSE = & H10

私人小组的Form_Load() 昏暗strCap作为字符串 昏暗lngHwnd只要 昏暗lngRet只要 strCap = Me.Caption Me.Caption = “*” & strCap

lngHwnd = FindWindow函数(BYVAL vbNullString,BYVAL strCap)

如果lngHwnd <> 0然后 PostMessage的lngHwnd,WM_CLOSE,0 &,0 & 结束如果

Me.Caption = strCap

末次