有时需要使用OpenProcess来查看进程的相关信息,或者结束进程的运行,但是会碰到“拒绝访问”的错误,这时需要提升进程的权限为Debug权限。
BOOL EnableDebugPrivilege()
{
HANDLE hToken;
BOOL fOk=FALSE;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount=1;
LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid);
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL);
fOk=(GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
}
return fOk;
}
以上代码提升权限 OpenProcess让 可以访问!!!
//读取mir3进程得到人物当前坐标
int GetMir3Position () {
DWORD dwPid = 0;
GetWindowThreadProcessId(hwnd_mir3, &dwPid);
HANDLE hProcess;
EnableDebugPrivilege();
hProcess=OpenProcess( PROCESS_ALL_ACCESS| PROCESS_VM_READ, FALSE, dwPid);
MsgLastError();
if(hProcess==NULL) MessageBox(NULL,"hProcess==NULL","GetMir3Position!",MB_ICONEXCLAMATION|MB_OK);
byte buff[8];
int num;
if(ReadProcessMemory(hProcess,0x027141C0,buff,8,NULL)) {
MessageBox(NULL,"","ReadProcessMemory!",MB_ICONEXCLAMATION|MB_OK);
return Point2int( *((int *)buff ), *((int *)(buff+4)) );
}
Beep(8000,500);
return 0;
}