Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need To Get hwnd of another apps window

Need To Get hwnd of another apps window

Need To Get hwnd of another apps window

I am trying to create a vb app the returns the hwnd of what ever the current application outside of this app is that is running.  And have it return the windows title/caption if available.

I believe the API calls I need to made are GetActiveWindow and GetWindowText but am not sure of how to impliment these calls to obtain the results I need.

Any assistance would be greatly appriciated.


RE: Need To Get hwnd of another apps window

I would approach it something like :

#define MAX_BUF_LEN 200
HWND myhwnd = GetForegroundWindow();
CHAR titlebuf[MAX_BUF_LEN];


I think that GetWindowText() will fail if you are
trying to retrieve from a window which has a seperate message handler.
Using SendMessage directly should circumvent that.
It does mean, however, that your calling thread will be blocked until you get a reply from the other thread - you might consider using SendMessageTimeout() instead.

Similarly with GetActiveWindow() which is tied to the current message thread.
GetForegroundWindow() returns a handle to the window that the user is currently working with independant of thread.

I'm not 100% about this but I think it's along the right lines....

RE: Need To Get hwnd of another apps window

Hi Chris,
this is a only a solution to an already-running app, tho it may help getcha going:
External_Function IsIconic "IsIconic" User32.dll  Handle hWnd Returns Integer
 // Check to see if it's running already.

Procedure DoAutoSwitch String sCaption
    Local Handle hWnd
    Local Integer iVoid iIcon
    Move (FindWindow("DFFrameClass", sCaption)) To hWnd
    If hWnd Begin
        Move (IsIconic(hWnd)) To iIcon
        If (iIcon)  Move (ShowWindow(hWnd,SW_RESTORE))    to iVoid
        Move (SetForeGroundWindow(hWnd)) To iVoid

Send DoAutoSwitch "the text in the window"

HTH, Baz.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close