Getting window handle for a window with UNICODE title?

How to get window handle of a window, knowing its title, but that will work even if the title is a Unicode text?

The obvious FindWindow API
hWnd = FindWindow(vbNullString, "Window Title")
works fine if the title of the window is ASCII, but if it is apparently Unicode, it just returns zero…

Namely, I need to get handle to MS Word window, for which I use
hWnd = FindWindow(vbNullString, WrdDoc.ActiveWindow.Caption & " - " & "Microsoft Word")

which works just fine, unless the Word window Caption contains Unicode characters, which can easily happen if the document is saved to with a file name containing Unicode, which is often; in that case the FindWindow fails to work, and returns zero…
npaunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
You can try using FindWindowW.
0
npaunAuthor Commented:
Do you know declaration for FindWindowW (preferably for VB6)? It seems I cannot find it on the net, or at least not any one I can trust to be correct...
0
louisfrCommented:
Found this. I cannot check it from here, but it looks like it should work.
http://support.microsoft.com/kb/145727
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

vb_elmarCommented:
Visual Basic 6.0 stores strings internally as Unicode (UTF-16) and has no Unicode (16-bit) controls. VB6 controls are ANSI only (using the 8-bit single-byte ANSI model).
For this reason, Visual Basic provides two sets of form controls and uses the ANSI(8-bit) set as the default control. Any strings passed to the ANSI(8-bit) set of form controls will be converted from their internal Unicode representation to an ANSI(8-bit) representation. Because of this some UNICODE strings are displayed as ' ??? ' .

How To Read and Display UNICODE String on Visual Basic Form
0
vb_elmarCommented:
There is the function 'GetWindowTextW'. It is not
documented in the API-Viewer. -Today I coded a small
VB6 project (see attachement).

If you place the mouse cursor over a ms-word window with
an unicode titlebar (e.g. chinese/japanese) the chinese or
japanese letters will appear in a VB6 (FORMS 2.0 !! ) textbox.

-Try it out.
6.zip
0
npaunAuthor Commented:
@vb_elmar
Thanks, but actually inverted the problem :) You're post tell how to get a Unicode text of a window for which you have its handle, which is not what I need. I have the inverse problem: I have the Unicode title of the window, and now I have to find the window's handle, presumably by using FindWindowW...
0
vb_elmarCommented:
something like this ?
Private Declare Function FindWindowW Lib "user32" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long

Sub mySub(ByVal szTitle As String)
    Dim hWndMain As Long
    hWndMain = FindWindowW(0, StrPtr(szTitle))
    If hWndMain <> 0 Then MsgBox hWndMain
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
npaunAuthor Commented:
Yes, that works just fine :)
Thanks to both. I'm going to split points, because louisfr answered first and gave the answer which was essentially correct, and the vb_elmar gave a full code example... I hope that is ok.
0
vb_elmarCommented:
For me It's ok to get 300 points.

-How did you pass the string to the FindWindowW function?

-When calling the mySub with (japanese) chars like Call mySub("ニコニコ")
it does not work because the VB6 IDE converts immediately
Call mySub("ニコニコ") to Call mySub("????")

here is the snippet:
Sub mySub(ByVal szTitle As String)
       Dim hWndMain As Long
       hWndMain = FindWindowW(0, StrPtr(szTitle))
       If hWndMain <> 0 Then MsgBox hWndMain
End Sub
0
npaunAuthor Commented:
a passed just as in your example:

Public Function FindWindowUnicode(ByVal szTitle As String) As Long
    Dim hWndMain As Long
    hWndMain = FindWindowW(0, StrPtr(szTitle))
   
    FindWindowUnicode = hWndMain
End Function

and then

mHwndWord = FindWindowUnicode(WrdDoc.ActiveWindow.Caption & " - " & "Microsoft Word")
0
vb_elmarCommented:
I have to correct - it must be:
Private Declare Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
not
Private Declare Function FindWindowW Lib "user32" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long

And this is the code I tested using a MS-Word document with a japanese caption.
It returns a handle like 3212872 :

MS-Word VBA code:
Private Declare Function FindWindowW Lib "user32" _ 
            (ByVal lpClassName As String, _ 
             ByVal lpWindowName As Long) As Long

Public Function FindWindowUnicode(ByVal szTitle As String) As Long
    Dim hWndMain As Long
    hWndMain = FindWindowW(vbNullString, StrPtr(szTitle))   
    FindWindowUnicode = hWndMain
End Function

Private Sub CommandButton1_Click()
   MsgBox (ActiveWindow.Caption)
   MsgBox FindWindowUnicode(ActiveWindow.Caption & " - microsoft word")
End Sub

Open in new window

0
npaunAuthor Commented:
...I've noticed lpClassName As Long, but I thought it might be a reason for that too, i.e. sending that too as a pointer, although it seamed strange, as the class name I would never expect to be Unicode...
Corrected. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.