Closed Spikxzy closed 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.
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.
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)
@Spikxzy Will you be able to help here?
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.
@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":
opc_server = OPCServer()
opc_server.start()
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:
The COM object was programmed in C# with .NET Core 6 and looks like this.
The .csproj file:
The ComObject.cs file:
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.