Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting window handle for a window with UNICODE title?

Posted on 2014-10-17
12
Medium Priority
?
396 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 800 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 
LVL 17

Accepted Solution

by:
vb_elmar earned 1200 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Six Sigma Control Plans
Loops Section Overview

927 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