我使用NuGet包UnmanagedExports建一个C#DLL(MyTestDll):加载C#DLL
path = "C:\\Temp\\Test"
os.chdir(path)
dll = ctypes.WinDLL("MyTestDll.dll")
f = dll.Test
f.restype = ctypes.c_char_p
print f('qqq')
:
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public static string Test(string name)
{
return "hi " + name + "!";
}
我通过ctypes的DLL导入使用它在Python
这只是一个幻想,它的作品。
然后,我增加了一个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();
}
不幸的是,这是行不通的。蟒蛇说:
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
我尽我所能编辑您的问答以便更容易理解,并且我将NoSen * c * eDll修正为NoSen * s * eDll。感谢您为此问答所做的努力! – Palec