【发布时间】:2015-10-16 16:12:29
【问题描述】:
我已经使用 NuGet 包 UnmanagedExports 构建了一个 C# DLL (MyTestDll):
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return "hi " + name + "!";
}
我通过 ctypes DLL import 从 Python 使用它:
path = "C:\\Temp\\Test"
os.chdir(path)
dll = ctypes.WinDLL("MyTestDll.dll")
f = dll.Test
f.restype = ctypes.c_char_p
print f('qqq')
这只是一个幻想,它有效。
然后,我又添加了一个 DLL (NoSenseDll):
namespace NoSenseDll
{
public class NoSenseClass
{
public static int Sum(int a, int b)
{
return a + b;
}
}
}
我开始使用这个 NoSenseDll 来实现 MyTestDll:
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return NoSenseDll.NoSenseClass.Sum(4, 5).ToString();
}
不幸的是,它不起作用。 Python 说:
WindowsError: [Error -532462766] Windows Error 0xE043435
我尝试将 C:\\Temp\\Test 添加到路径中,但这没有帮助。
我写了一个 C++ 测试:
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <string>
#include "WinBase.h"
typedef char*(__stdcall *f_funci)(const char*);
int _tmain(int argc, _TCHAR* argv[])
{
int t;
std::string s = "C:\\Temp\\Test\\MyTestDll.dll";
HINSTANCE hGetProcIDDLL = LoadLibrary(std::wstring(s.begin(), s.end()).c_str());
f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "Test");
std::cout << "funci() returned " << funci(std::string("qqq").c_str()) << std::endl;
std::cin >> t;
return EXIT_SUCCESS;
}
如果第二个 DLL (NoSenseDll) 与 C++ 可执行文件位于同一文件夹中,则它可以工作。如果我只是将 NoSenseDll 文件夹添加到 PATH,它不起作用。
【问题讨论】:
-
我尝试将
C:\\Temp\\Test添加到路径中,但没有帮助。 你真的使用了双反斜杠吗?也许这可能是问题所在。只是猜测。 -
我已尽力编辑您的问答,使其更易于理解,并将 NoSenceDll 修复为 NoSenseDll。感谢您为本问答所做的努力!
标签: c# python .net dll unmanaged