我正在尝试获取已运行的应用程序的COMObject。我已经能够获取hwnd,并使用正确的参数运行AccessibleObjectFromWindow后出现错误:
OSError: [WinError -2147467259] Unspecified error
当我为Microsoft Word或Excel使用正确的hwnd运行相同的脚本时,它会起作用。
获取应用程序的hwnd的示例代码,遍历所有子级并尝试运行AOFW。我会遍历所有可能的子项,因为我知道在this question中,在Word或Excel上,必须使用一个可访问的窗口来代替整个父项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
我已经尝试了一些应用程序,并且只有一个可以启动lazybind COMObject的子级的应用程序是Microsoft Office应用程序。其他所有东西都会打印很多“无骰子”
是否可以获得已部署的应用程序的COMObject?
H