杀死以前的VB6应用程序实例

时间:2013-03-01 20:27:53

标签: 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调用。如何在服务恢复时正确终止先前加载的实例?谢谢拉里

1 个答案:

答案 0 :(得分:1)

私有声明函数FindWindow Lib“user32”_ 别名“FindWindowA”(ByVal lpClassName As String,_ ByVal lpWindowName As String)As Long

私有声明功能PostMessage Lib“user32”_ 别名“PostMessageA”(ByVal hwnd As Long,_ ByVal wMsg As Long,_ ByVal wParam As Long,_ lParam As Any)As Long

Private Const WM_CLOSE =&amp; H10

Private Sub Form_Load() Dim strCap As String Dim lngHwnd As Long Dim lngRet As Long strCap = Me.Caption Me.Caption =“*”&amp; strCap

lngHwnd = FindWindow(ByVal vbNullString,ByVal strCap)

如果lngHwnd&lt;&gt; 0然后     PostMessage lngHwnd,WM_CLOSE,0&amp;,0&amp; 结束如果

Me.Caption = strCap

End Sub