Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C++

Find Which DLL / EXE Created a Window

Rate me:
Please Sign up or sign in to vote.
3.73/5 (5 votes)
8 Apr 2009CPOL 18.3K   17   4
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;
}
This article was originally posted at http://0memory.blogspot.com/feeds/posts/default?alt=rss

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you Pin
Pepsibot26-Jan-10 7:36
Pepsibot26-Jan-10 7:36 
GeneralRe: Thank you Pin
Pepsibot26-Jan-10 7:39
Pepsibot26-Jan-10 7:39 
GeneralMy vote of 1 Pin
Blkbam10-Apr-09 5:32
Blkbam10-Apr-09 5:32 
GeneralRe: My vote of 1 Pin
only_jack26-Apr-09 9:22
only_jack26-Apr-09 9:22 

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.