gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:10 30 4
gpt4 key购买 nike

有人写了一个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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com