环境: vc6.0, win2000
我想再程序中实现自动关机功能,代码如下:
if ( OpenProcessToken(::AfxGetThread(), TOKEN_ADJUST_PRIVILEGES, &handle) != 0 )
{
AfxMessageBox("get privilege right");
}
else
{
AfxMessageBox("get privilege error");
}
TOKEN_PRIVILEGES tp;
LUID luid; if ( !LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid ) ) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError() );
return FALSE;
} tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0; // Enable the privilege or disable all privileges. AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL);
// Call GetLastError to determine whether the function succeeded. if (GetLastError() != ERROR_SUCCESS)
{ printf("AdjustTokenPrivileges failed: %u\n", GetLastError() );
return FALSE;
}为什末每次openProcessToken()返回0呢,请帮忙瞧瞧.
我想再程序中实现自动关机功能,代码如下:
if ( OpenProcessToken(::AfxGetThread(), TOKEN_ADJUST_PRIVILEGES, &handle) != 0 )
{
AfxMessageBox("get privilege right");
}
else
{
AfxMessageBox("get privilege error");
}
TOKEN_PRIVILEGES tp;
LUID luid; if ( !LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid ) ) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError() );
return FALSE;
} tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0; // Enable the privilege or disable all privileges. AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL);
// Call GetLastError to determine whether the function succeeded. if (GetLastError() != ERROR_SUCCESS)
{ printf("AdjustTokenPrivileges failed: %u\n", GetLastError() );
return FALSE;
}为什末每次openProcessToken()返回0呢,请帮忙瞧瞧.
OpenProcessToken(::AfxGetThread()->m_hThread, TOKEN_ADJUST_PRIVILEGES, &handle)
还是不行?
大家帮忙想想怎末作。