1

I have a .NET 6.0 COM library that I am trying to use with Python 3.9. I additionally wrote an idl file to create a tlb and registered the COM object using regsvr32. The registration seems fine as the registry entries look good and I can also use the object with other COM clients such as Excel and LabWindows. However, when using the COM object in a python script I get the following errors:

Traceback (most recent call last):
  File "path\Python39\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
line 5, in Init
    test = win32com.client.Dispatch("ComTest")
  File "path\Python39\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "path\Python39\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "path\Python39\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147467262, 'No such interface supported', None, None)

My code looks like this:

Python:

import win32com.client

def Init():
    test = win32com.client.Dispatch("ComTest")
    test.SomeMethod(1, 2, "test")

if __name__ == "__main__":
    Init()

C#:
Interface

[ComVisible(true)]
[Guid(AssemblyInfo.InterfaceGuid)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComTest
{
    public double SomeMethod(double param1, double param2, string param3);
}

Class

[ComVisible(true)]
[Guid(AssemblyInfo.ClassGuid)]
[ProgId("ComTest")]
public class ComTest : IComTest
{
        public double SomeMethod(double param1, double param2, string param3)
        {
            // implementation
        }
    }

I first thought it might be some issue with different bit versions but that does not seem to be the problem either. I tried installing a 32 bit version of python and testing it with that but I still got the same error. Does someone know what might cause the problem? Thanks for your help.

tabaluga
  • 41
  • 5
  • Did you try using tlbexp.exe to export a type library for the assembly instead of creating IDL and compiling it to make a type library? (Unless the latest .NET stuff doesn't allow that anymore.) – Joseph Willcoxson Aug 31 '21 at 15:26
  • @JosephWillcoxson Thanks for the suggestion. I already tried to use tlbexp.exe but was not successful. This tool was not supported for .NET 5.0 as stated here [link](https://github.com/dotnet/runtime/issues/34609) which is why I am assuming that it is not planned for .NET 6.0 either. – tabaluga Sep 02 '21 at 06:41

0 Answers0