C # code for logout, restart, and Shutdown,

Source: Internet
Author: User

C # code for logout, restart, and Shutdown,

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 );

}

 
C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.