Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dllimport in Microsoft Visual C++

I have a DLL and I would like to use some of its functions.

#include <iostream>

using namespace std;

extern "C" __declspec(dllimport) int Initialize(char* localPort, char* adminServerName, int rpcTimeout);


int main()
{
    int res = Initialize("7864", "6000@kabc", 10000);

    return 0;
}

I don't have the DLL's .lib file, so is there anyway I can link to it. One thing that comes to my mind is to use the LoadLibrary function and then use the GetProcAddress(). Is there any other way?

When I compile the following code

  • error LNK2019: unresolved external symbol _imp_Initialize referenced in function _main

  • fatal error LNK1120: 1 unresolved externals

I get the above errors

I'm using Windows and Visual Studio 2008

like image 948
Tux Avatar asked Oct 28 '10 08:10

Tux


3 Answers

From your phrasing it is pretty clear you are using Dev Studio some or other version.

In order to implicitly link against a dll, VC++ needs a .lib file, no questions. Without a .lib you can only explicitly load the dll using LoadLibrary and GetProcAddress.

Fortunately import libraries contain nothing other than the dll's exported symbols, so it is perfectly legitimate to simply create a dll project in VC++ that exports the identical symbols, build it, and then use the resulting .lib file to access the target dll.

The trick really is, getting the symbols right: Depending on how the original dll was created there might be some decoration to deal with.

"Initialize" could be exported from a dll in several ways when viewed with dependencywalker

  • "Initialize" - was either exported via a .DEF file, or via extern "C" __declspec(dllexport) int __cdecl Initialize(...
  • "_Initalize@16" - was exported using: extern "C" __declspec(dllexport) int __stdcall Initialize(...
  • ?Initialize@@YAHPADOH@Z" - `__declspec(dllexport) int Initialize(char*,char*,int);
  • ?Initialize@@YGHPADOH@Z" - `__declspec(dllexport) int __stdcall Initialize(char*,char*,int);

The problem really is the first case - if the calling convention isn't __cdecl (and most dll api's are actually __stdcall - all windows dlls are __stdcall) then you must use a .def file to export the undecorated names.

like image 109
Chris Becke Avatar answered Sep 21 '22 03:09

Chris Becke


A good way would be to reconstruct some header that would declare the functions you want to call. For doing this, you must know/discover their exact signatures.

If you have access to the strings command (available in cygwin), you might run it against the DLL. It will list the sequences of more than 4 consecutive printable characters terminated by a 0 (observe that it is really easy to implement, in the case where you do not want to install cygwin). Among these "strings", you will have garbage and the function signatures, mangled by the C++ compiler (see: http://en.wikipedia.org/wiki/Name_mangling). You will then have to unmangle the ones you are interested in, using undname.exe (assuming the DLL was built by Microsoft Visual; see: http://msdn.microsoft.com/en-us/library/5x49w699.aspx)

... You might also want to consider DependencyWalker.

like image 33
Noe Avatar answered Sep 19 '22 03:09

Noe


Under visual studio you can't link directly to DLL, you need import library.

So basically you have several options:

  1. Create import library form dll and link to it. There are some tools coming with MinGW that do the job, they may actually work for you, AFAIK there are also some tools for MSVC.

    This can be helpful: http://support.microsoft.com/kb/131313, not tested myself

  2. Use GetProcAddress
  3. Use gcc as it know to link directly with DLL's without import library :-)
like image 38
Artyom Avatar answered Sep 22 '22 03:09

Artyom