首页 > 解决方案 > 如何在 ctypes 库 python 中获取我的 C# dll 的依赖关系?

问题描述

我想在 python 应用程序中使用 .net framework 4.7 运行 C# dll。我在 dll 中为我的方法使用 dllExport nuget,因为 ctypes 以这种方式访问​​我的方法。样本:

using System.Runtime.InteropServices;

namespace MyNameSpace
{
    public class MyClass
    {
        [DllExport("MyMethod", CallingConvention = CallingConvention.StdCall)]
        public static int MyMethod(int a,int b)
        {
            return a + b;
        }
    }
}

在蟒蛇中:

def myFunction():
  Dllpath = os.path.join("dll folder directory")
  os.add_dll_directory(Dllpath + r"myDll.dll")
  # WinDll used just windows os, in Linux is different
  lib = WinDLL(Dllpath + r"myDll.dll")
  lib.MyMethod(12,17)

这是工作。

但是当我在我的 dll 中使用其他库(dll)时,我得到了这个错误:

[WinError -532462766] Windows Error 0xe0434352

我猜这个错误是因为我的dll依赖问题。

如何解决此错误?

标签: c#python-3.xctypes.net-framework-version

解决方案


您也应该将“DllExport”放在您在所有依赖项中调用的所有函数之上。


推荐阅读