C# COM Interop. Invoke PROPERTYGET vs. FUNC RRS feed

  • Question

  • Now here's a strange question:

    I'm working with C# and Python and we're using COM for interoperability. The issue comes up when implementing an interface in C# and using it in Python, specifically, when attempting to use functions with a return value, but with no parameters. Implementing such a function in ATL C++ causes no problems, so I believe this is an issue with C#, not Python. I put the thread in CLR because it's COM interop, but if it's more appropriate in the C# forums please move it.


    Python first attempts to Invoke something like this with an invoke type of PROPERTYGET. If this fails, it tries again with invoke type FUNC. The problem is, it doesn't fail.

    What I want to know is if it's possible to stop C# from responding to a PROPERTYGET style invoke on a parameterless function by calling the function and returning its return value, and instead have it fail. 

    Thursday, September 18, 2008 7:48 PM

All replies

  • No, you cannot alter .NET's COM interop implementation.  Why is this a problem?
    Hans Passant.
    Thursday, September 18, 2008 8:14 PM
    Thanks for the quick reply.

    The problem is in how Python's win32com package does late-binding to IDispatch interfaces.

    The root issue is that win32com is unable to get certain info from the ITypeInfo it is getting from the object. In specific, it is having an issue with ITypeComp.Bind()

    When win32com fails to get that information, it tries a few other things to get the late-binding to work, following the procedure I outline in my first post. First it tries a PROPERTYGET Invoke, if that fails, it creates a Python method that wraps a FUNC Invoke and passes it back to Python.

    When the PROPERTYGET Invoke succeeds, the Python code that looks like this:


    evaluates 'returnString' as a unicode string, making the line end up like this:

    Of course, you cannot call a string, so exceptions are thrown.

    I can't just treat these parameterless functions as properties in the Python code because we're trying to keep the language a component is written in irrelevant, and the parameterless functions work just fine when implemented in Python.

    The only .NET side tweak I can think of now, is making sure the ITypeInfo win32com gets a handle on provides the correct information. Unfortunately I don't know how ensure that.
    Thursday, September 18, 2008 9:20 PM