Click here to Skip to main content
15,949,741 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: How to limit cin to string? Pin
David Crow12-Nov-08 3:11
David Crow12-Nov-08 3:11 
QuestionImport file content Pin
john563211-Nov-08 19:11
john563211-Nov-08 19:11 
AnswerRe: Import file content Pin
john563211-Nov-08 23:26
john563211-Nov-08 23:26 
AnswerRe: Import file content Pin
42ka112-Nov-08 2:13
42ka112-Nov-08 2:13 
AnswerRe: Import file content Pin
David Crow12-Nov-08 3:14
David Crow12-Nov-08 3:14 
QuestionFile Path from handle Pin
revanth198511-Nov-08 18:48
revanth198511-Nov-08 18:48 
AnswerRe: File Path from handle Pin
ThatsAlok11-Nov-08 20:57
ThatsAlok11-Nov-08 20:57 
AnswerRe: File Path from handle Pin
Randor 11-Nov-08 22:47
professional Randor 11-Nov-08 22:47 
If you are asking how to get the filename of an executable by knowing a window handle then this is how you do it. This code will tell you the PID of the executable even if you pass a child handle such as a button handle.

#pragma comment(lib, "psapi")
DWORD dwPID = -1;

BOOL CALLBACK EnumChildWindowsProc(HWND hWnd, LPARAM lParam)
{
	if(hWnd == (HWND)lParam)
	{
		GetWindowThreadProcessId(hWnd,&dwPID);
	}
	return TRUE;
}


BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
	if(hWnd == (HWND)lParam)
	{
		GetWindowThreadProcessId(hWnd,&dwPID);
	}
	else
	{
		EnumChildWindows(hWnd,EnumChildWindowsProc,lParam);
	}
	return TRUE;
}


BOOL FindWindowProcessModule(HWND Hwnd, TCHAR *szName,size_t iMaxLen)
{
	EnumWindows(EnumWindowsProc,(LPARAM)Hwnd);
	if(-1 != dwPID)
	{
		HMODULE hModule = NULL;
		DWORD dwNeeded = NULL;
		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,dwPID);
		if (NULL != hProcess)
		{
			if(EnumProcessModules(hProcess,&hModule,sizeof(hModule),&dwNeeded))
			{
				GetModuleFileNameEx(hProcess,hModule,szName,iMaxLen);
			}
			else
			{
				GetProcessImageFileName(hProcess,szName,iMaxLen);
			}
			CloseHandle(hProcess);
		}
	}
	return NULL != szName[0];
}


Here is how you would use it:

TCHAR szName[MAX_PATH] = {0};
HWND Hwnd = (HWND)::FindWindow(_T("SciCalc"),_T("Calculator"));

if(FindWindowProcessModule(Hwnd,szName,MAX_PATH))
{
	MessageBox(szName);
}
else
{
	MessageBox(_T("Calculator is not running."));
}


Best Wishes,
-David Delaune
GeneralRe: File Path from handle Pin
revanth198511-Nov-08 23:17
revanth198511-Nov-08 23:17 
GeneralRe: File Path from handle Pin
Randor 11-Nov-08 23:31
professional Randor 11-Nov-08 23:31 
GeneralRe: File Path from handle Pin
revanth198511-Nov-08 23:43
revanth198511-Nov-08 23:43 
GeneralRe: File Path from handle Pin
SRKSHOME12-Nov-08 0:47
SRKSHOME12-Nov-08 0:47 
QuestionRe: File Path from handle Pin
David Crow12-Nov-08 3:15
David Crow12-Nov-08 3:15 
GeneralRe: File Path from handle Pin
Randor 12-Nov-08 1:21
professional Randor 12-Nov-08 1:21 
QuestionCPU stimulation C++ Pin
124211-Nov-08 18:46
124211-Nov-08 18:46 
AnswerRe: CPU stimulation C++ Pin
David Crow12-Nov-08 3:35
David Crow12-Nov-08 3:35 
GeneralRe: CPU stimulation C++ Pin
124212-Nov-08 6:45
124212-Nov-08 6:45 
GeneralRe: CPU stimulation C++ Pin
124212-Nov-08 6:54
124212-Nov-08 6:54 
QuestionProblem with IDispatch::GetTypeInfo Pin
mvkvibin11-Nov-08 17:44
mvkvibin11-Nov-08 17:44 
QuestionConvert doc/txt file to PDF using C++. Pin
Subrat 470826611-Nov-08 17:26
Subrat 470826611-Nov-08 17:26 
AnswerRe: Convert doc/txt file to PDF using C++. Pin
bob1697211-Nov-08 18:32
bob1697211-Nov-08 18:32 
GeneralRe: Convert doc/txt file to PDF using C++. Pin
Saurabh.Garg11-Nov-08 22:41
Saurabh.Garg11-Nov-08 22:41 
GeneralRe: Convert doc/txt file to PDF using C++. Pin
bob1697212-Nov-08 3:18
bob1697212-Nov-08 3:18 
AnswerRe: Convert doc/txt file to PDF using C++. Pin
Cedric Moonen11-Nov-08 20:32
Cedric Moonen11-Nov-08 20:32 
QuestionHow to pass data from MFC to Win 32 Console through windows messaging [modified] Pin
Darrel Q Pham11-Nov-08 15:45
Darrel Q Pham11-Nov-08 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.