How to reference dll on Visual Studio without lib file

I never used a dll before it burned with me ...

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

I added the DLL to the project by going to the project properties page under General Properties β†’ Links β†’ Add New Link

I see a DLL in the External Dependencies folder in Solution Explorer, so I think it was included correctly.

But how do I refer to a 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 "::" should be the name of the class or namespace, but I know what is the class name that I see in the DLL information in the External Dependencies section.

+7
c ++ dll visual-studio-2013
source share
2 answers

The only way to access a bare DLL without a .lib file is to load the DLL explicitly using LoadLibrary() , get the pointers to the exported functions that you want to access with GetProcAddress() , and then put these pointers on the corresponding function signature. If the library exports C ++ functions, the names you must pass to GetProcAddress() will be garbled. You can specify the exported 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 others have noted, 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 in the DEF file:

 EXPORTS adler32 adler32_combine compress (etc.) 

Now create the LIB file:

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

In cases where the name was decorated, either using the C ++ language name, or using the stdcall 32-bit calling stdcall , just copy and paste any dumpbin names that will be displayed, swing and that's it.

+16
source share

If you do not have a .lib file, you can create it from a .dll :

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

Hope this helps.

+5
source share

All Articles