Link to home
Start Free TrialLog in
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)Flag for United States of America

asked on

Get Window Instance/Object From Handle (hWnd)

Working in VB/VBA, I have a window handle and I need to convert it to a window object/instance which I can use to access the window object's properties.

AccessibleObjectFromWindow has not proven useful.

How do I do this?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zorvek (Kevin Jones)

ASKER

It's a window created using mshta.exe and configured with some scripts:

    CreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""<head><script>moveTo(-32000,-32000);document.title='" & x86WindowSignature & "'</script><hta:application showintaskbar=no /><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & x86WindowSignature & "',document.parentWindow);</script></head>""", 0, False
   
            For Each Window In CreateObject("Shell.Application").Windows
                On Error Resume Next
                Set Getx86Window = Window.GetProperty(x86WindowSignature)
                Error = Err.Number
                On Error GoTo 0
                If Error = 0 Then Exit For
                Pause 0.01, True
            Next Window
   
    ' Configure the window environment - global object variables are defined, one for each scripting object - they are instantiated by calling the Initialize routine
    Getx86Window.execScript "var VBScript, JScript;"
    Getx86Window.execScript "Sub Initialize() : Set VBScript = CreateObject(""MSScriptControl.ScriptControl"") : VBScript.Language = ""VBScript"" : Set JScript = CreateObject(""MSScriptControl.ScriptControl"") : JScript.Language = ""JScript"" End Sub", "VBScript"
   
    ' Initialize the window environment
    Getx86Window.Initialize

x86WindowSignature is a function that returns a unique string or key.

I'm going through this process because Microsoft doesn't provide 64-bit versions of the VBScript and JScript engines and this process allows me to create 32-bit versions and use them from a 64-bit world.

To reuse this scripting container I need to look at the existing windows and query one of the properties I created:

 Set Getx86Window = Window.GetProperty(x86WindowSignature)

Normally I use this logic to find the window of interest:

    ' Look for an existing window
    For Each Window In CreateObject("Shell.Application").Windows
        On Error Resume Next
        Set Getx86Window = Window.GetProperty(x86WindowSignature)
        Error = Err.Number
        On Error GoTo 0
        If Error = 0 Then Exit Function
    Next Window

But, if the application crashes or otherwise fails to close the scripting window, it remains open but is not listed with CreateObject("Shell.Application").Windows.

But I can find it using FindWindow:

WindowHandle = API_FindWindow("HTML Application Host Window Class", x86WindowSignature)

I'm stuck trying to convert the window handle to a VB/VBA "Window" object variable.

I tried a simple copy memory from the handle variable to the object variable but that failed.

Kevin
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial