首页 文章

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

提问于
浏览
9

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

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

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

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

2 回答

  • 24

    在没有.lib文件的情况下访问裸DLL的唯一方法是使用LoadLibrary()显式加载DLL,获取要使用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;
    }
    

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

    ordinal hint RVA      name
          1    0 00001000 adler32
          2    1 00001350 adler32_combine
          3    2 00001510 compress
    (etc.)
    

    将名称放入DEF文件:

    EXPORTS
    adler32
    adler32_combine
    compress
    (etc.)
    

    现在制作LIB文件:

    lib /def:your.def /OUT:your.lib
    

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

  • 6

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

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

    希望有所帮助 .

相关问题