访问DLL中的重载函数

Pas*_*sha 3 c++ winapi direct2d

Direct2D的系统库提供4个重载版本的D2D1CreateFactory功能.现在假设我正在动态加载Direct2D库,并使用GetProcAddress系统调用获取指向CreateFactory函数的指针.将返回4个重载函数中的哪一个?有没有办法明确指定我需要哪个功能?这是动态加载与静态链接的缺点,因为某些重载函数将无法访问吗?

HMODULE hDllD2D = ::LoadLibraryExA("d2d1.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32);
FARPROC fnCreateFactory = ::GetProcAddress(hDllD2D, "D2D1CreateFactory");
// What should be the call signature of `fnCreateFactory` ?
Run Code Online (Sandbox Code Playgroud)

Ada*_*eld 5

DLL中的函数是最常用的函数D2D1CreateFactory(D2D1_FACTORY_TYPE,REFIID,D2D1_FACTORY_OPTIONS*,void**) function.如果您查看声明中的内容d2d1.h,您将看到声明该函数,而其他重载只是标题中的内联函数,它调用通用函数:

#ifndef D2D_USE_C_DEFINITIONS

inline
HRESULT
D2D1CreateFactory(
    __in D2D1_FACTORY_TYPE factoryType,
    __in REFIID riid,
    __out void **factory
    )
{
    return 
        D2D1CreateFactory(
            factoryType,
            riid,
            NULL,
            factory);
}


template<class Factory>
HRESULT
D2D1CreateFactory(
    __in D2D1_FACTORY_TYPE factoryType,
    __out Factory **factory
    )
{
    return
        D2D1CreateFactory(
            factoryType,
            __uuidof(Factory),
            reinterpret_cast<void **>(factory));
}

template<class Factory>
HRESULT
D2D1CreateFactory(
    __in D2D1_FACTORY_TYPE factoryType,
    __in CONST D2D1_FACTORY_OPTIONS &factoryOptions,
    __out Factory **ppFactory
    )
{
    return
        D2D1CreateFactory(
            factoryType,            
            __uuidof(Factory),
            &factoryOptions,
            reinterpret_cast<void **>(ppFactory));
}

#endif // #ifndef D2D_USE_C_DEFINITIONS
Run Code Online (Sandbox Code Playgroud)