如何从Python的C#DLL调用Windows COM方法?

时间:2015-10-30 14:49:01

标签: python com

有人写了一个C#COM DLL,我需要从中调用一些函数 最重要的部分是:

using System;
using System.Runtime.InteropServices; //For using COM interop
using VRTAComms;
namespace StepsToEveConverter
{
    interface IManagedInterface // needed for COM
    {
    }
    //GUID ec456b4b-5ac4-46e8-99e8-64c193c316af 32 - bit StepsToEveConverter
    [GuidAttribute("ec456b4b-5ac4-46e8-99e8-64c193c316af")]
    [ComVisible(true)]
    [ProgId("StepsToEveConverter")]
    public class StepsToEveConverter : IManagedInterface
    {
        //...bla bla
        public StepsToEveConverter()
        {
            System.Console.WriteLine("\nStepsToEveConverter::Constructor called\n");
        }
        public void StartvECU(string PathOfVECU)
        {
            System.Console.WriteLine("\nStepsToEveConverter::StartvECU called\n");
            connection = VECUFinder.Load("localhost", @PathOfVECU);
            connection.Start();
        }
    }
}

我在调用方法StartvECU()时遇到问题; 我可以在python中创建一个这样的对象:

...
    self.COMInterfaceObj = comtypes.client.CreateObject("StepsToEveConverter");
...

在python中打印C#消息

"StepsToEveConverter::Constructor called"

但是如果我从C#DLL模块调用该方法:

public void StartvECU(string PathOfVECU)

使用python代码

 self.COMInterfaceObj.StartvECU("123");

我收到错误Python执行错误

  File "C:\Users\XXX\AppData\Local\Continuum\Anaconda\lib\site-packages\comtypes-1.1.2-py2.7.egg\comtypes\__init__.py", line 277, in __getattr__
    raise AttributeError(name)
AttributeError: StartvECU

我做错了什么?

PS:一些perl代码工作正常。为什么在python中比在perl中这么难?

 $PathOfVECU = 'c:\x.exe';
$handle = Win32::OLE->new('StepsToEveConverter');
$handle->StartvECU($PathOfVECU);

Ps:我可以获得一些关于使用

创建的python COM对象的额外信息

帮助(self.COMInterfaceObj);

Help on POINTER(_StepsToEveConverter) in module comtypes object:

class POINTER(_StepsToEveConverter)(comtypes.gen._F0E8D21A_98E7_42D5_A8CD_A5001E215F96_0_1_0._StepsToEveConverter, POINTER(IDispatch))
 |  Method resolution order:
 |      POINTER(_StepsToEveConverter)
 |      comtypes.gen._F0E8D21A_98E7_42D5_A8CD_A5001E215F96_0_1_0._StepsToEveConverter
 |      POINTER(IDispatch)
 |      comtypes.automation.IDispatch
 |      POINTER(IUnknown)
 |      IUnknown
 |      _compointer_base
 |      ctypes.c_void_p
 |      _ctypes._SimpleCData
 |      _ctypes._CData
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  __getattr__(self, name)
 |      Implement case insensitive access to methods and properties
 |  
 |  __setattr__(self, name, value)
 |      Implement case insensitive access to methods and properties
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __com_interface__ = <class 'comtypes.gen._F0E8D21A_98E7_42D5_A8CD_A500...
 |  
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from comtypes.gen._F0E8D21A_98E7_42D5_A8CD_A5001E215F96_0_1_0._StepsToEveConverter:
 |  
 |  __map_case__ = {}
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from POINTER(IDispatch):
 |  
 |  __ctypes_from_outparam__ = wrap_outparam(punk)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from comtypes.automation.IDispatch:
 |  
 |  GetIDsOfNames(self, *names, **kw)
 |      Map string names to integer ids.
 |  
 |  GetTypeInfo(self, index, lcid=0)
 |      Return type information.  Index 0 specifies typeinfo for IDispatch
 |  
 |  GetTypeInfoCount(...)
 |  
 |  Invoke(self, dispid, *args, **kw)
 |      Invoke a method or property.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from IUnknown:
 |  
 |  AddRef(self)
 |      Increase the internal refcount by one and return it.
 |  
 |  QueryInterface(self, interface, iid=None)
 |      QueryInterface(interface) -> instance
 |  
 |  Release(self)
 |      Decrease the internal refcount by one and return it.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from IUnknown:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from IUnknown:
 |  
 |  __metaclass__ = <class 'comtypes._cominterface_meta'>
 |      Metaclass for COM interfaces.  Automatically creates high level
 |      methods from COMMETHOD lists.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from _compointer_base:
 |  
 |  __cmp__(self, other)
 |      Compare pointers to COM interfaces.
 |  
 |  __del__(self, _debug=<bound method Logger.debug of <logging.Logger object>>)
 |      Release the COM refcount we own.
 |  
 |  __eq__(self, other)
 |  
 |  __hash__(self)
 |      Return the hash value of the pointer.
 |  
 |  __repr__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Class methods inherited from _compointer_base:
 |  
 |  from_param(klass, value) from comtypes._compointer_meta
 |      Convert 'value' into a COM pointer to the interface.
 |      
 |      This method accepts a COM pointer, or a CoClass instance
 |      which is QueryInterface()d.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from _compointer_base:
 |  
 |  value
 |      Return self.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from _ctypes._SimpleCData:
 |  
 |  __init__(...)
 |      x.__init__(...) initializes x; see help(type(x)) for signature
 |  
 |  __nonzero__(...)
 |      x.__nonzero__() <==> x != 0
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from _ctypes._SimpleCData:
 |  
 |  __new__ = <built-in method __new__ of _ctypes.PyCSimpleType object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from _ctypes._CData:
 |  
 |  __reduce__(...)
 |  
 |  __setstate__(...)

感谢名单

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案

import win32com.client
o = win32com.client.Dispatch("StepsToEveConverter");
o.StartvECU('c:\x.exe');

python的问题在于有太多的软件包,浪费了大量的时间,直到找到一个足够简单的软件包:(