Solved

Getting window handle for a window with UNICODE title?

Posted on 2014-10-17
12
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

631 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