在c ++中如何使用来自不同库的相同签名调用dll函数?

时间:2013-12-24 07:51:23

标签: c++ dll

在C#中我有非常简单的代码:

class MicexBridgeNativeAdapterWrapper
{
    [DllImport("MicexBridgeNativeAdapter.dll", CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity]
    public static extern bool Connect();
    ...


class CGateNativeAdapter
{

    [DllImport("CGateNativeAdapter.dll"), SuppressUnmanagedCodeSecurity]
    public static extern bool Connect();
    ....

然后我只需要调用MicexBridgeNativeAdapterWrapper.Connect()或/和CGateNativeAdapter.Connect()所以我从两个不同的dll导入了完全相同签名的函数并同时使用它们。

现在我想使用C ++中的这些dll。什么是上面代码的C ++模拟?

1 个答案:

答案 0 :(得分:4)

这也很简单。

HMODULE lib1 = LoadLibrary("MicexBridgeNativeAdapter.dll");
HMODULE lib2 = LoadLibrary("CGateNativeAdapter.dll");

typedef bool (*ConnectFnPtr)();

ConnectFnPtr ConnectFromLib1 = (ConnectFnPtr)GetProcAddress(lib1, "Connect");
ConnectFnPtr ConnectFromLib2 = (ConnectFnPtr)GetProcAddress(lib2, "Connect");

ConnectFromLib1(); // Calls Connect from MicexBridgeNativeAdapter
ConnectFromLib2(); // Calls Connect from CGateNativeAdapter