Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Why can`t I get the path of the process(retrieved by GetWindowThreadProcessId+OpenProcess) which create the desktop window (retrieved by GetDesktopWindow) by GetModuleFileNameEx?
#include <stdio.h>
#include <windows.h>
#pragma comment (lib, "user32.lib")
#include <Psapi.h>
#pragma comment(lib,"Kernel32.lib")

main(){
	FILE *fp = fopen("C:/windowLog.txt", "a");
	HWND desktopWindow=GetDesktopWindow();
	fprintf(fp, "Handle of Desktop window is%x\n", (unsigned int)desktopWindow);
	CHAR dName[MAX_PATH] = { 0 };
	GetWindowTextA(desktopWindow, dName, MAX_PATH);
	fprintf(fp, "Title of desktop window is %s\n", dName);
	GetClassName(desktopWindow, dName, MAX_PATH);
	fprintf(fp, "Desktop window class is %s\n", dName);
	DWORD pid = 0;
	GetWindowThreadProcessId(desktopWindow, &pid);
	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
	CHAR path[260] = { 0 };
	GetModuleFileNameEx(hProcess, NULL, path, 260);
	fprintf(fp, "The path of the process is %s\n\n", path);
	fclose(fp);

I can't get the path from GetModuleFileNameEx.

What I have tried:

GetDesktopWindow + GetWindowThreadProcessId + OpenProcess + GetModuleFileNameEx
Posted
Updated 2-Apr-18 23:37pm
v4
Comments
Richard MacCutchan 3-Apr-18 4:11am    
Please show your code, and explain exactly what is going wrong. Do you have the correct permissions set in your application?
quanhuang 3-Apr-18 4:51am    
Thank you very much.Here is my code.Please have a look.
Richard MacCutchan 3-Apr-18 5:06am    
Running this through the debugger, GetWindowThreadProcessId returns 0. So it would appear that you cannot obtain that information. This may be a Windows restriction.
quanhuang 3-Apr-18 11:44am    
Thank you very much!

1 solution

Probably, it's a security restriction: the desktop is created by a system process as part of the logon procedure but before it starts the user session and it's processes, and is thus created by a process in a different session. Or at least, that's the way I read it: Window Station and Desktop Creation (Windows)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900