473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ExitWindowsEx function not working.

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
Jun 12 '06 #1
5 7888
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=impersonat e,(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

Jun 12 '06 #2
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:
%2******************@TK2MSFTNGP05.phx.gbl...
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=impersonat e,(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

Jun 12 '06 #3
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.Mar shal.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.

Jun 13 '06 #4
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:
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.Mar shal.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.

Jun 13 '06 #5
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/ar...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.

Jun 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Fish | last post by:
Hi I am using Xalan-J 2.4.1 and I am trying to get the EXSLT node-set function working. xalan:nodeset() works fine but when I try exslt:node-set I get this error: (Location of error...
0
by: adamsbarker | last post by:
i can't seem to get the "ssh2_exec" function working on Windows XP. the example in the manual (which is obviously linux biased) says: ssh2_exec($connection, '/usr/local/bin/php -i'); i have...
1
by: Julie May | last post by:
I have 90% of my function working and I know to get the next 10% it is justa matter of getting the quotations and the escaping quotations correct. Here is the portion that does work: <working...
4
by: Olex Malko | last post by:
How to manage FindWindow() function working properly in ASP.NET application? In Windows Forms application it works fine. But in ASP.NET it returns NULL :( Probalby this is because of different...
6
by: Rudy | last post by:
Having problems getting the PMT function working. I'm new to programming, and I read the tech notes On how to set up. But it still doesn't work. Can somebody just give me an example the proper way...
1
by: tkkarthik | last post by:
Hello everyone.. Let me tell where i am in my programming.. I have 3 command buttons in my form. say a, b,c When I press the Key A in keyboard, I need the color of my command buton A to...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
3
by: dkyadav80 | last post by:
Hi sir, I'have written small function in javascript for date (month and year), this is good working in IE at onclick event and current month-8 , year-2008 are displaying but in firefox3,opera,&...
4
johny10151981
by: johny10151981 | last post by:
/*name of the built program is dmns*/ int main() { int i, j; i=fork(); if(i<0) return 1; printf("created pid=%d",i); while(1) {i=i;} //unlimited loop
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.