OpenProcess总是返回NULL - C++ Builder / Windows SDK/API
在VS2008下面,
DWORD pID=GetProcessID("QQ.exe");
HANDLE hProcess=::OpenProcess(PROCESS_ALL_ACCESS, TRUE, pID);
if (NULL == hProcess)
{
cout<<"Error :"<<GetLastError()<<endl;
}
TerminateProcess(hProcess, 0);
结果发现,Error 的值是5。根本执行不到TerminateProcess这个语句,,,这是怎么回事??
[解决办法]
/******************************************************\功能描述:调整进程的权限参数说明:1.PrivilegeName:要调整权限的名字。\******************************************************/bool WINAPI EnablePrivilege(PCHAR PrivilegeName){ HANDLE hProc,hToken; TOKEN_PRIVILEGES TP; //获得进程句柄 hProc=GetCurrentProcess(); //打开进程令牌环 if(!OpenProcessToken(hProc,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) { return false; } //获得进程本地唯一ID if(!LookupPrivilegeValue(NULL,PrivilegeName,&TP.Privileges[0].Luid)) { CloseHandle(hToken); return false; } TP.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; TP.PrivilegeCount=1; //调整权限 if(!AdjustTokenPrivileges(hToken,false,&TP,sizeof(TP),0,0)) { CloseHandle(hToken); return false; } CloseHandle(hToken); return true;}//---------------------------------------
[解决办法]
给你的程序提升一下档次
void AdjustPrivilege()
{
HANDLE hToken;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
{
MessageBox(NULL,TEXT("NO"),TEXT(""),0);
}
LUID myLUID;
LookupPrivilegeValue(NULL,SE_DEBUG_NAME, &myLUID);
TOKEN_PRIVILEGES tp={sizeof(tp)};
tp.PrivilegeCount=1;
tp.Privileges[0].Luid=myLUID;
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
{
/*MessageBox(NULL,TEXT("权限提升成功"),TEXT(""),0);*/
}
}