Solved

Getting hWnd from titlebar

Posted on 1997-07-24
6
454 Views
Last Modified: 2012-05-04
If I know the title of a program ie. "MS-WORD", is it possible to get the hWnd (window handle) of that application (form[s])?
0
Comment
Question by:PersonX070197
6 Comments
 
LVL 3

Expert Comment

by:ESI
ID: 1428826
Yes its possible to retrieve the hWnd just by the caption (or a part of the caption) displayed in the main form.
Is it what u need before I dig in my API docs ?
0
 
LVL 2

Expert Comment

by:vorlon
ID: 1428827
PersonX,
Yes. In order to get the hWnd of the application knowing only it's titlebar, you have to use the FindWindow API function.

Here is some information of that function:
Declare Function FindWindow& Lib "user32" alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName as string)

Description:
Finds the first top level window in the window list that meets the specified conditions.

Return Value:
Long - Handle of the window found. Zero if no window was found. Sets GetLastError

Use in VB:
No problem. One common use of FindWindow is to obtain the handle of the hidden window of class ThunderRTMain that is part of every running Visual Basic executable. You can then use the GetWindowText API to retrieve the window name of this window which is also the appliation title.

Parameters:
lpClassName - String: Pointer to null terminated (C Language) string containing the name of the class of the window, or zero to  accept any class.

lpWindowName - String: Pointer to null terminated (C Language) string containing the window text (or title), or zero to accept any window title.

Hope this helps.
0
 
LVL 3

Expert Comment

by:ESI
ID: 1428828
What is this ThunderRTMain hidden window ? What's the use to get a handle on it ? I've seen too there is a "CONJURE.MAK" hidden window when VB5 is running.
Any infos welcomed !
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:vorlon
ID: 1428829
I have no idea what the CONJURE.MAK hidden window or the ThunderRTMain class do. I simply copied the description of the function from my reference. I've used this function before manny times and it does what it's support to do.

Have you ever noticed that sometimes you can't run multiple VB applications simultaneously? Well maybe these windows/class names have something to do with that.

Just my $0.02.
vorlon
0
 

Author Comment

by:PersonX070197
ID: 1428830
I'm sure that the API you gave me works, but it doesn't seem to work for me... You see, let's say I want to terminate "Notepad" I  would first declare the API (Findwindow and Destroywindow), Find the titlebar of notepad, possibly "Untitled - Notepad" then my code:
x=FindWindow& (0,"Untitled - Notepad"")
x=DestroyWindow (x)
....It should work according to the code you pasted above,  (If I replace the values with all 0's it should work for sure right?
[FindWindow& (0,0) - should return ANY window, well it doesnt)
So am I messing something up here or is it mess'd or what?
0
 
LVL 3

Accepted Solution

by:
wolfcrag earned 100 total points
ID: 1428831
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long

Is this the declaration you used? If not, try it out. If it is,  try String instead of Any in both parameters, and then use the following code:

hwndWin = FindWindow(vbNullString, "Untitled - Notepad")
Throw = DestroyWindow(hwndWin)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now