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 my DLL, here is an example of a function in my C# DLL:
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static float Test()
{
return (float)1.234;
}
[DllExport("Test1", CallingConvention = CallingConvention.Cdecl)]
public static string Test1()
{
return "123456789";
}
If I return an (int) it works very reliably in Python. Does anybody know what's going on? This is my Python code:
import ctypes
import sys
from ctypes import *
self.driver = ctypes.cdll.LoadLibrary(self.DLL)
a= self.driver.Test()
Eoin
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 therestype
attribute on the foreign function. Here is an example using libc (linux):Or a for a function that returns a C string:
This method should also work in your Windows environment.
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 adouble
from your c# code.