如何在没有lib文件的情况下将dll引用到Visual Studio

Hea*_*her 15 c++ dll visual-studio-2013

我需要在我的项目中添加第三方库,它们只提供.dll文件(没有.lib)

我已经通过转到Common Properties - > References - > Add New Reference下的项目Property Page将dll添加到项目中

我可以在解决方案资源管理器中看到外部依赖项文件夹下的dll,所以我猜它已被正确包含.

但是我如何引用dll?当我尝试添加一个实例变量(例如,MCC :: iPort :: ASCII iPort)来访问dll类时,我得到错误:名称后跟'::'必须是类或命名空间名称,但我知道那些我可以在外部依赖项下的dll信息中看到它的类名.

Kho*_*ano 33

在没有.lib文件的情况下访问裸DLL的唯一方法是显式加载DLL LoadLibrary(),获取要访问的导出函数的指针GetProcAddress(),然后将这些指针强制转换为正确的函数签名.如果库导出C++函数,则必须传递的名称GetProcAddress()将被破坏.您可以列出导出的名称dumpbin /exports your.dll.

extern "C" {
    typedef int (*the_func_ptr)( int param1, float param2 );
}

int main()
{
    auto hdl = LoadLibraryA( "SomeLibrary.dll" );
    if (hdl)
    {
        auto the_func = reinterpret_cast< the_func_ptr >( GetProcAddress( hdl, "the_func" ) );
        if (the_func)
            printf( "%d\n", the_func( 17, 43.7f ) );
        else
            printf( "no function\n" );

        FreeLibrary( hdl );
    }
    else
        printf( "no library\n" );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如其他人所指出的,可以创建LIB文件.从dumpbin /exports your.dll以下位置获取导出函数的列表:

ordinal hint RVA      name
      1    0 00001000 adler32
      2    1 00001350 adler32_combine
      3    2 00001510 compress
(etc.)
Run Code Online (Sandbox Code Playgroud)

将名称放入DEF文件:

EXPORTS
adler32
adler32_combine
compress
(etc.)
Run Code Online (Sandbox Code Playgroud)

现在制作LIB文件:

lib /def:your.def /OUT:your.lib
Run Code Online (Sandbox Code Playgroud)

对于已经通过C++名称修改或32位stdcall调用约定来装饰名称的情况,只需复制并粘贴dumpbin报告的任何名称,修改和所有.


小智 7

如果您没有.lib文件,可以从以下位置创建一个文件.dll:

https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/

希望有所帮助.