且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-03 12:46:20

访问a的唯一方法没有.lib文件的裸DLL是使用 LoadLibrary() ,获取指向要使用 GetProcAddress() ,然后将这些指针转换为正确的函数签名。如果该库导出了C ++函数,则您将不得不传递给 GetProcAddress()的名称。您可以使用 dumpbin / exports your.dll列出导出的名称。

The only way to access a bare DLL without a .lib file is to load the DLL explicitly with LoadLibrary(), get pointers to the exported functions you want to access with GetProcAddress(), and then cast those pointers to the proper function signature. If the library exports C++ functions, the names you have to pass to GetProcAddress() will be mangled. You can list the exported names with 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 获取导出函数的列表:

As has been noted by others, a LIB file can be created. Get the list of exported functions from dumpbin /exports your.dll:

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

将名称放入DEF文件:

Put the names into a DEF file:

EXPORTS
adler32
adler32_combine
compress
(etc.)

现在制作LIB文件:

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

对于名称经过修饰的情况,可以使用C ++名称修饰或32位 stdcall 调用约定,只需复制并粘贴报告的任何名称 dumpbin ,修改和全部。

For cases where the name has been decorated, either by C++ name mangling or 32-bit stdcall calling convention, simply copy and paste whatever names dumpbin reported, mangling and all.