1

I'm trying to obtain the COMObject of an already running application. I have been able to obtain the hwnd and after running AccessibleObjectFromWindow with the correct parameters I get the error:

OSError: [WinError -2147467259] Unspecified error

When I run the same script using the correct hwnd for Microsoft Word or Excel, it works.

Example code that gets the hwnd of the application, iterates through all children and tries to run AOFW. I run through all possible children because I know on Word or Excel, as per this question, there is a accessible window that must be used instead of the whole parent hwnd.

import win32gui
from ctypes import oledll
from ctypes import byref

#installed by easy_install comtypes
from comtypes import POINTER
from comtypes.automation import IDispatch
import comtypes.client.dynamic as comDy

#obtain hwnd of application
hwnd = win32gui.FindWindow(None, "CATIA V5")

def winfun(hwnd, lparam):
    allHwnd.append(hwnd)
#naughty global variable
allHwnd = []
#call winfun to populate allHwnd with all the child hwnds
win32gui.EnumChildWindows(hwnd, winfun, None)

for childHwnd in allHwnd:
    OBJID_NATIVEOM = -16
    p = POINTER(IDispatch)()
    try:
        oledll.oleacc.AccessibleObjectFromWindow(childHwnd, OBJID_NATIVEOM, byref(IDispatch._iid_), byref(p))
        window = comDy.Dispatch(p)
        CATIA = window.application
        print('winner winner hwnd: '+str(childHwnd))
        print(CATIA)
    except:
        print('No dice')
#lots of 'No dice' printed with one successful COMObject when the application is Word or Excel.
#only lots of 'No Dice' when running on everything else

I've tried a few applications and the only one that has a child that can initiate a lazybind COMObject are Microsoft Office applications. everything else prints lots of 'No Dice'

Is it not possible to get the COMObject of an already deployed application?

H

HCAWN
  • 11
  • 3
  • The COM server must implement WM_GETOBJECT, as explained at [Using OBJID_NATIVEOM](https://learn.microsoft.com/en-us/windows/desktop/winauto/using-objid-nativeom-to-expose-a-native-object-model-interface-for-a-window) – Castorix Jun 13 '19 at 17:03

0 Answers0