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

标签 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__(...)

谢谢

最佳答案

好的,我找到了解决方案

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

Python 的问题是有太多的包,并且浪费了很多时间直到找到一个工作足够简单的包:(

关于python - 如何从 Python 中的 C# DLL 调用 Windows COM 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438745/

相关文章:

c++ - 如何为 SENS 事件注册接收器对象

python - XMLRunner - "unicode object has no attribute ' 构建时写入“”

OSX 上的 Python 请求

c++ - COM:返回字符串的拷贝,还是实际值?

c++ - 在 Windows 环境中从 C 或 C++ 访问 COM 接口(interface)

c# - COM 互操作 : What should I do to make a C# property usable as a VARIANT from VBA

c++ - 在 COM : should I call AddRef after CoCreateInstance?

python - 在不丢失精度的情况下将 `float` 类型转换为 `str`

python - 在 Python 中读写内存

python - 在干草堆中搜索几个等长的针(Python)