Python win32 com : how to handle 'out' par

2020-07-16 03:23发布

问题:

I need to access a third-party COM server with following interface definition (idl):

interface IDisplay : IDispatch
{
  HRESULT getFramebuffer (
    [in] ULONG aScreenId,
    [out] IFramebuffer * * aFramebuffer,
    [out] LONG * aXOrigin,
    [out] LONG * aYOrigin );
};

As you can see, it returns 3 values via [out] parameter modificators. How to handle this via python win32 COM api? For example, i create an object and get IDisplay from it:

object = win32com.client.Dispatch( "VirtualBox.VirtualBox" )
display = object.display

How to call display.getFrameBuffer() so it will work? I have tried different ways, but it's always 'type mismatch' on second argument ([out] for IFrameBuffer)

回答1:

Since those are out parameters, can't you simply do the following?

Framebuffer, XOrigin, YOrigin = display.getFrameBuffer(ScreenId)

There is some good references in Python Programming on Win32 Chapter 12 Advanced Python and COM

And they indicate that the syntax should be like above. They also mention using MakePy for COM objects:

There are a number of good reasons to use MakePy: (copied from the book)

  • The Python interface to automation objects is faster for objects supported by a MakePy module.

  • Any constants defined by the type library are made available to the Python program. We discuss COM constants in more detail later in the chapter.

  • There is much better support for advanced parameter types, specifically, parameters declared by COM as BYREF can be used only with MakePy-supported objects. We discuss passing parameters later in the chapter.



回答2:

Use the makepy module, invoking it as follows:

>>> import win32com.client.makepy as makepy
>>> makepy.main()

A window will open with a list of type libraries. Scroll to "Virtual Box Type Library" and select it, then click "OK". A Python module will be created in a location which is printed out (typically %TEMP%\gen_py\2.x\).

The generated class will automatically be used by win32com.client.Dispatch, but if you need it explicitly you can access it via functions in the win32com.client.gencache module.



标签: python com