Psapi QueryFullProcessImageName fails

Hi everyone,
In exploring the workings of the process API, I am attempting to write a simple program that would display the PIDs and filepaths of every running process on my computer.

This is done by calling the QueryFullProcessImageName function, however, it fails every time even though it seems as though I have given it the appropriate security permissions in the handle, etc.

The full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Windows.h>
#include <iostream>
#include <Psapi.h>

using namespace std;


void printProcPath(DWORD pid) {

	HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_QUERY_INFORMATION, false, pid);//create process handle

	if (hProc = NULL) cout << "Cannot open process."; // if cannot open process, return error description

	LPWSTR path = new WCHAR[MAX_PATH];//will hold .exe path to be returned
	DWORD charsCarried = MAX_PATH; // holds size of path[], will then hold amount of characters returned by QueryFullProcessImageName
	BOOL iResult = QueryFullProcessImageName(hProc, 0, path, &charsCarried);

	if (iResult == 0) { //if QueryFullProcessImageName fails, return description
		cout << "Cannot get process path."; 
		}
	else { //if success, print path
		wcout << path;
	}
}

int main() {

	DWORD PIDs[1024], bytesReturned, procQty;

	EnumProcesses(PIDs, sizeof(PIDs), &bytesReturned);//gets PIDs and stores it into array
	procQty = bytesReturned / sizeof(DWORD);

	for (DWORD i = 0; i < procQty; ++i) { // for each PID, print result in format <PID>: {<process_name> | <error_description>}

		cout << PIDs[i] << ": ";
		printProcPath(PIDs[i]); 
		cout << '\n';														 
	}

	cin.get();//wait for enter press before exiting
	return 0;
}


Upon running, in my console window I get:


0: Cannot get process path.
4: Cannot get process path.
344: Cannot get process path.


...and so on until all PIDs have been enumerated.

Am I doing something wrong, not meeting some prerequisite, etc?
Any assistance would be appreciated.
Thank you.

Call GetLastError() imediately after to see what is the cause.

Also did the call to Openprocess() succeeded ? Check that also. Pass either PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION, not both.
OpenProcess did succeed, otherwise the appropriate message would have been output by this line 12.

I have also tried editing my code to pass only PROCESS_QUERY_INFORMATION, and edited again to pass only PROCESS_QUERY_INFORMATION. Neither way worked.

Thank you, however, for the suggestion regarding GetLastError(), as it may lead me to a solution.
Line 12 has an assignment operator in the IF(). This is effectively destroying any good returned handle from OpenProcess().

Beginners should get used to do comparisons backwards: First the value, then the variable. This way if they forget the second Equals sign the compiler will complain:

if (NULL == hProc) //2 Equals signs, not just one.
wow... my apologies.

I must have mistyped the comparison operator, and it is hard to notice.
Thank you so much!
Topic archived. No new replies allowed.