473,382 Members | 1,409 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,382 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 7887
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.