要设置特权级别的。
给你我的代码:/*
* This function is used to shutdown the destination computer.
* If no computer name is provided, it shutdowns the current system.
*/
bool ShutDown(DWORD mode, LPTSTR lpMachineName){ bool flag = false;
HANDLE tokHandle;
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &tokHandle))
{
TOKEN_PRIVILEGES priv;
priv.PrivilegeCount = 1;
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &priv.Privileges[0].Luid)
&& AdjustTokenPrivileges(tokHandle, FALSE, &priv, NULL, NULL, NULL)
&& (lpMachineName == NULL
? ExitWindowsEx(mode, 0)
: InitiateSystemShutdown(lpMachineName,
NULL,
0,
(mode & EWX_FORCE) != 0,
(mode & EWX_REBOOT) != 0)
)
)
flag = true;
}
return flag;
}
给你我的代码:/*
* This function is used to shutdown the destination computer.
* If no computer name is provided, it shutdowns the current system.
*/
bool ShutDown(DWORD mode, LPTSTR lpMachineName){ bool flag = false;
HANDLE tokHandle;
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &tokHandle))
{
TOKEN_PRIVILEGES priv;
priv.PrivilegeCount = 1;
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &priv.Privileges[0].Luid)
&& AdjustTokenPrivileges(tokHandle, FALSE, &priv, NULL, NULL, NULL)
&& (lpMachineName == NULL
? ExitWindowsEx(mode, 0)
: InitiateSystemShutdown(lpMachineName,
NULL,
0,
(mode & EWX_FORCE) != 0,
(mode & EWX_REBOOT) != 0)
)
)
flag = true;
}
return flag;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货