ExitWindowsEx() of user32.dll Not Functioning

G

Guest

Hi everybody,
Please look at this code, the rebooting/reset does not happen. Waht is
wrong with this code?
How to fixed this?

Code:
[StructLayout(LayoutKind.Sequential, Pack=1)]
private struct Luid //change to internal public accessor not working
{
public int Count;
public long pLuid;
public int Attr;
}

const int SE_PRIVILEGE_ENABLED = 0x2;
const int TOKEN_QUERY = 0x8;
const int TOKEN_ADJUST_PRIVILEGES = 0x20;
const int EWX_REBOOT = 0x2;
const int EWX_FORCE = 0x4;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";


[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool
disall,ref Luid newst, int

len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll")]
static extern int ExitWindowsEx(int uFlags, int dwReason);

private void ActivatePrivileges()
{
Luid uID;
IntPtr ptrProcess = Process.GetCurrentProcess().Handle;
IntPtr ptrToken = IntPtr.Zero;
//Need to adjust the priviledge in order to shut down...
OpenProcessToken(ptrProcess, TOKEN_ADJUST_PRIVILEGES / TOKEN_QUERY,
ptrToken);
uID.Count = 1;
uID.pLuid = 0;
uID.Attr = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, uID.pLuid);
AdjustTokenPrivileges(ptrToken, false, uID, 0, IntPtr.Zero,
IntPtr.Zero); //Error Occurs

here
}


//Executing inside
private void btnReset_Click(object sender, System.EventArgs e)
{
ActivatePrivileges();
ExitWindowsEx(2, 0);//This does nothing
}


Den2005
 
M

Mattias Sjögren

Please look at this code, the rebooting/reset does not happen. Waht is
wrong with this code?

I recommend you start chcking the return values if the functions you
call. That should make it a lot easier to tell where it's failing.

OpenProcessToken(ptrProcess, TOKEN_ADJUST_PRIVILEGES / TOKEN_QUERY,
ptrToken);

You want to use the | (OR) operator to combine the access flags, not
the division operator /.

LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, uID.pLuid);

The last parameter must be declared ref or out for uID.pLuid to be
assigned to.


Mattias
 
G

Guest

Thanks for reply Mattias, I have found another sample and applied and it works.

den2005
 

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