简体   繁体   中英

How do I pass a ulong type argument to a C# function from Python?

From Python, I am calling a C# function which is in a dll. This is done by "python.net". Most of the functions can be called correctly in Python. However, there is one function I have a problem because of data type on the argument.

In C#, there is a function, that takes an unsigned long type:

bool SetValueAsInt64U(ulong rValue);

In Python, I am calling it with

SetValueAsInt64U(22);

This gives the error message: TypeError: No method matches given arguments for SetValueAsInt64U:(<class 'int'>)

I also tried to use the ctypes, and call the function like:

SetValueAsInt64U(c_ulong(22));

This gives a similar error: TypeError: No method matches given arguments for SetValueAsInt64U: (<class 'ctypes.c_ulong'>)

For more information, I am showing a correct call. In C#, there is another function with signed argument:

bool SetValueAsInt64(long rValue)

In Python, I can call the function without any error message, for example:

SetValueAsInt64(-5);

So the question is how can I call the C# function with the unsigned type of argument from Python?

在这种情况下,我要做的就是重载该方法以接受 int 参数,如果达到最大 int 范围 2,147,483,647,python 会将其解析为 ulong。

Use numpy.

import numpy as np

dp.SetValueAsInt64U(np.uint64(22));

This approach does not need to change C# code. It is a solution in Python code.

The numpy data types are: https://numpy.org/devdocs/user/basics.types.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM