c# - 在 python 中使用 ctypes 访问 C# dll 的方法

标签 c# python dll load ctypes

我想在我的 python 程序的关键部分实现 C# 代码以使其更快。它说(在 Python 文档和 this site 上)您可以按如下方式加载动态链接库(也就是 PyDocs):

cdll.LoadLibrary("your-dll-goes-here.dll")

这是我处理此功能的代码部分:

from ctypes import *
z = [0.0,0.0]
c = [LEFT+x*(RIGHT-LEFT)/self.size, UP+y*(DOWN-UP)/self.size]
M = 2.0

iterator = cdll.LoadLibrary("RECERCATOOLS.dll")

array_result = iterator.Program.ITERATE(z[0],z[1],c[0],c[1],self.iterations,M)

z = complex(array_result[0],array_result[1])
c = complex(array_result[2],array_result[3])
last_iteration = int(round(array_result[4]))

我使用的 RECERCATOOLS.dll 是这样的(C# 代码,不是 C 或 C++):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KarlsTools;

public class Program
{
    public static Array ITERATE(double z_r,double z_i,double c_r,
                        double c_i, int iterations, 
                        double limit)
    {
        Complex z = new Complex(z_r, z_i);
        Complex c = new Complex(c_r, c_i);

        for (double i = 1; Math.Round(i) <= iterations; i++)
        {
            z = Complex.Pow(z, 2) + c;
            if (Complex.Abs(z) < limit)
            {
                double[] numbers = new double[] { Complex.Real(z),
                                                  Complex.Imag(z),
                                                  Complex.Real(c),
                                                  Complex.Imag(c),
                                                  i};
                return numbers;
            }
        }
        double iter = iterations;
        double[] result = new double[]        { Complex.Real(z),
                                                  Complex.Imag(z),
                                                  Complex.Real(c),
                                                  Complex.Imag(c),
                                                  iter};
        return result;
    }
}

为了构建此 DLL,我在 Visual Studio 2010 项目上使用“构建”命令,该项目仅包含此文件和对“Karlstools”的引用,这是一个允许我使用复数的模块。

我不知道为什么,但是当我尝试运行我的 Python 代码时,它只是抛出一个异常:

    [...]
    array_result = iterator.Program.ITERATE(z[0],z[1],c[0],c[1],self.iterations,M)
  File "C:\Python32\lib\ctypes\__init__.py", line 353, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python32\lib\ctypes\__init__.py", line 358, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'Program' not found

我需要这方面的帮助,因为即使所有内容都设置为 public 并且函数设置为 static,即使我尝试访问它,它也会不断抛出异常直接调用函数而不指定“程序”类...我不知道问题出在哪里。

最佳答案

其实很简单。只需使用 NuGet 将“UnmanagedExports”包添加到您的 .Net 项目中。参见 https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports了解详情。

然后您可以直接导出,而无需执行 COM 层。这是示例 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}

然后您可以加载 dll 并在 Python 中调用公开的方法(适用于 2.7)

import ctypes
a = ctypes.cdll.LoadLibrary(source)
a.add(3, 5)

关于c# - 在 python 中使用 ctypes 访问 C# dll 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6718088/

相关文章:

python - 类型错误 : 'WebElement' object is not iterable error

python - Django生成 'WHERE ... BETWEEN ...'句?

c++ - 部署qt mysql应用程序

c# - 从 MainPage 访问 UserControl 中 ItemsSource 内的 Canvas

c# - 事务集成测试 ASP.Net Core 和 Entity Framework Core

python getattr 和 setattr 与 self.__dict__

delphi - 可以在 DLL 中使用 TDateTime 以供其他语言使用吗?

c# - 在构建时将所有依赖程序集合并到 Exe

c# - 从远程计算机获取驱动器信息

c# - Entity Framework 代码优先中的 DbUpdateConcurrencyException