我举一个例子吧(C强制重启代码):
#define PB_ID 1
#include <stdio.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "Advapi32.lib")
typedef void *HANDLE;
typedef struct _LUID {
unsigned long LowPart;
long HighPart;
} LUID, *PLUID;
typedef struct _LUID_AND_ATTRIBUTES {
LUID Luid;
unsigned long Attributes;
} LUID_AND_ATTRIBUTES, * PLUID_AND_ATTRIBUTES;
typedef struct _TOKEN_PRIVILEGES {
unsigned long PrivilegeCount;
LUID_AND_ATTRIBUTES Privileges[1];
} TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES;
#define FALSE false
#define TOKEN_QUERY (0x0008)
#define TOKEN_ADJUST_PRIVILEGES (0x0020)
#define SE_PRIVILEGE_ENABLED (0x00000002L)
#define EWX_LOGOFF 0
#define EWX_SHUTDOWN 0x00000001
#define EWX_REBOOT 0x00000002
#define EWX_FORCE 0x00000004
#define EWX_POWEROFF 0x00000008
#define SE_SHUTDOWN_NAME ("SeShutdownPrivilege")
extern "C" __declspec(dllimport) int __stdcall LookupPrivilegeValueA(char *,char *,PLUID);
extern "C" __declspec(dllimport) int __stdcall OpenProcessToken(void *,unsigned long,void **);
extern "C" __declspec(dllimport) HANDLE __stdcall GetCurrentProcess(void);
extern "C" __declspec(dllimport) int __stdcall ExitWindowsEx(unsigned int,unsigned long);
extern "C" __declspec(dllimport) int __stdcall AdjustTokenPrivileges
(HANDLE,int,PTOKEN_PRIVILEGES,unsigned long,PTOKEN_PRIVILEGES,unsigned long *);
int main()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValueA(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
return 0;
}