我使用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