+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get executable/exe name from window handle/name?

  1. #1
    Senior Member
    Join Date
    Feb 2012
    Posts
    109

    How to get executable/exe name from window handle/name?

    I want to get executable name from given handle/name.

    I wan to do this using WIN32 APIs.

    Please help.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    24
    Following code can be useful to you.

    Code:
    CString GetProcessName(DWORD dwProcessID)
    {
    	CString			strProcessName ;
    	HANDLE			hProcessSnapshot ;
    	PROCESSENTRY32	processEntry32 ;
    	
    	hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ;
    	if(hProcessSnapshot != INVALID_HANDLE_VALUE)
    	{
    		processEntry32.dwSize = sizeof(PROCESSENTRY32) ;
    		if(Process32First(hProcessSnapshot, &processEntry32))
    		{
    			do
    			{
    				if (dwProcessID == processEntry32.th32ProcessID)
    				{
    					strProcessName = processEntry32.szExeFile ;
    				}
    			}
    			while(strProcessName.IsEmpty() && Process32Next(hProcessSnapshot, &processEntry32)) ;
    			
    		}
                    CloseHandle(hProcessSnapshot) ;
    	}
    
    	return strProcessName;
    }
    
    CString GetExecutableNameFromWindowName(CString strWindowName)
    {
    	CString strExecutableName ;
    	
    	HWND hWnd = FindWindow(NULL, strWindowName.GetBuffer(0)) ;
    	if (hWnd != NULL)
    	{
    		DWORD dwProcessID ;
    		GetWindowThreadProcessId(hWnd, &dwProcessID) ;
    		strExecutableName = GetProcessName(dwProcessID) ;
    	}
    
    	return strExecutableName ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts