C# Return Type Issue
I have a C# Class Library DLL that I call from Python. No matter what I do, Python thinks the return type is (int) I am using RGiesecke.DllExport to export the static functions in
Solution 1:
Yes, that is correct; as explained in the ctypes documentation, it is assumed that all functions return int
s. You can override this assumption by setting the restype
attribute on the foreign function. Here is an example using libc (linux):
>>>import ctypes>>>libc = ctypes.cdll.LoadLibrary("libc.so.6")>>>libc.strtof
<_FuncPtr object at 0x7fe20504dd50>
>>>libc.strtof('1.234', None)
1962934
>>>libc.strtof.restype = ctypes.c_float>>>libc.strtof('1.234', None)
1.2339999675750732
>>>type(libc.strtof('1.234', None))
<type 'float'>
Or a for a function that returns a C string:
>>>libc.strfry
<_FuncPtr object at 0x7f2a66f68050>
>>>libc.strfry('hello')
1727819364
>>>libc.strfry.restype = ctypes.c_char_p>>>libc.strfry('hello')
'llheo'
>>>libc.strfry('hello')
'leohl'
>>>libc.strfry('hello')
'olehl'
This method should also work in your Windows environment.
Solution 2:
Just because the datatypes are given the same name doesn't mean that they are actually the same. It sounds as it python is expecting a different structure for what it calls a "float"
.
This previous answer may be of use, and this answer states that a python float
is a c# double
; so my advice would be to try returning a double
from your c# code.
Post a Comment for "C# Return Type Issue"