Easiest what to get a windows handle with just the filename?

This is a discussion on Easiest what to get a windows handle with just the filename? within the Windows Programming forums, part of the Platform Specific Boards category; is there an easy way to get the main windows handle(it only uses one), by just knowing the programs filename?...

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267

    Easiest what to get a windows handle with just the filename?

    is there an easy way to get the main windows handle(it only uses one), by just knowing the programs filename?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    50
    Hmmm... how does the program know its own filename?

    Did you know that any windows process has a handle to itself? It's easy enough to get that:

    Code:
    char szFileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);
    
                GetModuleFileName(hInstance, szFileName, MAX_PATH);
                MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
    You don't even need to know the filename.

    This code here actually finds the program pathway too but it does it by first getting the handle, i.e.

    HINSTANCE hInstance = GetModuleHandle(NULL);

    Tell me how that goes.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    thanks fordy, looks good.

    i'll try it soon.

    the only real change think would help

    is using strstr() to find the filename without the path since that can change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM
  4. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  5. child windows
    By face_master in forum Windows Programming
    Replies: 14
    Last Post: 03-01-2002, 06:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21