C#Unmanaged将数组导出到python

时间:2017-12-12 00:47:24

标签: c# python unmanaged

我正在使用Robert Giesecke的DLLExport库来允许我导出一个可由python应用程序访问的c#dll。我已经弄清楚如何调用基本类型,如整数,双精度和字符串,但我还需要将字符串或整数数组从c#dll返回到python。

我尝试将其定义为:

[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; 
    }

我还尝试使用&#34; Unmanaged Exports with Arrays&#34;主题,我的c#代码是:

[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);
        }

基本测试函数是创建一个由函数提供的长度的整数数组,并将值返回给调用者。

在python方面,我有一个简单的代码来测试:

import os
import ctypes
import numpy as np
output = ctypes.CDLL(".\\pythontstl.dll").Test7(10)

我试过

  array_holder = ctypes.ARRAY()
  output = ctypes.CDLL(".\\pythontst1.dll").Test8(10,array_holder)

在这两种情况下,我都会遇到OSError:[WinError -532462766] Windows错误0xe0434352。我正在学习python,所以它可能是我可能缺少的语法或调用,但输出= ctypes.CDLL(&#34;。\ pythontst1.dll&#34;)。测试6(&#39;你好&#39; )代码工作并返回一个字符串(以及我对基元的其他测试函数)。

0 个答案:

没有答案