Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need To Get hwnd of another apps window

Status
Not open for further replies.

FireFett

Programmer
Mar 31, 2000
42
0
0
US
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.

Thanks
Chris
CRFetterolf@Coolblue.com
 
I would approach it something like :

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

SendMessage(myhwnd,WM_GETTEXT,(WPARAM)MAX_BUF_LEN,(LPARAM)titlebuf);

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....
 
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
Abort
End
End_Procedure

Send DoAutoSwitch "the text in the window"

HTH, Baz.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top