Weird request...!!!
I had a few tries at this, and it appears there would be more than 1 way to do this.....
GetWindowModuleFileName() looked like a winner, but for some reason I couldnt get it to do what I wanted.....Also I realised my old version of the SDK is starting to become a pain in the arse......but that's another story....
Anyway...not perfect by a long shot, but it seems to produce something....
Code:
#include <windows.h>
#include <iostream>
#include <cstring>
using namespace std;
typedef DWORD (WINAPI *GMFNE)(HANDLE,HMODULE,LPSTR,DWORD);
/*My PSAPI lib seems to be faulty......so I had to
link to GetModuleFileNameEx at runtime*/
char exefile[] = "C:\\WINNT\\System32\\calc.exe";
//The exe of your choice....
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam){
GMFNE lpGMFNE = (GMFNE)lParam;
/*Entry point to the GetModuleFileNameEx func*/
DWORD dwPID,//Process ID
dwBytes;//Bytes returned
HANDLE hProc;//Process Handle
HMODULE hMod;//Module Handle
char buff[MAX_PATH];//Buffer
hMod = (HMODULE)GetWindowLong(hwnd,GWL_HINSTANCE);
//Get the module for the window
GetWindowThreadProcessId(hwnd,&dwPID);
//Also the Process ID
hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwPID);
//Now use that ID to get Process handle
if(hProc && hMod){
dwBytes = lpGMFNE(hProc,hMod,buff,MAX_PATH);
//GetModuleFileNameEx
if(dwBytes){
buff[dwBytes] = NULL;
if(!strcmp(exefile,buff))//Is it from calc.exe?
cout << "Found HWND - " << hwnd << endl;
}
CloseHandle(hProc);//Close
}
return TRUE;
}
int main(void){
HMODULE hMod;
GMFNE lpGMFNE;
hMod = LoadLibrary("PSAPI.dll");
if(!hMod){
cout << "PSAPI.dll not present";
return 1;
}
lpGMFNE = (GMFNE)GetProcAddress(hMod,"GetModuleFileNameExA");
if(!lpGMFNE){
cout << "Could not locate procedure";
FreeLibrary(hMod);
return 1;
}
EnumWindows((WNDENUMPROC)EnumWindowsProc,(LPARAM)lpGMFNE);
FreeLibrary(hMod);
return 0;
}
This uses the PSAPI.dll which is an addon to WINNT (oh...you need NT at least for this prog as it deals with processes and stuff - there's a shock! )...this dll is usually in the system32 dir as loads of apps ship it & install it.....
The code is messy, and there is probably a better way, but it might do with some tidying up.