Aborting system shutdown

Novatech / DON'T KNOW
January 8, 2009 at 10:17:25
Specs: Windows XP Pro, 1GB
Hi I want to abort system shutdown and do something and then continue shutdown procedure I am using Visual Basic Ok I have managed to do the rest but it doesnt abort system shutdown, my code is

If cont = vbCancel Then
AbortSystemShutdown vbNullString ' // Abort Localhost Shutdown
End If

There is a statement before this that works fine but it just doesn't abort the system shutdown if I pressed cancel there is a private sub on the top which is as follows

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


See More: Aborting system shutdown

Report •


#1
January 8, 2009 at 12:29:52
It might be useful to know what the previous statement is.

Where and how are you initiating the shutdown?

Why are you sending a Null string to the Function instead if the MachineName as the Function Declaration requires?

Stuart


Report •

#2
January 9, 2009 at 11:01:14
OK my full program is

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

Private Sub Form_Load()
' /* Declare Variables */
Dim wsh

' /* Hide The Form */
Me.Visible = False

' /* Set The Object Variable To A New Instance Of WScript.Shell */
Set wsh = CreateObject("WScript.Shell")

If Dir("c:\windows\system32\shut.exe") <> "" Then
Else
regist
End If

' /* Add The Program To The System Startup (In The Registry) */
'/*wsh.regwrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe", "REG_SZ"

End Sub
Private Sub regist()
Dim regkey
FileCopy App.Path & "\" & App.EXEName & ".exe", "C:\windows\system32\shut.exe"
Set regkey = CreateObject("wscript.shell")
regkey.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\shut.exe", "C:\windows\system32\shut.exe"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then ' // Windows Is Shutting Down

cont = MsgBox(" Windows Is Shutting down! Good Bye", vbOKCancel)
If cont = vbOK Then
Dim intFile As Integer
Dim strLine As String
intFile = FreeFile
Open "E:\Test" For Output As intFile
Print #intFile, "hi"
Close intFile
intFile = FreeFile
Open "E:\Test" For Input As intFile
Line Input #intFile, strLine
MsgBox strLine
Close intFile

Else

Dim intFile2 As Integer
Dim strLine2 As String
intFile = FreeFile
Open "F:\Test.txt" For Output As intFile
Print #intFile, "hi"
Close intFile
intFile2 = FreeFile
Open "F:\Test.txt" For Input As intFile
Line Input #intFile2, strLine2
MsgBox strLine2
Close intFile2


If cont = vbCancel Then
AbortSystemShutdown vbNullString ' // Abort Localhost Shutdown
End If



End If
End Sub


Report •

#3
January 9, 2009 at 13:12:47
After seeing the whole code it is as I suspected. You don't need the AbortSystemshutdown API at all.

All you need is to set the Cancel parameter to true and the shutdown will be aborted. Bear in mind though that this could leave the system in an unknown state as other things could have been shutdown before it gets to your routine.

The line if Cont = cancel is superfluous. For the programme to have go that far cont couldn't equal anything else but cancel. If it were VBOk, the only other option, then the first part of the If Then statement would be executed.

One other thing, if this is VB as apposed to VB Script then you shouldn't be using the wsScript. Although it works there are much better ways of writing to the registry in VB using the API. wsScript is designed to be used with VB Script but is is cumbersome and heavy on resources when used within VB.

Stuart


Report •

Related Solutions

#4
January 9, 2009 at 13:50:02
Ah ok thanks for that As you can notice from my code I sent a file to USB port 'E' and 'F' now here I am sending a text file as you can see I want to know is there a way to just send a chracter to USB port E and F rather than the text file and if possible could you write an example code as well please thanks

Report •

#5
January 9, 2009 at 14:50:19
You are not sending a files to USB port E and F. You are sending files to drives E: and F: It is just incidental that there is a USB device on the other end. From your code it could just as well be a floppy drive, hard disk or CD-RW.

You can't send a single character to a drive. It has to go to and be stored in a file.

Sending characters to a USB port is a whole different ball games altogether and I'm not sure you can even do it in VB without a lot of API calls. To do that you need to by-pass the file system and communicate directly with the system drivers. No easy task.

Stuart


Report •


Ask Question