Class C # Win200 shutdown code passed by debugging

xiaoxiao2021-03-06  97

Using system; using system.runtime.interopservices;

Namespace AutoexitWindows {///

/// Newshutdown's summary description. /// public class newshutdown {[structLayout (layoutkind.sequential, pack = 1)] Internal struct tokpriv1luid {public inters; public long 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 INTPTPTPTPTOK);

[DLLIMPORT ("Advapi32.dll", SetLastError = true)] Internal Static Extern Bool (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 ; INTERNAL 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);} public newshutdown () {// // Todo: Add constructor logic // console.writeline ("Putting the computer ..."); // Modify EWX_LOGOFF, EWX_REBOOT, etc. Different functions. // You can see the help information under XP to get different parameters // shutdown /? Doexitwin (EWX_Poweroff);

}}}

转载请注明原文地址:https://www.9cbs.com/read-97050.html

New Post(0)