我使用NuGet包UnmanagedExports构建了一个C# DLL(MyTestDll):
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return "hi " + name + "!";
}
我通过ctypes DLL引入在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
添加到路径中,但没有帮助。你真的使用了双反斜杠吗?也许这可能是问题所在。只是猜测。 - Palec