C # written Win2000 shutdown code

xiaoxiao2021-03-06  74

After querying online and MSDN, I know some code about Win2000 shutdown. After the C # code is changed, you are as follows: use system; use system.runtime.interopservices; using system.io;

Namespace AutoExitWindows {///

/// exitwindows Summary Description. /// Shutdown, logout windows. /// /// public class exitwindows {public const INT EWX_LOGOFF = 0; // Exit (Logic) PUBLIC Const Int EWX_SHUTDOWN = 1; // 'Shutdown Public Const Int EWX_REBOOT = 2; // 'restart public const INT EWX_FORCE = 4; //' Force shutdown, that is, not to notify the current active application to let him close

public const int TOKEN_ADJUST_PRIVILEGES = 0x20; public const int TOKEN_QUERY = 0x8; public const int SE_PRIVILEGE_ENABLED = 0x2; public const int ANYSIZE_ARRAY = 1; struct LUID {int lowpart; int highpart; LUID (int low, int high) {lowpart = low; highpart = high;}} struct LUID_AND_ATTRIBUTES {LUID pLuid; int Attributes; public LUID_AND_ATTRIBUTES (LUID luid, int attributes) {pLuid = luid; Attributes = attributes;}} struct TOKEN_PRIVILEGES {int PrivilegeCount; LUID_AND_ATTRIBUTES Privileges; public TOKEN_PRIVILEGES (int privilegecount, LUID_AND_ATTRIBUTES privileges) {privilegeCount = privilegecount; privileges = privileges;}} [DllImport ( "user32", SetLastError = true)] static extern int ExitWindowsEx (int uFlags, int dwReserved);

[DLLIMPORT ("kernel32", setlasterror = true)] static extern int getcurrentprocess ();

[DLLIMPORT ("Advapi32", SetLastError = true)] static extern int lookupprivilerage (String lpsystemname, string lpname, ref luid lpluid);

[DllImport ( "advapi32", SetLastError = true)] static extern int AdjustTokenPrivileges (int TokenHandle, int DisableAllPrivileges, TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, int ReturnLength); [DllImport ( "advapi32", SetLastError = true)] static EXTERN INT OpenProcessToken (int processhandle, int desiredaccess, ref int tokenhand);

PUBLIC EXITWINDOWS () {/// Todo: Add constructor logic //} here

Public void closewindows (int exitType) {adjusttokenprivilegesfornt (); exitwindowsex (exittype, 0);}

// This function is used for public static void AdjustTokenPrivilegesForNT NT Shutdown used () {int hdlProcessHandle = 0; int hdlTokenHandle = 0; LUID tmpLuid = new LUID (); TOKEN_PRIVILEGES tkp; TOKEN_PRIVILEGES tkpNewButIgnored = new TOKEN_PRIVILEGES (); int lBufferNeeded = 0; hdlProcessHandle = GetCurrentProcess (); OpenProcessToken (hdlProcessHandle, TOKEN_ADJUST_PRIVILEGES, ref hdlTokenHandle); LookupPrivilegeValue ( "", "SeShutdownPrivilege", ref tmpLuid); LUID_AND_ATTRIBUTES luidtemp = new LUID_AND_ATTRIBUTES (tmpLuid, SE_PRIVILEGE_ENABLED); tkp = new TOKEN_PRIVILEGES (1, LuidTemp); AdjustTokenPrivileges (HDLTokenhandle, 0, TKP, 100, Ref TkpnewButignored, lbufferneed);

}

EXITWINDOWS EXITTEMP = New EXITWINDOWS (); EXITTETEMP.CLOSEWINDOWS (4);

Test, which can only log out of the user without shutting down.

Currently, I haven't found code that can truly shut down, waiting for and improvement.

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

New Post(0)