First, import the namespace
Using system. runtime. interopservices;
References
[Structlayout (layoutkind. Sequential, pack = 1)]
Internal struct tokpriv1luid
{
Public int count;
Public long luid;
Public int ATTR;
}
[Dllimport ("kernel32.dll", exactspelling = true)]
Internal static extern intptr getcurrentprocess ();
[Dllimport ("advapi32.dll", exactspelling = true, setlasterror = true)]
Internal static extern bool openprocesstoken (intptr H, int ACC, ref intptr phtok );
[Dllimport ("advapi32.dll", setlasterror = true)]
Internal static extern bool lookupprivilegevalue (string host, string name, ref long pluid );
[Dllimport ("advapi32.dll", exactspelling = true, setlasterror = true)]
Internal static extern bool adjusttokenprivileges (intptr htok, bool disall,
Ref tokpriv1luid newst, int Len, intptr Prev, intptr relen );
[Dllimport ("user32.dll", exactspelling = true, setlasterror = true)]
Internal static extern bool exitwindowsex (INT flg, int rea );
Internal const int se_privilege_enabled = 0x00000002;
Internal const int token_query = 0x00000008;
Internal const int token_adjust_privileges = 0x00000020;
Internal const string se_shutdown_name = "seshutdownprivilege ";
Internal const int ewx_logoff = 0x00000000;
Internal const int ewx_shutdown = 0x00000001;
Internal const int ewx_reboot = 0x00000002;
Integer const int ewx_force = 0x00000004;
Internal const int ewx_poweroff = 0x00000008;
Internal const int ewx_forceifhung = 0x00000010;
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 );
}
Private void button2_click (Object sender, eventargs E)
{
// This code implements the restart function
Doexitwin (ewx_reboot );
}
Private void button3_click (Object sender, eventargs E)
{
// This code can be deregistered
Doexitwin (ewx_logoff );
}
Private void button1_click_1 (Object sender, eventargs E)
{
// This code implements the shutdown Function
Doexitwin (ewx_shutdown );
}