Solved

How to get process id from window handle?

Posted on 2002-07-18
9
2,975 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:ibferoz
9 Comments
 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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.
0
 

Author Comment

by:ibferoz
Comment Utility
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
Use 'GetWindowThreadProcessId()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/getwindowthreadprocessid.asp)

You can find a sample in http://support.microsoft.com/default.aspx?scid=kb;EN-US;q183009 ("HOWTO: Enumerate Windows Using the WIN32 API (Q183009)"), but it's pretty straightfoward:

HWND hWnd = ...;

DWORD dwPID;

GetWindowThreadProcessId ( hWnd, &dwPID);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:BeyondWu
Comment Utility
you should include
#include <WINABLE.H>

TCHAR fileBuff[256];
GetWindowModuleFileName(pMsg->hwnd,fileBuff,256);
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
The Q is answered
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now