In C #, how does one call Windows API to shut down !!!

Source: Internet
Author: User
Http://chenpeng.cnblogs.com/archive/2006/05/24/408313.html

Due to work needs, I have written a LAN multimedia software. it involves shutdown, logout, and restart operations on the computer. system APIs are required. at the beginning, I found a lot of information. there are still errors. later, relevant information is found in msdn. however, it is in English. there is a difficulty. modified multiple times. finally, I wrote it out. now I shut down the API that calls the system and deregister it. the source code of the restart operation is sent out. for your reference .... if any. please leave a message. I am convenient to correct!

using system;
using system. collections. generic;
using system. text;
using system. runtime. interopservices;
using system. threading;
namespace syslibrary
{< br> public class sysconfig
{< br> [structlayout (layoutkind. sequential, pack = 1)]
internal struct tokpriv1luid
{< br> 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;
internal const int ewx_force = 0x00000004;
internal const int ewx_poweroff = 0x00000008;
internal const int ewx_forceifhung = 0x00000010;

Private bool 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 );
Return OK;
}

Public void logoff ()
{
Doexitwin (ewx_logoff );
}

Public void reboot ()
{
Doexitwin (ewx_reboot );
}

Public void Shutdown ()
{
Doexitwin (ewx_shutdown );
}

}
}

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.