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.


Hope my doubt made sence to you.
Who is Participating?
jkrConnect With a Mentor Commented:
Use 'GetWindowThreadProcessId()' (

You can find a sample in;EN-US;q183009 ("HOWTO: Enumerate Windows Using the WIN32 API (Q183009)"), but it's pretty straightfoward:

HWND hWnd = ...;


GetWindowThreadProcessId ( hWnd, &dwPID);
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.
ibferozAuthor Commented:
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];

error C2065: 'GetWindowModuleFileName' : undeclared identifier.

Hope you can relate now.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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];
The Q is answered
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.