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

    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:
    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.
    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 online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    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
    Iasi - Romania

    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.
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.