简体   繁体   中英

Get running instance of COM object in Python with pywin32

I am attempting to automate actions in Reflection Desktop from python. I have done this successfully with VBA before like this:

Set Session = CreateObject("Reflection Workspace")

Then this gives me the active session:

Set Screen = Session.GetObject("Frame").SelectedView.Control.Screen

or this gives me a specific session:

Set Screen = Session.tView(iView).Control.Screen

When doing this from python I anticipated the COM object should be the same and based on this guide Attach to a running instance of a COM application I should be able to attach to a running application like this:

system = win32com.client.GetObject('Reflection Workspace') 

and while this does not return any errors, I also can not find any usable attributes. If I instead use:

system = win32com.client.gencache.EnsureDispatch('EXTRA.System')

I can get the active session using:

screen = system.ActiveSession.Screen

and everything works. If I run

win32com.client.combrowse.main()

and look at the running objects, Reflection Workspace is the one that is listed, not EXTRA.System.

I also used

win32com.client.makepy.main() 

to create a python library of Reflection Workspace and in doing so I can list out all the available properties and methods of the object but I do not know how to use that new library to early bind an already active session of the application.

So my questions at this point are:

  1. If the COM browser is showing Reflection Workspace and not EXTRA.System why can I connect to EXTRA.System as the running process?

  2. Why can I connect to Reflection Workspace but none of the COM attributes that are available when using VBA are available when using python?

  3. Can someone point to a guide on how to actually assign a running COM process to an object using the library created by makepy()?

I ended up finding a solution to this. It looks like the COM API "EXTRA.System" is kept for legacy compatibility and "Reflection Workspace" should be used for new development.

I was sure I had tried already but after posting the question I tried again using GetObject to get the reflection frame object and it worked. So for anyone else trying to do this in python you can the application object using:

system = win32com.client.GetObject('Reflection Workspace')

then get the active view:

screen = system.GetObject("Frame").SelectedView.Control.Screen

or get a specific view, where 1 is the view number:

screen = system.GetObject("Frame").view(1).Control.Screen

After that the methods are the same as the documentation for GetText, PutText, etc.

Thanks for providing the above answer. @chris

system.GetObject("Frame").SelectedView returns None always

system.GetObject("Frame").view(1) 

gives pywintypes.com_error:(-214735256,'Exception occurred',(0,Attachmate.Relection.Objects','Index was outside the bound of the arra.', None, 0, -2146233080),None) I have tried with 0 also still gives same error and reflection window is open.

I can not figure out what the actual cause is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM