Python中使用win32com加载COM对象遇到的问题
Let's break down each of your issues and walk through targeted fixes, since you've already ruled out 32/64-bit mismatches (that's a huge win already!).
1. Dispatch() returns only default methods, no custom plugin methods
The problem here is that win32com.client.Dispatch() uses late binding by default, which doesn't pull in the custom type information from your COM plugin. LabVIEW likely uses early binding under the hood, which is why it sees all the methods.
Fix: Use EnsureDispatch to force type cache generation
This method automatically generates and caches the type library for your COM object, so Python can see your custom methods:
import win32com.client # Generate type cache and create the instance sendDMscript = win32com.client.gencache.EnsureDispatch('COMExamplePlugIn.COMExampleInterface') # Now dir() should show your plugin's methods like ExecuteScript print(dir(sendDMscript))
If that still doesn't work, try dynamic binding as a fallback:
import win32com.client.dynamic sendDMscript = win32com.client.dynamic.Dispatch('COMExamplePlugIn.COMExampleInterface') sendDMscript.ExecuteScript(script='result("I did it")')
2. gencache.GetModuleForProgID() throws "Class not registered"
This error usually means one of three things: your plugin's ProgID/CLSID isn't properly registered, the type library cache is corrupted, or you're missing a dependency.
Fixes to try:
- Verify registry entries: Open
regeditand search forCOMExamplePlugIn.COMExampleInterfaceunderHKEY_CLASSES_ROOT\ProgID. Ensure it points to a valid CLSID entry inHKEY_CLASSES_ROOT\CLSID. - Re-register the COM plugin: Open a 32-bit Command Prompt (critical since everything is 32-bit) and run:
regsvr32 "C:\Full\Path\To\Your\COMExamplePlugIn.dll" - Clear corrupted gen_py cache: Delete the
gen_pyfolder at%LOCALAPPDATA%\Temp\gen_py(this is where win32com stores cached type info). Then re-run your Python code to let it regenerate fresh cache files.
3. Passing IID causes AttributeError: 'PyIID' object has no attribute 'InvokeTypes'
You're mixing up how to use CLSIDs and IIDs here. The interface class from gencache doesn't take an IID as a constructor argument—you need to use CoCreateInstance to properly instantiate the object with both the class ID and interface ID.
Fix: Use CoCreateInstance with correct IDs
First, make sure you have two distinct IDs:
- CLSID: The ID of the COM class (
{8C0B6A41-4ED2-4B44-B7B1-6492695B9C3D}in your code) - IID: The ID of the
ICOMExampleInterfaceinterface (you can get this using the OLEView tool from the Windows SDK to inspect your plugin's type library)
Then use this code to create the instance properly:
import win32com.client import pythoncom from pywintypes import IID # Replace with your actual interface IID IID_ICOMExampleInterface = IID('{Your_Interface_IID_Here}') CLSID = IID('{8C0B6A41-4ED2-4B44-B7B1-6492695B9C3D}') # Create the raw COM instance raw_instance = pythoncom.CoCreateInstance( CLSID, None, pythoncom.CLSCTX_INPROC_SERVER, IID_ICOMExampleInterface ) # Wrap it for easy Python access sendDMscript = win32com.client.Dispatch(raw_instance) sendDMscript.ExecuteScript(script='result("I did it")')
Bonus General Checks
- Make sure DigitalMicrograph is running before you create the COM instance—some plugins require the host application to be active.
- Use the OLEView tool (part of Windows SDK) to inspect your COM plugin's type library: this will confirm ProgIDs, CLSIDs, IIDs, and method signatures are all correct.
内容的提问来源于stack exchange,提问作者Tyler

