I am using Robert Giesecke's DLLExport library to allow me to export a c# dll that can be accessed by a python application. I have figured out how to call for basic types, such as integers, double and strings, but I also need to return arrays of strings or integers back from the c# dll to python.
I tried defining it like:
[DllExport("Test7", System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static int[] Test7()
{
Random rnd = new Random();
int[] info = new int[10];
for (int i = 0; i < 10; i++)
{
info[i] = Convert.ToInt32(Math.Round(Convert.ToDecimal(rnd.Next()), 0));
}
return info;
}
I also tried to use the example from the "Unmanaged Exports with Arrays" topic, with my c# code being:
[DllExport("Test8", System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static void Test8(int length, out IntPtr info)
{
Random rnd = new Random();
int[] valList = new int[length];
for (int i = 0; i < length; i++)
{
valList[length] = Convert.ToInt32(Math.Round(Convert.ToDecimal(rnd.Next()), 0));
}
info = Marshal.AllocHGlobal(valList.Length * Marshal.SizeOf(typeof(int)));
Marshal.Copy(valList, 0, info, length);
}
The basic test function is to create an integer array of a length provided by the function, and to return the value back to the caller.
On the python side, I have a simple code to test:
import os
import ctypes
import numpy as np
output = ctypes.CDLL(".\\pythontstl.dll").Test7(10)
and I tried
array_holder = ctypes.ARRAY()
output = ctypes.CDLL(".\\pythontst1.dll").Test8(10,array_holder)
In both cases, I get an OSError: [WinError -532462766] Windows Error 0xe0434352. I am learning python so it may be a syntax or call that I might be missing, but the output = ctypes.CDLL(".\pythontst1.dll").Test6('hello') code works and returns a string (along with the other test functions I have for the primitives).
User contributions licensed under CC BY-SA 3.0