Home All Groups Group Topic Archive Search About

ExitWindowsEx function not working.

Author
12 Jun 2006 3:40 PM
cj
Using VB2003 on Windows XP Pro

Why doesn't this work?

Public Class Form1
     Inherits System.Windows.Forms.Form

     Private Declare Auto Function ExitWindowsEx Lib "user32.dll" (ByVal
uFlags As Int32, ByVal dwreserved As Int32) As Int32

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
         Dim retval As Int32
         retval = ExitWindowsEx(2 + 4, 0)
     End Sub
End Class

Author
12 Jun 2006 3:45 PM
cj
I am an administrator if that matters.  Also this does work.

Public Class Form1
     Inherits System.Windows.Forms.Form

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
         'Shutdown
         Dim t As Single
         Dim WMIService, Computer As Object
         WMIService =
GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
         For Each Computer In
WMIService.InstancesOf("Win32_OperatingSystem")
             t = Computer.Win32Shutdown(2 + 4, 0)
         Next
     End Sub
End Class

cj wrote:
Show quoteHide quote
> Using VB2003 on Windows XP Pro
>
> Why doesn't this work?
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
>     Private Declare Auto Function ExitWindowsEx Lib "user32.dll" (ByVal
> uFlags As Int32, ByVal dwreserved As Int32) As Int32
>
>     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button3.Click
>         Dim retval As Int32
>         retval = ExitWindowsEx(2 + 4, 0)
>     End Sub
> End Class
Author
12 Jun 2006 4:15 PM
Patrice
Try perhaps :
http://support.microsoft.com/kb/168796/en-us

It could be that you need also some code to grant this privilege to the
process...

--
Patrice

"cj" <cj@nospam.nospam> a écrit dans le message de news:
%23SW%232cjjGHA.2***@TK2MSFTNGP05.phx.gbl...
Show quoteHide quote
>I am an administrator if that matters.  Also this does work.
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         'Shutdown
>         Dim t As Single
>         Dim WMIService, Computer As Object
>         WMIService =
> GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
>         For Each Computer In
> WMIService.InstancesOf("Win32_OperatingSystem")
>             t = Computer.Win32Shutdown(2 + 4, 0)
>         Next
>     End Sub
> End Class
>
> cj wrote:
>> Using VB2003 on Windows XP Pro
>>
>> Why doesn't this work?
>>
>> Public Class Form1
>>     Inherits System.Windows.Forms.Form
>>
>>     Private Declare Auto Function ExitWindowsEx Lib "user32.dll" (ByVal
>> uFlags As Int32, ByVal dwreserved As Int32) As Int32
>>
>>     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles Button3.Click
>>         Dim retval As Int32
>>         retval = ExitWindowsEx(2 + 4, 0)
>>     End Sub
>> End Class
Author
13 Jun 2006 3:38 AM
Jeffrey Tan[MSFT]
Hi cj,

Thanks for your post!

Regarding Win32 programming, you should always check the API return value
to determine if the API calling fails. Once the API fails, we can use
Marshal.GetLastWin32Error() method to get the Win32 error code. With
searching this error code in errlook.exe, you can get the description text
of the error.
Note: errlook.exe is a tool followed with VS2005/VS.net2003. You can type
errlook.exe in "Visual Studio 2005 Command Prompt" to run the tool.

You should modify the code snippet as below:
Private Declare Auto Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags
As Int32, ByVal dwreserved As Int32) As Int32
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim retval As Int32
        retval = ExitWindowsEx(2 + 4, 0)
        If (retval = 0) Then

MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().T
oString())
        End If
    End Sub

With running, you will get error code 1314, which standards for "A required
privilege is not held by the client." in errlook.exe. With MSDN, you will
see that to shut down or restart the system, the calling process must use
the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
privilege. This is why our calling will fail.
Note: most of the privilege is disabled for the user account by default.
You have to use AdjustTokenPrivileges to enable it explicitly.

The KB provided by Patrice has demonstrating the correct way of calling
ExitWindowsEx. You may give it a try.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
13 Jun 2006 3:13 PM
cj
Jeffrey,  do you know where I can find a VB.net 2003 example of
rebooting a pc with ExitWindowsEx?  The MS page is working with VB6 and
I've already spent too long trying to get it to work in VB.net 2003.
The simpler the example the better.

Jeffrey Tan[MSFT] wrote:
Show quoteHide quote
> Hi cj,
>
> Thanks for your post!
>
> Regarding Win32 programming, you should always check the API return value
> to determine if the API calling fails. Once the API fails, we can use
> Marshal.GetLastWin32Error() method to get the Win32 error code. With
> searching this error code in errlook.exe, you can get the description text
> of the error.
> Note: errlook.exe is a tool followed with VS2005/VS.net2003. You can type
> errlook.exe in "Visual Studio 2005 Command Prompt" to run the tool.
>
> You should modify the code snippet as below:
>  Private Declare Auto Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags
> As Int32, ByVal dwreserved As Int32) As Int32
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         Dim retval As Int32
>         retval = ExitWindowsEx(2 + 4, 0)
>         If (retval = 0) Then
>            
> MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().T
> oString())
>         End If
>     End Sub
>
> With running, you will get error code 1314, which standards for "A required
> privilege is not held by the client." in errlook.exe. With MSDN, you will
> see that to shut down or restart the system, the calling process must use
> the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME
> privilege. This is why our calling will fail.
> Note: most of the privilege is disabled for the user account by default.
> You have to use AdjustTokenPrivileges to enable it explicitly.
>
> The KB provided by Patrice has demonstrating the correct way of calling
> ExitWindowsEx. You may give it a try.
>
> Hope this helps!
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
Author
14 Jun 2006 2:29 AM
Jeffrey Tan[MSFT]
Hi CJ,

Thanks for your feedback!

I can find the 2 VB.net sample below:
"Shut Down, Restart, and Log Off From Code "
http://blogs.msdn.com/brad_mccabe/archive/2005/03/02/383542.aspx
http://www.vbdotnetforums.com/showthread.php?p=3662

Additionally, http://www.pinvoke.net provides a lot of Win32 API
declaration regarding C#/VB.net, it is a good reference for doing p/invoke
in .Net. Also, any further interop and p/invoke questions can be posted in
microsoft.public.dotnet.framework.interop newsgroup.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.