Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Find Which DLL / EXE Created a Window

0.00/5 (No votes)
8 Apr 2009 1  
How to find which DLL / EXE created a Window

The GetWindowModuleFileName() functions can be used to find which EXE or DLL has created a window. But the problem with this function is that it will not work across processes.

Whenever we create a window, we have to pass an HINSTANCE into it. Later, we can use GetWindowLong to get that HINSTANCE. Actually the HINSTANCE is nothing but the HMODULE itself. So if we get the hinstance of a window, we can pass this handle to the GetModuleFileNameEx() and simply get the name of the window from other processes as well.

C++
CString MyGetWindowModuleFileName( HANDLE hwindowhandle )
{
    CString csModuleName;
    DWORD dwProcessId;
    GetWindowThreadProcessId( hwindowhandle, &dwProcessId );
    HINSTANCEhModule = (HINSTANCE)GetWindowLong( hwindowhandle, GWL_HINSTANCE );
    if(hModule == NULL)
    {
        return csModuleName;
    }
    HANDLE hProcess = OpenProcess(PROCESS_VM_READPROCESS_QUERY_INFORMATION,
        FALSE, dwProcessId );
    if( hProcess == NULL )
    {
        return csModuleName;
    }
    BOOL bReturn = GetModuleFileNameEx( hProcess, hModule,
        csModuleName.GetBuffer( MAX_PATH), MAX_PATH );
    csModuleName.ReleaseBuffer();
    CloseHandle(hProcess);
    return csModuleName;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here