Solved

Getting window handle for a window with UNICODE title?

Posted on 2014-10-17
12
312 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
Need to pass a connection through class/Method 1 39
Syntax error 9 45
Entity Framework 7 30
This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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