|
-
Jun 27th, 2007, 09:46 AM
#1
Thread Starter
New Member
What API should I call to abort Logoff/Shutdown ?
I need to write a script that will run every time someone tries to log
off or shutdown a computer. In the past people have logged off
computers while virtual machines were running QE tests or automation.
So it would always be a real pain when these VMs were shut off without
warning. I need to write a script that would check for VMware's
executable file (vmware-vmx.exe) and (if the process is found) prompt
a warning to the user and stop the shut down or log off process from
happening.
I've done A LOT of research on the internet about logoff/shutdown
scripts, posted on message boards and I've emailed people that I
know. I've found four different API calls/messages and I can't move forward with my scripts until I find out which one I should use:
AbortSystemShutdown - This call aborts a shutdown initiated by
InitiateSystemSutdown or InitiateSystemShutdownEx calls. ASS is
thrown during the timeout period specified by ISS or ISSE. I don't know if it
will work for the console session since ISS and ISSE are only used in
RDP connections. Does anyone know if this call work to abort a
shutdown initiated by ExitWindowsEx?
ExitWindowsEx - Can shutdown, logoff, or restart a computer and pass
reasons to the event log as to why any of these happened. I know that this API sends the WM_QUERYENDSESSION message which can potentially abort a logoff/shutdown if the a value of "0" is returned. How do I respond to this message with "0" in a script? I haven't found any examples on the internet.
SystemEvents.SessionEnding - Can cancel shutdown. I don't know if this is the best API to use though. There are too many caveats: console apps don't raise this event, cancel only works sometimes, and there's no
guarantee this event will fire before the closing event fires.
I would appreciate any help or suggestions you all can give me.
P.S. Just in case your wondering, I'm aware of the fact that I can't
directly call an API from a script and that I have to use either MS
Office as proxy to load the code into a macro to make the calls or use
an ActiveX control to act as a wrapper to make the calls. Sorry this
post is so long.
Last edited by kbern3; Jun 28th, 2007 at 08:36 AM.
Reason: grammer
-
Jun 29th, 2007, 01:16 PM
#2
Addicted Member
Re: What API should I call to abort Logoff/Shutdown ?
-
Jun 29th, 2007, 02:05 PM
#3
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
I thought the command "shutdown -a" would work. When I first tested my script, I used this command. Unfortunately, this doesn't work. I think the reason why is because shutdown -a aborts a shutdown that's initiated from the command line. Start > Shutdown doens't use the command line to shutdown. It uses the API call ExitWindowsEx. I need to use another API to abort this (or somehow intercept WM_QUERYENDSESSION and return a value of "0" ). Thanks for replying though. I'm glad someone tried to help. I've been all over the internet at several different forums and scripting websites. It seems that no one has attempted this before.
-
Jun 29th, 2007, 02:20 PM
#4
Re: What API should I call to abort Logoff/Shutdown ?
You'd have to create a window that sits offscreen and listens for WM_QUERYENDSESSION, as far as I can tell. No can do in a script.
-
Jun 29th, 2007, 02:25 PM
#5
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
Based on your knowledge, is this a simple program? I just got into vbscript three weeks ago so at the moment, vb is beyond my understanding. Should the code for this be easy to find on the internet?
Last edited by kbern3; Jun 29th, 2007 at 02:25 PM.
Reason: grammer
-
Jun 29th, 2007, 02:35 PM
#6
Re: What API should I call to abort Logoff/Shutdown ?
You seem the competent sort.
If you want to use VB (classic) then you'll need to subclass a form. There are examples of that about this forum.
If you use .NET, it's a lot easier; you just need to override some method in your form class (can't remember what it's called offhand).
So: what tools do you have at your disposal?
-
Jun 29th, 2007, 02:47 PM
#7
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
I have Visual Studio.NET 2003 on my machine. I use this program to edit my scripts.
-
Jun 29th, 2007, 03:09 PM
#8
Re: What API should I call to abort Logoff/Shutdown ?
Actually, if you're going to use .NET, it's even easier: the framework provides an abstraction of that window message.
Here is a C# example.
Code:
void ShutdownListenerWindow_FormClosing (object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{
// Get X processes
Process[] x_processes = Process.GetProcessesByName("x");
if (x_processes.Length > 0) {
if (
MessageBox.Show(
"Are you sure you wish to shutdown with " + x_processes.Length + " X open?",
"Confirm Shutdown",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning
) == DialogResult.Yes
)
e.Cancel = true;
}
}
}
-
Jun 29th, 2007, 03:17 PM
#9
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
Thank You! So all I have to do is replace x with vmware-vmx.exe? Is it really that simple? Btw - where did you get this code? I would like to see if I can find the same thing in visual basic.NET
-
Jun 29th, 2007, 03:23 PM
#10
Re: What API should I call to abort Logoff/Shutdown ?
I wrote it
Here it is in VB.NET:
Code:
Private Sub ShutdownListenerWindow_FormClosing (sender As Object, e As FormClosingEventArgs) Handles MyBase.Closing
If (e.CloseReason = CloseReason.WindowsShutDown) Then
' Get X processes
Dim x_processes As Process() = Process.GetProcessesByName("x")
If (x_processes.Length > 0) Then
If ( _
MessageBox.Show( _
"Are you sure you wish to shutdown with " + x_processes.Length + " X open?", _
"Confirm Shutdown", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning _
) = DialogResult.Yes _
) Then _
e.Cancel = True
End If
End If
End Sub
-
Jun 29th, 2007, 03:47 PM
#11
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
WOW. That was quick. Thank you. You must program for a living. I don't know of many people who have a good working knowledge of more than one language. The ones I know are excellent at one and mediocre at the other. Thank you for doing most of the work for me. Now I just have to figure out the UI and debugging part.
-
Jul 6th, 2007, 04:26 PM
#12
New Member
Re: What API should I call to abort Logoff/Shutdown ?
Correct me if I"m wrong, but won't that just stop that program from closing ? Windows is still going to attempt to shutdown; however, it will pop up a window asking if you want to End the task now as it will think its not responding ?
-
Jul 9th, 2007, 08:44 AM
#13
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
Correct. It will stop that program from closing. I didn't know Windows would attempt to shutdown anyway though. I guess there isn't really a way to prevent shutdown once it has been initiated. At least there's a window that will ask if I want to end the task. That's close enough for me.
-
Jul 11th, 2007, 10:50 AM
#14
Frenzied Member
Re: What API should I call to abort Logoff/Shutdown ?
Maybe you could add AbortSystemShutdown() inside the IF statement (of penegate's code) to abort the shutdown.
-
Jul 11th, 2007, 03:14 PM
#15
Hyperactive Member
Re: What API should I call to abort Logoff/Shutdown ?
I found this code a while ago, if you leave it running in the background or something, when the user gose to shutdown it wont let he/she shutdown the computer.
Code:
'constants needed, form level
Private Const WM_QUERYENDSESSION As System.Int32 = &H11
Private Const WM_CANCELMODE As System.Int32 = &H1F
'the sub to intercept the windows messages
Protected Overrides Sub WndProc(ByRef ex As Message)
If ex.Msg = WM_QUERYENDSESSION Then
'cancel the message
Dim MyMsg As New Message
MyMsg.Msg = WM_CANCELMODE
MyBase.WndProc(MyMsg)
Else
'send the message as normal
MyBase.WndProc(ex)
End If
End Sub
-
Jul 12th, 2007, 09:50 AM
#16
Thread Starter
New Member
Re: What API should I call to abort Logoff/Shutdown ?
Great! Thanks for finding the code. I wish I had my own personal programmer. I prefer batch files and vbscripts much more.
l4m3c0d3r - adding it in the 'if' statement is a good idea. That's a good way to cover all my bases. Just out of curiosity, what does your user name mean?
-
Jul 12th, 2007, 11:28 AM
#17
Frenzied Member
Re: What API should I call to abort Logoff/Shutdown ?
FireKnox, exactly how does this code work? Is that code only intercepting messages sent to the form or the whole system?
This code:
MyBase.WndProc(MyMsg)
the code above, does it basically cancel the shutdown for the whole system because the form (where the code resides) won't let itself be "shutdown"? So basically, if one program doesn't let itself shutdown, the whole system doesn't shutdown?
-
Jul 15th, 2007, 01:56 AM
#18
Hyperactive Member
Re: What API should I call to abort Logoff/Shutdown ?
The code will intercept the whole system. for example, i put it in my forms loading sub, so if i leave the form open an i go an click shutdown the form will see that the computer wants to shutdown but wont let it. So yes, it basically cancels the shutdown of the whole system.
-
Dec 20th, 2007, 02:11 PM
#19
Banned
Re: What API should I call to abort Logoff/Shutdown ?
I just found out about this thread. I'm trying to do something similar, but with Windows XP shutdown scripts.
The VBScript basically checks if someone is still connected to the computer when trying to shut down, and if true, it warns te user:
Code:
Set objShell = CreateObject("Wscript.Shell")
intReturn = objShell.Popup("Connections detected! Abort shutdown?", 10, "Abort shutdown", 4 + 48)
Select Case intReturn
case 6
Msgbox "Aborting shutdown."
' Somehow abort shutdown...
Msgbox "Shutdown aborted"
case -1
Msgbox "Aborting shutdown."
' Somehow abort shutdown...
Msgbox "Shutdown aborted"
End Select
Can I also use your code above in my script somehow?
Last edited by marcjack; Dec 20th, 2007 at 03:40 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|