mhammond / pywin32

Python for Windows (pywin32) Extensions
5.05k stars 797 forks source link

Error: -2147467262, 'No such interface supported' #1827

Closed Spikxzy closed 2 years ago

Spikxzy commented 2 years ago

I am trying to use a COM object in Python. This COM object was created with C# and when I try to load the COM object by using the command win32com.client.Dispatch(ProgId). I am getting the following error instead of a working COM object:

>>> import win32com.client
>>> comobj = win32com.client.dynamic.Dispatch("HelloWorldCOMObject")
Traceback (most recent call last):
  File "C:\PathToPrograms\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 86, 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):
  File "<stdin>", line 1, in <module>
  File "C:\PathToPrograms\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 134, in Dispatch
    IDispatch, userName = _GetGoodDispatchAndUserName(IDispatch, userName, clsctx)
  File "C:\PathToPrograms\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\PathToPrograms\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(
pywintypes.com_error: (-2147467262, 'No such interface supported', None, None)
>>>

The COM object was programmed in C# with .NET Core 6 and looks like this.

The .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
      <EnableComHosting>true</EnableComHosting>
  </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <PlatformTarget>x86</PlatformTarget>
    </PropertyGroup>

</Project>

The ComObject.cs file:

using System;
using System.Runtime.InteropServices;

namespace COMNetCore6
{
    [ComVisible(true)]
    [Guid("64D6F9F6-6163-401A-82E6-C941CAF01399")]
    [ProgId("HelloWorldCOMObject")]
    public class ComObject
    {
        public string SayHello() => "Hello world from .NET Core";
    }
}

After building the Project I registered the generated 'x.comhost.dll' file with 'regsvr32 x.comhost.dll'. The COM object gets registered successfully and works fine with Visual Basic in Excel for example.

I am using the 32 bit version of Python 3.9.10 and pywin32 version 303. I have also tried compiling the COM object in 64 bit and using the 64 bit version of Python 3.9.10 which led to the same error.

mhammond commented 2 years ago

You removed the suggestion to follow the instructions at https://github.com/mhammond/pywin32/blob/master/README.md#support. That suggestion exists because issues like this are far more likely to get attention if followed - I suspect this issue will remain untouched here.

Spikxzy commented 2 years ago

I found a solution to this problem:

>>> import pythoncom
>>> import pywintypes
>>> from win32com.client import Dispatch
>>> unk = pythoncom.CoCreateInstance(pywintypes.IID('{64D6F9F6-6163-401A-82E6-C941CAF01399}'), None, pythoncom.CLSCTX_ALL, pythoncom.IID_IUnknown)
>>> disp = Dispatch(unk.QueryInterface(pythoncom.IID_IDispatch))
>>> print(disp.SayHello)
Hello world from .NET Core
>>>

This way around it works.

gopinath0712 commented 6 months ago

Hi Team, I am also facing this issue as listed below but above solutions not working Someone pls help?

Issue: (Refer Screenshot) File "C:\Users\SuperUser\AppData\Local\Programs\Python\Python312\Lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch IDispatch = pythoncom.connect(IDispatch) pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)

Details: Trying to run OPC Server simulation script using Python language

OS: Windows Server 2019 Python: Python 3.12.3 (64 bit)

Python Error1
gopinath0712 commented 6 months ago

@Spikxzy Will you be able to help here?

Spikxzy commented 6 months ago

From what I can see now I don't really know what the problem could be. If I see the entire code, I might be able to help. However, in my case I used Python only as a client and not as a server, so I am not sure if I can help at all.

gopinath0712 commented 6 months ago

@Spikxzy Thanks for responding. I am trying to develop simulator for OPC with Server and client using python. As i analyzed, Server ProdId/ClassId reference is not actually correct (opc_server = win32com.client.Dispatch("OPC Server ProgId/ClsId")). i am trying to use Matrikon and Kepserver as OPC server but their references not working here eventhough registered in this system (Refer the screenshot attached for OPC server registration)

Please find my Server code below, import pythoncom import win32com.client import random import time import string

class OPCServer: def init(self): self.running = False self.value = 0 self.value1 = 0.0

def start(self):
    self.running = True
    while self.running:
        # Generate random value for simulation
        self.value = random.randint(0, 100)
        print(f"Publishing value: {self.value}")

        self.value1 = random.uniform(0.0, 1.0)
        print(f"Publishing value: {self.value1}")

     #   self.value2 = random.string(8)
     #   print(f"Publishing value: {self.value2}")

        # Update OPC item value
        self.update_opc_item()

        # Sleep for a short interval (simulate data update rate)
        time.sleep(1)

def update_opc_item(self):
 #try:
    # Create OPC server object
    opc_server = win32com.client.Dispatch("WScript.Shell")
    print("Connected")
    # Connect to OPC server (replace 'OPCServer.OPCServer.1' with your OPC server ProgID)
    opc_server.Connect('WScript.Shell')
    print("Connected1")
    # Update OPC item value (replace 'ItemID' with your OPC item ID)
    #opc_server.OPCItems.Item("ItemID").Write(self.value)
    #opc_server.OPCItems.Item("ItemID").Write(self.value1)
  #  opc_server.OPCItems.Item("ItemID").Write(self.value2)

    # Disconnect from OPC server
    opc_server.Disconnect()
 #except pythoncom.com_error as e:
 #       print(f"COM Error: {e}")

if name == "main":

Initialize and start OPC server simulation

opc_server = OPCServer()
opc_server.start()

image