QueryFullProcessImageName works while debugging and not in runtime

This function should print to the console the path of the desired process by its PID, but it only works fine while I am debugging. When the application is compiled and the resulting binary is run, it does not print the path of the desired process if it is an elevated process.
I am using VS2019 and I am just writing code in the console project. So, standard configuration. I am new in c++

Code here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void GetProcess(DWORD dwPID)
{
	HANDLE hProcess = NULL;
	TCHAR fn[MAX_PATH];
	DWORD size = MAX_PATH;

	hProcess = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, dwPID);
	cout << hex << hProcess << endl;
	if (hProcess)
	{

		if (::QueryFullProcessImageName(hProcess, 0, fn, &size))
		{
			_tprintf(TEXT(" => %s"), fn);
			::CloseHandle(hProcess);
		}
	}
}
Last edited on
> but it only works fine while I am debugging.
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
If the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.

Your process would seem to get this for free by virtue of being debugged.

On another note, each of your if statements needs an else to print the error information.
At least then it would tell you why it doesn't work.
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryfullprocessimagenamea
Thanks a lot for the north
Registered users can post here. Sign in or register to post.