borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (To read xml files)Next Topic (missing licence info) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Detecting Unload
Poster Message
Vpa
Level: Sage


Registered: 14-03-2004
Posts: 66

icon Detecting Unload

The most dull question ever, you may think. But I do have problems with it.
How do you detect when your program unloads?
In my case it is closed using Taskmanager, the other ways work fine with Form_Unload. The form is not visible

The Form_Unload event isn't fired , and when I'm subclassing my form I only get one event, And that one is in the beginning.
I believe I'm doing something wrong with the Subclassing, here's my code:

Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)
Dim m_Hwnd As Long
Dim PrevProc As Long

Public Sub Hook(ByVal hWnd As Long)
   If m_Hwnd <> 0 Then Unhook
   m_Hwnd = hWnd
   PrevProc = SetWindowLong(m_Hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub Unhook()
   SetWindowLong m_Hwnd, GWL_WNDPROC, PrevProc
   m_Hwnd = 0
End Sub

Private Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Open "tmp.txt" For Append As #1
      Print #1, uMsg
   Close #1
   WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
End Function


____________________________
Shondoit fais G'ot < http://home.deds.nl/~7dfp >

16-11-2005 at 11:31 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Detecting Unload

Hi again :-)

Use the QueryUnload event in your form

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

End Sub


and the unload mode has the following constants

[Pasted below from help]
The unloadmode argument returns the following values:

Constant Value Description
vbFormControlMenu 0 The user chose the Close command from the Control menu on the form.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Microsoft Windows operating environment session is ending.
vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application.
vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing.
vbFormOwner 5 A form is closing because its owner is closing.

Steve  

[Edited by steve_w on 16-11-2005 at 12:17 PM GMT]

16-11-2005 at 12:16 PM
View Profile Send Email to User Show All Posts | Quote Reply
Vpa
Level: Sage


Registered: 14-03-2004
Posts: 66
icon Re: Detecting Unload

Like I said in the first post, the Unload and QueryUnload events don't fire
(This occurs when you close it with the Taskmanager under (I believe) proccesses, when you have App.TaskVisible=false)

Got any other Ideas?

____________________________
Shondoit fais G'ot < http://home.deds.nl/~7dfp >

16-11-2005 at 12:20 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Detecting Unload

quote:
Like I said in the first post, the Unload and QueryUnload events don't fire


Can't see the mention of QueryUnload  

I'll have a look later  
16-11-2005 at 12:32 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1617
icon Re: Detecting Unload

If you actually "terminate the process" then I don't think you will be notified in-program (the purpose for the most part is to kill a non-responsive program, in a worst-case-senario).

If you really want to know if that happens... I think you'd have to have 2 programs running, both monitoring if the other's closed, and if so do whatever's needed (like a virus does).



____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

16-11-2005 at 08:12 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VB General : Detecting Unload
Previous Topic (To read xml files)Next Topic (missing licence info) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder