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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31708832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:00:53  来源:igfitidea点击:

How to reference a dll to Visual Studio without lib file

c++dllvisual-studio-2013

提问by Heather

I needed to add a third party library to my project, and they only supply a .dll file (no .lib)

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

I have added the dll to the project by going to the project Property Page under Common Properties -> References -> Add New Reference

我已通过转到 Common Properties -> References -> Add New Reference 下的项目属性页将 dll 添加到项目中

I can see the dll under the External Dependencies folder in the Solution Explorer, so I guess it was included correctly.

我可以在解决方案资源管理器的 External Dependencies 文件夹下看到 dll,所以我猜它被正确包含了。

But how do I reference the dll? When I try to add an instance variable (For example, MCC::iPort::ASCII iPort) to access the dll class, I get Error: name followed by '::' must be a class or namespace name, but I know thats the class name I can see it in the dll info under External Dependencies.

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

回答by Khouri Giordano

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.

访问没有 .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;
}

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

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

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

Put the names into a DEF file:

将名称放入 DEF 文件中:

EXPORTS
adler32
adler32_combine
compress
(etc.)

Now make the LIB file:

现在制作 LIB 文件:

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

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

对于名称已被修饰的情况,无论是通过 C++ 名称修饰还是 32 位stdcall调用约定,只需复制并粘贴dumpbin报告的任何名称、修饰和所有名称。

回答by Richard Vu

If you don't have a .libfile you can create one from the .dll:

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

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

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

Hope that helps.

希望有帮助。