3

I have to create a tlb file so I can access a Python COM server from .Net. I only want a single method (Process) that takes a string and returns a string. The idl file is below:

import "oaidl.idl";
import "ocidl.idl";
[
    uuid(A66551C8-ADB4-4A1E-BB19-39F356282A7E),
    dual,
    oleautomation
]
interface IMyInterface : IDispatch {
    HRESULT Process([in, out] BSTR str);
}

[
  uuid(BE0CDA23-A2D0-40E5-8D33-61DBE78E0A03)
]
library MyTypeLib
{
    importlib("stdole2.tlb");

    [uuid(F235B9D8-9C1A-44C3-A59F-3C822EC82A67)]
          coclass MyObject {
          [default] interface IMyInterface;
    };
};

Using midl this successfully generates a tlb file The python program is below:

import comtypes
import comtypes.server.localserver

from comtypes.client import GetModule
# generate wrapper code for the type library, this needs
# to be done only once (but also each time the IDL file changes)
GetModule("audiclave.tlb")

from comtypes.gen.MyTypeLib import MyObject

class AudiclaveImpl(MyObject):
    # registry entries
    _reg_threading_ = "Both"
    _reg_progid_ = "Audiclave.Analysis.1"
    _reg_novers_progid_ = "Audiclave.Analysis"
    _reg_desc_ = "Python engine for Audiclave"
    _reg_clsctx_ = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER
    _regcls_ = comtypes.server.localserver.REGCLS_MULTIPLEUSE

    def Process(self, a):
        return str(a) + "executed"


if __name__ == "__main__":
    from comtypes.server.register import UseCommandLine
    UseCommandLine(AudiclaveImpl)

I run that with

python audiclaveAnalysis.py /regserver

Now I open python and do the following:

>>> from comtypes.client import CreateObject
>>> x = CreateObject("Audiclave.Analysis")
# Generating comtypes.gen._BE0CDA23_A2D0_40E5_8D33_61DBE78E0A03_0_0_0
>>> x.Process("test")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\site-packages\comtypes\__init__.py", line 596, in call_with_inout
v = atyp.from_param(v)
AttributeError: 'str' object has no attribute 'from_param'

How do I define a method to take a string parameter and return one?

1 Answer 1

3

This worked

HRESULT Process([in] BSTR str, [out, retval] VARIANT *pResult);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.