Solved

Getting window handle for a window with UNICODE title?

Posted on 2014-10-17
12
294 Views
Last Modified: 2014-10-18
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…
0
Comment
Question by:npaun
  • 5
  • 5
  • 2
12 Comments
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 200 total points
ID: 40386640
You can try using FindWindowW.
0
 

Author Comment

by:npaun
ID: 40386650
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
 
LVL 11

Expert Comment

by:louisfr
ID: 40386673
Found this. I cannot check it from here, but it looks like it should work.
http://support.microsoft.com/kb/145727
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40388682
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
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40388730
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
 

Author Comment

by:npaun
ID: 40388773
@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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 17

Accepted Solution

by:
vb_elmar earned 300 total points
ID: 40388799
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
 

Author Comment

by:npaun
ID: 40388848
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
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40388890
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
 

Author Comment

by:npaun
ID: 40388920
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
 
LVL 17

Expert Comment

by:vb_elmar
ID: 40388960
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
 

Author Comment

by:npaun
ID: 40389004
...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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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

20 Experts available now in Live!

Get 1:1 Help Now