进程权限提升参见 http://blog.csdn.net/cigogo/article/details/4136590
相关Windows API参见http://msdn.microsoft.com/en-us/library/aa379295(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa379180%28v=VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa375202(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/aa376868(v=VS.85).aspx
下面是代码:
#include <windows.h>
#pragma comment(lib, "Advapi32")
#pragma comment(lib, "User32")
BOOL PreProcess()
{
// 提升进程权限
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// 打开当前进程令牌
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) == 0)
{
return FALSE;
}
// 查询SE_SHUTDOWN_NAME权限ID
if(LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid) == 0)
{
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// 提升至SE_SHUTDOWN_NAME权限
if(AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0) == 0)
{
return FALSE;
}
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
return TRUE;
}
BOOL ShutDown()
{
if(ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0) == 0)
return FALSE;
return TRUE;
}
int main()
{
if(ShutDown() == FALSE)
exit(-1);
return 0;
}