CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    getting hwnd after createprocess

    This seems to be a common problem. I think I've found a good way of doing it and wanted to share to see if anyone notices something wrong.

    First create a WinEventHook:
    Code:
    SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
    This will capture alot of things, but what you're interested is when the window associated with your process is created. So in WinEventProc, you can filter out what you don't need. In this example I'm using the classname, but you could use whatever identifier you want.
    Code:
    void CALLBACK WinEventProc (HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
    {
        if (event == EVENT_OBJECT_CREATE)
        {
            WCHAR classname[1024];
            GetClassName(hwnd, classname, 1024);
    
            if (wcscmp(classname, AppClassName) == 0)
            {
                // you found it!  hwnd is the pointer you wanted
            }
        }
    }
    In the above code AppClassName is a LPCWSTR that has the classname of the process (i.e. IEFrame for Internet Explorer).

    This has worked well so far and makes it so you don't have to figure out how long to wait before calling EnumWindows or FindWindow. In fact, you don't have to use EnumWindows or FindWindow at all!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: getting hwnd after createprocess

    Using a class name as a filter is not a good idea, because there may be created a lot of windows with the same class name.
    Better would be to check process handle or/and process id of the process this window belongs to.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: getting hwnd after createprocess

    Quote Originally Posted by Daktor View Post
    [...]have to figure out how long to wait before[...]
    No sweat, by calling WaitForInputIdle function.
    Then, as Victor already suggested, use the main process thread ID (returned in PROCESS_INFORMATION structure) to find the main window.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured