How to reference a dll to Visual Studio without lib file
Asked Answered
L

2

25

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

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

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

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.

Landmark answered 29/7, 2015 at 18:53 Comment(1)
You cannot in c+±, without a header file, and a .lib file implementing the binding stubs, for the .dll.Mantis
L
47

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;
}

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.)

Put the names into a DEF file:

EXPORTS
adler32
adler32_combine
compress
(etc.)

Now make the LIB file:

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

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.

Lorelle answered 29/7, 2015 at 20:48 Comment(3)
it is possible to create import library from DLL, gcc can even link with dll directlyUnboned
@Artyom, what if dumpbin outputs names like _NWET@16 = _NWET@16, but in the .h file the name of the function is NWET? What do we write in .def file in this case? Thank you in advancePalish
For 32-bit stdcall functions, the underscore prefix and parameter stack usage suffix is added to the exported function name. So, you should put _NWET@16 in the def file.Lorelle
M
13

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

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

Mariken answered 29/7, 2015 at 22:23 Comment(2)
There is also (or used to be) IMPLIBBoong
Isn't this something that the compiler should be doing for you? This leaves me very confused what the point of the .lib is, if the information was in the .dll all along and the linker needs to look at the .dll anyway.Crippen

© 2022 - 2024 — McMap. All rights reserved.