Solved

Detect WM_CLOSE event in vb6

Posted on 2009-05-06
6
807 Views
Last Modified: 2012-05-06
I need to know when a particular process has shut down so I can shut down my running vb6 program
0
Comment
Question by:cmrobertson
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 24322402
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.
0
 
LVL 7

Author Comment

by:cmrobertson
ID: 24388745
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
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 24391551
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Author Comment

by:cmrobertson
ID: 24391765
I more than likely will be shelling to the application from inside my vb project
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 24397372
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_PAINT _

  Or QS_TIMER _

  Or QS_POSTMESSAGE _

  Or QS_MOUSEBUTTON _

  Or QS_MOUSEMOVE _

  Or QS_HOTKEY _

  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

  Else

    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)
 

  Do

    bWait = MsgWaitForMultipleObjects(1, hProcess, 0, INFINITE, QS_ALLINPUT)

      If PrematureExit Then

        bWait = WAIT_OBJECT_0

      End If

    DoEvents

  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"

  Else

    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

0
 
LVL 7

Author Closing Comment

by:cmrobertson
ID: 31578729
Excellent, exactly what I was looking for - Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now