ExitWindowsEx from service

  • Thread starter Thread starter Gary Lamb
  • Start date Start date
G

Gary Lamb

Hi,

I have written a .NET service that, if certain conditions are met, reboots
the server by calling
ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 0)

This works fine on my XP Professional box, but does not work on the server
which is running windows 2000 (it has the framework installed)
The entire function is:

public static void reboot()
{
DoExitWin(EWX_REBOOT + EWX_FORCE);
}

private static void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref
htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero,
IntPtr.Zero );

ok = ExitWindowsEx( flg, 0 );
}

Does anybody have any ideas?

Thanks

Gary
 
it is:

[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
 
Gary,
[StructLayout(LayoutKind.Sequential, Pack=1)]
^^^^^^

As far as I can tell from Winnt.h, TOKEN_PRIVILEGES doesn't have byte
packing. So I'd try removing the Pack assignment from the attribute.

If that doesn't help, it would be great if you could tell us which of
the functions you're calling it is that's failing, and any error code
it might return.



Mattias
 
Mattias,

thx for that, I changed the packing but it still does not work.
I get an error 1300 from AdjustTokenPrivileges (meaning I could not set the
privilege).

However, my service is running as Administrator, so I guess it has the
privilege to adjust
tokens.

It all works fine on XP, just doesn't work on win2000.

Cheers


Mattias Sjögren said:
Gary,
[StructLayout(LayoutKind.Sequential, Pack=1)]
^^^^^^

As far as I can tell from Winnt.h, TOKEN_PRIVILEGES doesn't have byte
packing. So I'd try removing the Pack assignment from the attribute.

If that doesn't help, it would be great if you could tell us which of
the functions you're calling it is that's failing, and any error code
it might return.



Mattias
 
Back
Top