CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2007
    Posts
    274

    Get hwnd of a control in another application.

    I've done this a couple years ago, but I certainly dont remember what API's I used. Anyone know off had on how to do it?

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get hwnd of a control in another application.

    FindWindow and FindWindowEx APIs should help you here

  3. #3
    Join Date
    Mar 2007
    Posts
    274

    Re: Get hwnd of a control in another application.

    I think FindWindowEx will do it, any ideas on the parameters? Here's what I got so far:

    FindWindowEx(GetForegroundWindow(), null, null, nulll);

    and its not much. if spy++ can do it so can I, I'm pretty darn sure I've done this before, in fact I know I have because I wrote a macro program that would record keyboard and mouse events, and play them back. I would have had to of got the control hwnd to have used sendmessage correctly..... Problem is it was in vb6, and on another harddrive that I have stored away right now.... gurrrrr.....


    Well, now I'm thinking maybe I created a global mouse and keyboard hook, and just threw in the recorded key and mouse events into the message queue ... I dunno, Hopefully someone will shed some light, in the meantime, when I get a day off from work, I'll dig up my harddrive, and try to find my original code.
    Last edited by Traps; February 13th, 2009 at 05:53 AM.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get hwnd of a control in another application.

    You'll need both FindWindow and FindWindowEx.
    FindWindow finds the external application's main window, and FindwindowEx finds a window ( a control ) inside the external application's main window.

    Here's a couple examples on FindWindow :

    http://www.devasp.net/net/articles/display/690.html
    http://www.dotnetspider.com/resource...indow-API.aspx

    Here's a VB.NET example of FindWindowEx :

    http://www.codeguru.com/forum/showthread.php?t=444234

    Should be quite simple to convert to C# if you know a bit of VB. Just in case, here's a nice VB.NET to C# converter :

    http://www.developerfusion.com/tools.../csharp-to-vb/

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    Re: Get hwnd of a control in another application.

    Hey, thanks I'll definately check out those links. Funny you mention vb, because I am writing this in vb.

    Remember I'm trying to get the hwnd for any control on any application, I'm not going to know their names.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get hwnd of a control in another application.

    You may also need to use a program called Spy++ ( It should be on your system, listed under Visual Studio .NET Tools ). This program will show all the current Windows and Child Windows for any open application; so it should be relatively easy to obtain the name of the particular control(s) in question.

    If you're writing this app in VB, you must please ask in the appropriate VB forum

  7. #7
    Join Date
    Mar 2007
    Posts
    274

    Re: Get hwnd of a control in another application.

    Quote Originally Posted by HanneSThEGreaT View Post
    You may also need to use a program called Spy++ ( It should be on your system, listed under Visual Studio .NET Tools ). This program will show all the current Windows and Child Windows for any open application; so it should be relatively easy to obtain the name of the particular control(s) in question.

    If you're writing this app in VB, you must please ask in the appropriate VB forum
    Thanks, but I dont need to ask in vb forum, as vb=c#, and any source code I provide you will be in c#. As far as using SPy++ thats impossible, I dont know what windows the enduser will be sending messages to. Like I indicated in my previous post, I need to get the handle at runtime for controls in any application in any window.

    C# solutions are fine and expected since I'm in a c# forum.

    Thanks.
    Last edited by Traps; February 13th, 2009 at 03:23 PM.

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