64位C#程序,使用LoadLibrary调用64位的C++编写的dll,失败
用代码块功能插入代码,请勿粘贴截图
namespace DllDemoUse
{
class NativeMethod
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
public static extern bool FreeLibrary(int hModule);
}
}
static void Main()
{
//1. 动态加载C++ Dll
int hModule = NativeMethod.LoadLibrary("myDll.dll");
if (hModule == 0) return;
//2. 读取函数指针
IntPtr intPtr = NativeMethod.GetProcAddress(hModule, "add");
//3. 将函数指针封装成委托
Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));
//4. 测试
int addResult = addFunction(1, 2);
···
}
delegate int Add(int a, int b);
运行结果及报错内容
intPtr 里面是个空的指针??
然后我试了32位环境的程序,
我建一个32位的dll,用32位的程序去调用是可以的。
如果我一定要用64位的程序去调用64位的dll的话,请教下如何实现?