Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get process id from window handle?

Posted on 2002-07-18
9
Medium Priority
?
3,016 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
6 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

782 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