• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

Detect WM_CLOSE event in vb6

I need to know when a particular process has shut down so I can shut down my running vb6 program
  • 3
  • 3
1 Solution
You can do this by using OpenProcess() API with the SYNCHRONIZE (0x00100000L) access right. After which you than create an event with CreateEvent() you can then wait on the process handle by using WaitForMultipleObjects().

If the process in question is already running before your process then the above applies. You will also need the Process ID you can either use CreateToolHelp32() to find the PID by name or FindWindow() and GetWindowThreadProcessId() to find the PID by title.

However if you  launc the application from your exe than you can use the Shell() command which will return you the process id.
cmrobertsonAuthor Commented:
I haven't abandoned the question, sorry i got busy and havent had a chance to try this - I am going to work on it in the next day or so and wanted to leave open in case i need a follow up question
Sure no problem. The only question I have is how you are opening the application.

Do you want to open the application from within your program.
Are you trying to do this on a program that is already running before your program starts.

The above will help when creating an example but a couple of things have to be altered depending on which reflects how you launch the program.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

cmrobertsonAuthor Commented:
I more than likely will be shelling to the application from inside my vb project
Here is a small example that should help you get started.

Create a class module : Class1
Add a command button to a form.
' Class1
Option Explicit
Private Const WAIT_OBJECT_0       As Long = 0
Private Const SYNCHRONIZE         As Long = &H100000
Private Const INFINITE            As Long = &HFFFFFFFF
Private Const QS_HOTKEY           As Long = &H80
Private Const QS_KEY              As Long = &H1
Private Const QS_MOUSEBUTTON      As Long = &H4
Private Const QS_MOUSEMOVE        As Long = &H2
Private Const QS_PAINT            As Long = &H20
Private Const QS_POSTMESSAGE      As Long = &H8
Private Const QS_SENDMESSAGE      As Long = &H40
Private Const QS_TIMER            As Long = &H10
Private Const QS_ALLINPUT         As Long = (QS_SENDMESSAGE _
  Or QS_KEY)
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Event OnProcessStart()
Public Event OnProcessClose()
Public PrematureExit  As Boolean
Private hProcess      As Long
Private mPid          As Long
Public Property Get IsWaiting() As Boolean
  If hProcess <> 0 Then
    IsWaiting = True
    IsWaiting = False
  End If
End Property
Public Sub Launch(ByVal szProgram As String)
  ' Wait on a single process to close.
  Dim bWait   As Long
  mPid = Shell(szProgram, vbNormalFocus)
  RaiseEvent OnProcessStart
  hProcess = OpenProcess(SYNCHRONIZE, 0, mPid)
    bWait = MsgWaitForMultipleObjects(1, hProcess, 0, INFINITE, QS_ALLINPUT)
      If PrematureExit Then
        bWait = WAIT_OBJECT_0
      End If
  Loop Until bWait = WAIT_OBJECT_0
  CloseHandle hProcess
  hProcess = 0
  If PrematureExit = False Then
    RaiseEvent OnProcessClose
  End If
End Sub
' Form1
Option Explicit
Dim WithEvents cProcessWait As Class1
Private Sub Command1_Click()
  If cProcessWait.IsWaiting = False Then
    cProcessWait.Launch "c:\windows\system32\calc.exe"
    Debug.Print "Already waiting...."
  End If
End Sub
Private Sub cProcessWait_OnProcessClose()
  Debug.Print "Process has closed : " & Time
End Sub
Private Sub cProcessWait_OnProcessStart()
  Debug.Print "Process launched at : " & Time
End Sub
Private Sub Form_Load()
  Set cProcessWait = New Class1
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  cProcessWait.PrematureExit = True
  Set cProcessWait = Nothing
End Sub

Open in new window

cmrobertsonAuthor Commented:
Excellent, exactly what I was looking for - Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now