Link to home
Start Free TrialLog in
Avatar of ibferoz
ibferoz

asked on

How to get process id from window handle?

I have code like following

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg){
     LPARAM m_winHandle= pMsg->lParam;
...
...

So I have window handle now. From here How can I get process id of the application. Thru which I have to get
application(.EXE's) name.

When I am using the GetWindowModuleFileName as follows for the same purpose its giving me 'undeclared identifier' error.

GetWindowModuleFileName(pMsg->hwnd,fileBuff,256);

Hope my doubt made sence to you.
Avatar of fl0yd
fl0yd

This sounds like a compile-time error which tells you that you are attempting to use an identifier that is unknown to the compiler in this context. Since this is an MFC-application, GetWindowModuleFileName is definately known. pMsg, if it is a MSG*, does have a hwnd member. So if this function call is inside your PreTranslateMessage, I'd say you haven't declared fileBuff, or possibly misspelled it.
Avatar of ibferoz

ASKER

Hi fl0yd ,

Ya its a compile time error and its a MFC application too.
I have checked with all variations. Here I am giving the code and error...


LPTSTR fileBuff[256];
GetWindowModuleFileName(pMsg->hwnd,fileBuff,256);
....

error C2065: 'GetWindowModuleFileName' : undeclared identifier.

Hope you can relate now.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmmmm, strange, I thought MFC #include'd <windows.h> automatically. Try putting this statement

#include <windows.h>

right after the #include "stdafx.h" statement in your implementation file. I'm not sure if you will get compiler errors when doing this.

Somewhat unrelated to your question: you might want to also check out GetModuleFileName( NULL, fileBuff, 256 ) -- which pretty much does the same but is a bit more direct.
you should include
#include <WINABLE.H>

TCHAR fileBuff[256];
GetWindowModuleFileName(pMsg->hwnd,fileBuff,256);
The Q is answered