這裏是一個C++代碼,可以關閉與消息的計算機。
#include <windows.h>
#pragma comment(lib, "advapi32.lib")
BOOL MySystemShutdown(LPTSTR lpMsg)
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
lpMsg, // message for user
30, // time-out period, in seconds
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}
ShutdownBlockReasonCreate,您可以添加自定義消息。 –
ShutdownBlockReasonCreate與提問無關,請參閱其文檔並再次閱讀該問題。 –
該函數在關機屏幕上顯示消息(而不是在控制檯應用程序或消息框中),這似乎是請求的內容。我想你可以在消息字符串中編碼進度計數器。你不能做的是強制關機等到你的後臺進程完成。如果用戶說「無論如何關機」,那麼系統將會關閉。 –