在 python 中将参数传递给 COM 对象

发布于 2025-01-06 18:18:17 字数 2617 浏览 2 评论 0原文

我正在编写一个 python 脚本来控制 Com 对象。官方文档是 C#、VB 和 C 语言的。 文档(VB中)如下:

Public Function GetDATA { 
ByVal vecRecords() As DATAType,
ByRef Time As String, 
optional ByVal filter1 As String, 
optional ByVal kind2 As Kind = KindAll, 
optional ByVal type1 As Types = TypeAll 
) As Long 

据我所知,第一个参数是返回值,并且在Python中正常返回。最后三个参数是枚举类型(转换为数字),但在任何情况下都是可选的。 我剩下的问题是我应该在“ByRef Time As String”中传递什么参数?

我的代码是:

>>> from win32com.client import Dispatch
>>> Obj = Dispatch("Service.Foo")
>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

我还跑了:

>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
 TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a bufferobject.

和:

>>> a= ("00000000","00000000",-1,-1)
>>> Obj.Function(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
MemoryError: CreatingSafeArray

retvalue 不是太大,所以我真的不明白最后一个。

系统: 视窗 7 64 位。 python 2.7.2 32位(从以管理员身份启动的cmd运行,COM对象需要它。)

有没有一种简单的方法来发现函数想要什么?

提前致谢 马布斯。

[编辑] 我给你一个有效的 C# 代码片段:

System.Array tempArray;
string lTime = "00000000";
Res = Obj.getDATA(out tempArray, ref lTime, "0", Obj.type1, Obj.type2);  

其中 Obj.type1 和 Obj.type2 是 Obj 本身的枚举。它们都转换为 -1(整数)。

I am writing a python script to control a Com object. The official documentation is in C#, VB and C.
the documentation (in VB) is as follows:

Public Function GetDATA { 
ByVal vecRecords() As DATAType,
ByRef Time As String, 
optional ByVal filter1 As String, 
optional ByVal kind2 As Kind = KindAll, 
optional ByVal type1 As Types = TypeAll 
) As Long 

As far as i can tell, the 1st argument is the return value and is returned as normal in python. Last three arguments are enum types (translated in to numbers) but are optional in any case.
I am left wit the question what argument do I pass in "ByRef Time As String"?

my code is:

>>> from win32com.client import Dispatch
>>> Obj = Dispatch("Service.Foo")
>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

I also ran:

>>> Obj.Function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
 TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a bufferobject.

and:

>>> a= ("00000000","00000000",-1,-1)
>>> Obj.Function(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\win32com\gen_py\48C77DB1-D9E0-45B8-9992-C9
2B047CC700x0x1x0.py", line 399, in Function 
    , LastTime, BNO, kind, madadType)
  File "D:\Python27\lib\site-packages\win32com\client\__init__.py", line 456, in
 _ApplyTypes_
     self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
MemoryError: CreatingSafeArray

The retvalue is not too big, so i really do not understand the last one.

system:
windows 7 64bit.
python 2.7.2 32 bit (running from cmd started as administrator, the COM object needs it.)

Is there a simple way to discover what the function wants?

Thanks in advance
Marbs.

[Edit]
I give you a working C# Code Snippet:

System.Array tempArray;
string lTime = "00000000";
Res = Obj.getDATA(out tempArray, ref lTime, "0", Obj.type1, Obj.type2);  

Where Obj.type1 and Obj.type2 are enums form the Obj it self. They both translate in to -1 (an int).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2025-01-13 18:18:17

如果您在困难的事情上遇到困难,请尝试做一些简单的事情,这可能会让您对困难问题有一些见解。

听起来您正在尝试从 Python 调用一个您并不完全熟悉的 COM API,并且该 API 使用比简单字符串和整数更复杂的数据类型。为什么不尝试用 C# 或 VB 等 .Net 语言编写一个简单的客户端,并了解该库期望的值是什么?甚至可能有一些这些语言的示例,您可以复制并运行。然后,您可以从一个工作示例将其转换为 Python。

您可能采取的另一个策略是使用 IronPython,它已经是 .Net 感知/友好的,并且可能会使您的开发更简单。

最后,另一种对 .Net 友好的 Pythonesque 语言是 Boo,我过去用它来构建一些为客户端提供简单的实用程序,因为您可以编写与 Python 非常相似的代码,但它构建为独立的 .DLL 或 .EXE。尽管该项目已经一年多没有任何明显的活动,但几年前当我开发一些 SOAP 客户端和服务器代码时,该语言对我非常有用。

If you are having trouble with something hard, try doing something easy that might give you some insights into the hard problem.

It sounds like you are trying to call a COM API that you are not entirely familiar with, from Python, and that the API makes use of datatypes that are more complex than simple strings and integers. Why not try writing a simple client in a .Net language like C# or VB, and get an idea of what values the library expects? There might even be some examples available in those languages that you could just copy and run. Then from a working example, you could convert that to Python.

Another tack you might take could be to use IronPython, which is already .Net-aware/friendly, and might make your development simpler.

Lastly, another Pythonesque language that is .Net-friendly is Boo, which I have used in the past to build some simple utilities for clients, since you can write code very similar to Python, but which builds to free-standing .DLLs or .EXEs. Although that project has not had any apparent activity in over a year, the language was very useful to me when I was developing some SOAP client and server code, a few years ago.

深巷少女 2025-01-13 18:18:17

当用VB编写时,并使用像解密ByRef Time As String,中的ByRef开关一样,来自我的问题,该函数更改了实际变量(就像c++中的指针) 。

该 COM 对象尝试更改一些不允许更改的内存。这就是为什么我在 python 中运行它时出现内存错误。

When writing in VB, and using the ByRef switch like in the decryption ByRef Time As String, from my question, the function changes the actual variable (like a pointer in c++).

This COM object tried changing some memory it was not allowed to change. That is why I got a memory error when running it in python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文