在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++模拟是什么?

最佳答案

这也很简单。

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

08-16 10:20