Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:41:13+00:00 2026-05-22T22:41:13+00:00

I have injected my dll into process. How can I get Main window handle

  • 0

I have injected my dll into process.
How can I get Main window handle of host application?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:41:14+00:00Added an answer on May 22, 2026 at 10:41 pm

    The host application may have multiple ‘main windows’. To detect them, you could

    1. Call GetCurrentProcessId to get the PID of the current process
    2. Call EnumWindows to iterate over all toplevel windows of the desktop
    3. For each window on the desktop, call GetWindowThreadProcessId to get the PID of the process which created the window
    4. If the PID of the window matches the PID of your own process, memorize the window.

    That gives you a list of toplevel windows created by the process which you injected your DLL into. However, please note that this approach may yield windows which have been destroyed by the time you process the constructed list of windows. Hence, when doing something with the windows, make sure to use the IsWindow function to ensure that the window at hand is still valid (this is still prone to race conditions since the window may become invalid between your call to IsWindow and actually accessing the window, but the time window is much smaller).

    Here’s a C++ function implementing this algorithm. It implements a getToplevelWindows function which yields a std::vector<HWND> containing the handles of all toplevel windows of the current process.

    struct EnumWindowsCallbackArgs {
        EnumWindowsCallbackArgs( DWORD p ) : pid( p ) { }
        const DWORD pid;
        std::vector<HWND> handles;
    };
    
    static BOOL CALLBACK EnumWindowsCallback( HWND hnd, LPARAM lParam )
    {
        EnumWindowsCallbackArgs *args = (EnumWindowsCallbackArgs *)lParam;
    
        DWORD windowPID;
        (void)::GetWindowThreadProcessId( hnd, &windowPID );
        if ( windowPID == args->pid ) {
            args->handles.push_back( hnd );
        }
    
        return TRUE;
    }
    
    std::vector<HWND> getToplevelWindows()
    {
        EnumWindowsCallbackArgs args( ::GetCurrentProcessId() );
        if ( ::EnumWindows( &EnumWindowsCallback, (LPARAM) &args ) == FALSE ) {
          // XXX Log error here
          return std::vector<HWND>();
        }
        return args.handles;
    }
    

    UPDATE: These days (about four years after I gave the answer) I would also consider traversing the list of threads of the application and then using EnumThreadWindows on each thread. I noticed that this is considerably faster in many cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have injected my DLL into a target application where I've hooked few WINAPI-functions
i have to inject a dll into some process .now i want that after
I have this code injected in my site. How can I decode the trailing
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a n-tire web application and search often times out after 30 secs. How
Have you managed to get Aptana Studio debugging to work? I tried following this,
Before I get into to my question,let me explain what I am exactly doing.I
I would really appreciate your help in this. I have been trying to get
I have a bean in which I've injected an HttpServletRequest using the @Autowired annotation
I have a class, being injected by guice and this class constructor invokes super,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.