Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get process id from window handle?

Posted on 2002-07-18
9
Medium Priority
?
3,007 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 400 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

688 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