IDL文件
import "oaidl.idl"; import "ocidl.idl"; [ object, uuid(B325C169-61FA-4513-9DB1-0CC231CCB633), dual, nonextensible, helpstring("IComTestClass Interface"), pointer_default(unique) ] interface IComTestClass : IDispatch{ [id(1), helpstring("method Method1")] HRESULT Method1(LONG arg1); }; [ uuid(9098C7D5-91BA-4C0C-837B-DB81C3DBA6AE), version(1.0), helpstring("ComTest 1.0 Type Library") ] library ComTestLib { importlib("stdole2.tlb"); [ uuid(F41DAE07-8D10-430B-8834-06BB4B9AE8A2), helpstring("_IComTestClassEvents Interface") ] dispinterface _IComTestClassEvents { properties: methods: [id(1), helpstring("method Event1")] HRESULT Event1(LONG arg1); }; [ uuid(2961361E-8E6D-4618-95A8-4DF03B65BDC3), helpstring("ComTestClass Class") ] coclass ComTestClass { [default] interface IComTestClass; [default, source] dispinterface _IComTestClassEvents; }; };
STDMETHODIMP CComTestClass::Method1(LONG arg1) { // TODO: Add your implementation code here Fire_Event1(arg1); return S_OK; }
win32com调用组件
import win32com.client class EventHandler: def OnEvent1(self, val): print(val) o = win32com.client.DispatchWithEvents("ComTest.ComTestClass", EventHandler) o.Method1(23)