Solved

How to get process id from window handle?

Posted on 2002-07-18
9
2,998 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 7161824
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
ID: 7161834
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
ID: 7161947
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 8

Expert Comment

by:fl0yd
ID: 7161949
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
ID: 7163735
you should include
#include <WINABLE.H>

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

Expert Comment

by:jkr
ID: 10668254
The Q is answered
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

631 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