Python win32com - Class 未注册错误

Python win32com - Class not registered error

我正在尝试使用 win32com 通过其 COM 接口控制设备 (Gamry Interface 5000 Potentiostat)。

# Imports
import win32com.client as client

# Get device list
devices = client.Dispatch('GamryCOM.GamryDeviceList')

# Iterate through devices
for i in range(devices.Count()):
    # Get device (this wors as we only have one connected yet)
    device = devices.EnumSections()[i]
    print(device)
    
# Setup potentiostat object
potentiostat = client.Dispatch('GamryCOM.GamryPstat')

当我 运行 执行此操作时,我收到以下错误消息:

IFC5000-10519
Traceback (most recent call last):
  File "c:\Users\Rob\AppData\Local\Programs\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 "c:\Users\[...]\gamry_control_01.py", line 23, in <module>
    potentiostat = client.Dispatch('GamryCOM.GamryPstat', clsctx = pythoncom.CLSCTX_LOCAL_SERVER )
  File "c:\Users\Rob\AppData\Local\Programs\Python\Python39-32\lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
  File "c:\Users\Rob\AppData\Local\Programs\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "c:\Users\Rob\AppData\Local\Programs\Python\Python39-32\lib\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(
pywintypes.com_error: (-2147221164, 'Class not registered', None, None)

有趣的是,第一个调度语句工作正常,只是第二个失败了。

我正在使用 64 位 Windows11 设置并测试了不同的 Python 环境:

我也尝试使用 comtypes 而不是 win32com 导致了同样的错误。

非常感谢您的帮助!

此致

"Class 未注册" 表示无法找到 class 的 class 工厂。这是一个独立于 Python 的错误,与它无关......除了 Python 的位数(无论是 32 位还是 64 位,以及 COM 服务器是 32 位还是 64-少量)。服务器是进程中(DLL)还是进程外(EXE)也很重要。对于 EXE,这真的无关紧要,但对于 In-Process 服务器,调用程序和 COM 服务器的位数必须匹配。为了查看您是否有位数问题,请执行此操作...

创建以下 VB 脚本,将其命名为 test.vbs

set obj = CreateObject("GamryCOM.GamryDeviceList")
MsgBox TypeName(obj)

从命令行调用脚本两次,一次使用 64 位 VB 脚本引擎,一次使用 32 位引擎。如果您从 test.vbs 所在的其他目录 运行ning,您还必须提供 test.vbs 的路径。

64 位引擎:

c:\windows\system32\wscript.exe test.vbs

32 位引擎:

c:\windows\syswow64\wscript.exe test.vbs

如果对象已正确注册并且是一个 EXE 服务器,则脚本 运行 对于 32 位和 64 位都应该成功。如果对象已正确注册并且是 DLL 服务器,则上述脚本中只有一个 运行 会成功。如果对象注册不正确,脚本 运行 和两个引擎都会失败。

在双重失败的情况下,您将不得不弄清楚 COM 对象的生产者希望您如何调用它。可能涉及许可。

在单一故障的情况下,您将不得不使用 Python 的不同位数来匹配服务器,或者如果您确实 运行ning 64 位 Python 正如你所说,然后可能想出一种方法来使用 DllHost 之类的东西在 64 位中托管 32 位 DLL。我从来没有做过,但是网上有关于如何做的文章。

我遇到了同样的问题并联系了销售人员。 他给我发了一份文件,说在某些设备上你必须使用 pstat = client.CreateObject('GamryCOM.GamryPC5Pstat') 参考系列和 pstat = client.CreateObject('GamryCOM.GamryPC6Pstat') 接口系列。 对我来说,至少 OSError: [WinError -2147221164] Class not registered 消失了。 我用过“comtypes”。