Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect whether VB app is closed by user or by the system

Status
Not open for further replies.

khizerharoon

Programmer
Jan 21, 2004
3
GB
I need to check whether the app is being closed by the system or by the user itself.

if the user is shutting down the windows, he would cause the application to abort normally which I want to avoid. Basically i wanna avoid the user to NOT to press the "End Task" button.

I can manage to stop the shutdown by the following API

Public Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long

what i wanna know is whether I can s judge my app is being closed by the system or by the user. if it is system i would call abort....but if it is the user then i can easily unload the form and save my settings etc.

 
you want to create a virus?

Ion Filipski
1c.bmp
 

Welcome to TT khizerharoon. To get the most from this site please read FAQ222-2244.

Now for your VB question it should be asked in the Visual Basic(Microsoft) Version 5 & 6 Forum222 and while you are reading look up in vb's help files queryunload and read about unloadmode.

Then know that if I as a user had a program like yours I would kill it and delete it and if it did any harm to my system I would be seeking damages from you! I think I would like to replace my old system with one of the new 64 bit chips comming out along with a few gigs of ram let alone some nice new 7200 rpm 120 gig drives or perhaps the SCSI 10000 rpm drives.

Good Luck

 
WEll first of all i am not making a virus :p
I am working on a VB app which saves info in registery and files when it is closed. Therefore when it is improperly shutdown like "End Tasked" forced by the user, the program next time starting is a nightmare.
Anyways I figured out the solution, ask me if someone needs it.
Reguards

 
khizerharoon, welcome to Tek-Tip as vb5prgrmr suggested as a new member you should read FAQ222-2244.

As far a posting the solution, it is always, not only recommended but expected that the resolution be posted.

Taken directly from FAQ222-2244 Item number 15

Please post the solution if you solve a problem yourself. That way, all questions will have the solutions. And all of us can learn from others' experience.
 
Sure why not, here is the code

'declare API for stop user for closing the application
Public Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long


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

Dim objAPIs As New clsAPIcalls

If UnloadMode = vbAppWindows Or UnloadMode = vbAppTaskManager Then
'stop user from shutting down
boolGlobalAppExit = False
AbortSystemShutdown objAPIs.GetComputerName
Cancel = 1
MsgBox "Please close the application explicitly before shutting down"
Else
'keep the form loaded
boolGlobalAppExit = True
Cancel = 0
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top