Home > Software engineering >  Excuse me the OpenProcess function returns a handle to the same every time is why
Excuse me the OpenProcess function returns a handle to the same every time is why

Time:11-15

Promoted to debug privilege, first of all, I will program, and then call OpenProcess function but always returns 0 x00000048 this handle, could you tell me where is wrong operation,

The following code function is promoted to debug the program himself first privileges, then open the other programs, it is also set to debug privilege, have never thought the OpenProcess function wrong
#include
#include
using namespace std;
BOOL EnableDebugPrivilege (BOOL bEnable, HANDLE hProcess);
UINT g_dwProcessId;
HANDLE g_hProcess;
DWORD g_ErrorId;
Int main ()
{
If (EnableDebugPrivilege (TRUE, : : GetCurrentProcess ()))
While (1)
{
Cout & lt; <"Please input the process ID:";
Cin & gt;> G_dwProcessId;
Cin. The get ();
G_hProcess=: : OpenProcess (PROCESS_ALL_ACCESS, FALSE, g_dwProcessId);
Cout & lt; <"Process handle:" & lt; If (g_hProcess==NULL)
{
Cout & lt; <"Open the process failure, may process does not exist \ n";
continue;
}
If (EnableDebugPrivilege (TRUE, g_hProcess))
Cout & lt; <"The process of debugging privilege has been open! Enter a '#' will shut down the process of debugging privilege \ n ";
The else
{
Cout & lt; <"Open the debug privileges failure \ n";
continue;
}

char ch;
Ch=cin. The get ();
If (ch=='#')
EnableDebugPrivilege (FALSE, g_hProcess);

G_dwProcessId=0;
G_hProcess=0;
}
The else
Cout & lt; <"Failed to start the program debugging privilege";
return 0;
}

BOOL EnableDebugPrivilege (BOOL bEnable, HANDLE hProcess)
{
BOOL bOk=FALSE;
HANDLE hToken;
If (: : OpenProcessToken (hProcess, TOKEN_ADJUST_PRIVILEGES, & amp; HToken))
{
LUID uID;
: : LookupPrivilegeValue (NULL, SE_DEBUG_NAME, & amp; UID);
The TOKEN_PRIVILEGES tp;
Tp. PrivilegeCount=1;
Tp. Privileges [0]. Luid=uID;
Tp. Privileges [0]. Attributes=bEnable? SE_PRIVILEGE_ENABLED: 0;
: : AdjustTokenPrivileges (hToken, FALSE, & amp; Tp, sizeof (tp), NULL, NULL);
BOk=(: : GetLastError ()==ERROR_SUCCESS);
: : CloseHandle (hToken);
}
The else
{
G_ErrorId=: : GetLastError ();
Cout & lt; <"What went wrong id:" & lt; }
Return bOk.
}

CodePudding user response:

Added the following process ID I am using the task manager to look PID, PID of figure g_dwProcessId for notebooks
  • Related