c++调用不同版本同名dll
时间: 2023-07-29 12:07:48 浏览: 68
如果你要在同一程序中调用不同版本的同名 DLL,可以在程序中使用 LoadLibrary 函数加载不同版本的 DLL,并使用 GetProcAddress 函数获取所需的函数地址。然后使用函数指针来调用所需的函数。以下是一个示例代码:
```
HINSTANCE hInstance1 = LoadLibrary("MyDll1.dll");
if (hInstance1 == NULL) {
// 处理加载 MyDll1.dll 失败的情况
}
HINSTANCE hInstance2 = LoadLibrary("MyDll2.dll");
if (hInstance2 == NULL) {
// 处理加载 MyDll2.dll 失败的情况
}
// 获取 MyFunction 函数地址
typedef int (*MyFunctionPtr)(int);
MyFunctionPtr MyFunction1 = (MyFunctionPtr)GetProcAddress(hInstance1, "MyFunction");
MyFunctionPtr MyFunction2 = (MyFunctionPtr)GetProcAddress(hInstance2, "MyFunction");
// 调用 MyFunction
int result1 = MyFunction1(1);
int result2 = MyFunction2(2);
// 卸载 DLL
FreeLibrary(hInstance1);
FreeLibrary(hInstance2);
```
在上面的示例中,我们首先使用 LoadLibrary 函数加载 MyDll1.dll 和 MyDll2.dll。然后,使用 GetProcAddress 函数获取名为 MyFunction 的函数地址,并将其转换为函数指针类型 MyFunctionPtr。最后,我们使用函数指针 MyFunction1 和 MyFunction2 分别调用 MyDll1.dll 和 MyDll2.dll 中的 MyFunction 函数。最后,我们使用 FreeLibrary 函数卸载加载的 DLL。