Disable computer code

Source: Internet
Author: User

On the Internet

 

  1. UsingSystem;
  2. UsingSystem. Runtime. InteropServices;
  3. NamespaceFlyingzl
  4. {
  5. Public ClassCloseWindow
  6. {
  7. [StructLayout (LayoutKind. Sequential, Pack = 1)]
  8. Internal StructTokPriv1Luid
  9. {
  10. Public IntCount;
  11. Public LongLuid;
  12. Public IntAttr;
  13. }
  14. [DllImport ("kernel32.dll", ExactSpelling =True)]
  15. Internal Static ExternIntPtr GetCurrentProcess ();
  16. [DllImport ("advapi32.dll", ExactSpelling =True, SetLastError =True)]
  17. Internal Static Extern BoolOpenProcessToken (IntPtr h,IntAcc,RefIntPtr phtok );
  18. [DllImport ("advapi32.dll", SetLastError =True)]
  19. Internal Static Extern BoolLookupPrivilegeValue (StringHost,StringName,Ref LongPluid );
  20. [DllImport ("advapi32.dll", ExactSpelling =True, SetLastError =True)]
  21. Internal Static Extern BoolAdjustTokenPrivileges (IntPtr htok,BoolDisall,
  22. RefTokPriv1Luid newst,IntLen, IntPtr prev, IntPtr relen );
  23. [DllImport ("user32.dll", ExactSpelling =True, SetLastError =True)]
  24. Internal Static Extern BoolExitWindowsEx (IntFlg,IntRea );
  25. // The following defines the constants required to call WinAPI. These constants can usually be found in the Platform SDK's inclusion file (header file ).
  26. Internal Const IntSE_PRIVILEGE_ENABLED = 0x00000002;
  27. Internal Const IntTOKEN_QUERY = 0x00000008;
  28. Internal Const IntTOKEN_ADJUST_PRIVILEGES = 0x00000020;
  29. Internal Const StringSE_SHUTDOWN_NAME = "SeShutdownPrivilege ";
  30. Internal Const IntEWX_LOGOFF = 0x00000000;
  31. Internal Const IntEWX_SHUTDOWN = 0x00000001;
  32. Internal Const IntEWX_REBOOT = 0x00000002;
  33. Internal Const IntEWX_FORCE = 0x00000004;
  34. Internal Const IntEWX_POWEROFF = 0x00000008;
  35. Internal Const IntEWX_FORCEIFHUNG = 0x00000010;
  36. // Shutdown is achieved by calling WinAPI. The main code is followed by the last line of ExitWindowsEx, which calls the WinAPI of the same name, which is used for shutdown.
  37. Private Static VoidDoExitWin (IntFlg)
  38. {
  39. BoolOK;
  40. TokPriv1Luid tp;
  41. IntPtr hproc = GetCurrentProcess ();
  42. IntPtr htok = IntPtr. Zero;
  43. OK = OpenProcessToken (hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,RefHtok );
  44. Tp. Count = 1;
  45. Tp. Luid = 0;
  46. Tp. Attr = SE_PRIVILEGE_ENABLED;
  47. OK = LookupPrivilegeValue (Null, SE_SHUTDOWN_NAME,RefTp. Luid );
  48. OK = AdjustTokenPrivileges (htok,False,RefTp, 0, IntPtr. Zero, IntPtr. Zero );
  49. OK = ExitWindowsEx (flg, 0 );
  50. }
  51. Public Static VoidShutDown (){
  52. DoExitWin (EWX_FORCE | EWX_SHUTDOWN );
  53. }
  54. }
  55. }

 

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.