Re: How to call ExitWindows or ExitWindowsEx in C#?

A

Arild Bakken

Hi,

Have you enabled the SE_SHUTDOWN_NAME privilege for the process using
AdjustTokenPrivilege()? Check the docs on ExitWindowsEx() for information
about this.

Arild
 
W

Will Pittenger

I saw that in the document, but do not understand what is going on or who to
do what you are talking about. Is this something that I change in code or
in the shortcut?
----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Arild Bakken said:
Hi,

Have you enabled the SE_SHUTDOWN_NAME privilege for the process using
AdjustTokenPrivilege()? Check the docs on ExitWindowsEx() for information
about this.

Arild

Will Pittenger said:
Please note that I am a MFC programmer that is just starting with C#. I
have never created services and have no idea what is meant by marshalling.

I have a class that displays options to shut down, reboot, or log out. You
can assume the current user is the administrator. The code below will log
the user out successfully, but the reboot and power off options are ignored.
Furthermore, WM_QUERYENDSESSION is never sent. I can tell because Visual
Studio acts like it crashed if it was open when I ran the program.

private static uint EWX_LOGOFF = 0;
//private static uint EWX_SHUTDOWN = 1;
private static uint EWX_REBOOT = 2;
//private static uint EWX_FORCE = 4;
private static uint EWX_POWEROFF = 8;

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

// Now inside a function that has decided to shutdown Windows.
uint shutDownFlags = 0;
switch(closeType)
{
case CWOptions.shutDown:
shutDownFlags = EWX_POWEROFF;
break;

case CWOptions.reboot:
shutDownFlags = EWX_REBOOT;
break;

case CWOptions.logOut:
shutDownFlags = EWX_LOGOFF;
break;
}
ExitWindowsEx(shutDownFlags, 1);
 
A

Arild Bakken

You need to do it it code. The steps are as follows:

1. Get the process token with OpenProcessToken()
2. Lookup the privilege value with LookupPrivilegeValue() for
"SeShutdownPrivilege"
3. Adjust the process token privileges with AdjustTokenPrivileges() adding
the value from step 2


Arild

Will Pittenger said:
I saw that in the document, but do not understand what is going on or who to
do what you are talking about. Is this something that I change in code or
in the shortcut?
----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Arild Bakken said:
Hi,

Have you enabled the SE_SHUTDOWN_NAME privilege for the process using
AdjustTokenPrivilege()? Check the docs on ExitWindowsEx() for information
about this.

Arild

Will Pittenger said:
Please note that I am a MFC programmer that is just starting with C#. I
have never created services and have no idea what is meant by marshalling.

I have a class that displays options to shut down, reboot, or log out. You
can assume the current user is the administrator. The code below will log
the user out successfully, but the reboot and power off options are ignored.
Furthermore, WM_QUERYENDSESSION is never sent. I can tell because Visual
Studio acts like it crashed if it was open when I ran the program.

private static uint EWX_LOGOFF = 0;
//private static uint EWX_SHUTDOWN = 1;
private static uint EWX_REBOOT = 2;
//private static uint EWX_FORCE = 4;
private static uint EWX_POWEROFF = 8;

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

// Now inside a function that has decided to shutdown Windows.
uint shutDownFlags = 0;
switch(closeType)
{
case CWOptions.shutDown:
shutDownFlags = EWX_POWEROFF;
break;

case CWOptions.reboot:
shutDownFlags = EWX_REBOOT;
break;

case CWOptions.logOut:
shutDownFlags = EWX_LOGOFF;
break;
}
ExitWindowsEx(shutDownFlags, 1);
 
A

Arild Bakken

I'm afraid I don't have any .NET sample code, but attached is VB6 code. It
uses WinAPI calls for most of the stuff which you should be able to convert
to C# dllimports.

Arild


Will Pittenger said:
Do you have some sample code explaining this? Some sample code from the
Visual Studio Samples or Microsoft KB would work too. There are two
problems that I am running into. One is how to handle the PHANDLE
parameter. (I think that I can call it a "ref IntPtr", but am not sure. I
suppose if I had worked on projects that used C++ and VB, I would
understand, but as is...) The other is where do I get the process handle?
I see the Process class has a property called Handle, but is this what I
need and how go I get at an instance of Process?
----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Arild Bakken said:
You need to do it it code. The steps are as follows:

1. Get the process token with OpenProcessToken()
2. Lookup the privilege value with LookupPrivilegeValue() for
"SeShutdownPrivilege"
3. Adjust the process token privileges with AdjustTokenPrivileges() adding
the value from step 2


Arild

who
code
or
in the shortcut?
----------
Will Pittenger
E-Mail: mailto:[email protected]
All mail filtered by Qurb (www.qurb.com)
Hi,

Have you enabled the SE_SHUTDOWN_NAME privilege for the process using
AdjustTokenPrivilege()? Check the docs on ExitWindowsEx() for information
about this.

Arild

Please note that I am a MFC programmer that is just starting with
C#.
I
have never created services and have no idea what is meant by
marshalling.

I have a class that displays options to shut down, reboot, or log out.
You
can assume the current user is the administrator. The code below will
log
the user out successfully, but the reboot and power off options are
ignored.
Furthermore, WM_QUERYENDSESSION is never sent. I can tell because
Visual
Studio acts like it crashed if it was open when I ran the program.

private static uint EWX_LOGOFF = 0;
//private static uint EWX_SHUTDOWN = 1;
private static uint EWX_REBOOT = 2;
//private static uint EWX_FORCE = 4;
private static uint EWX_POWEROFF = 8;

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

// Now inside a function that has decided to shutdown Windows.
uint shutDownFlags = 0;
switch(closeType)
{
case CWOptions.shutDown:
shutDownFlags = EWX_POWEROFF;
break;

case CWOptions.reboot:
shutDownFlags = EWX_REBOOT;
break;

case CWOptions.logOut:
shutDownFlags = EWX_LOGOFF;
break;
}
ExitWindowsEx(shutDownFlags, 1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top