1

I'm new C++, I have a dll file called DiceInvaders.dll, in my project, I need to use this library, I'm using visual c++ 2010, I set the Linker Input as DiceInvaders.lib and DiceInvaders.dll, I also copied this dll file to my porject directory, I always got error in this line of code:

m_lib = LoadLibrary("DiceInvaders.dll");
assert(m_lib);

The error is assertion failure. How should I solve this? Thank you in advance.

Christophe
  • 68,716
  • 7
  • 72
  • 138
Tom
  • 385
  • 3
  • 7
  • 17

1 Answers1

7

First you cannot pass the DLL to the linker like you are, it is not a file type that the linker recognizes and cannot be linked that way. When you create the Diceinvaters.dll file the linker will create an import library with the same filename and the extension .lib. It appears this is already being done. That is the library file you should pass to the linker when building any application that uses it.

Second, the Diceinvaders.dll file must be accessible in the DLL search path. This varies slightly depending on which version of Windows you are using but is generally something like the following

  1. The directory the program was loaded from.
  2. The current working directory.
  3. The System directory.
  4. The Windows directory.
  5. The directories that are listed in the PATH environment variable.

Placing the DLL in your project directory is not going to be enough. Instead you should place it in the same directory as the EXE file(s) that have a dependency on it.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • So I just need to put the dll file in the directory as the exe file, nothing more is needed to do on visual c++? – Tom Dec 20 '14 at 16:21
  • Correct. As long as you are linking to the import library (DiceInvaders.lib) or using `LoadLibrary` placing it in the same directory as the `.exe` is the only thing left to do in order for it to work. – Captain Obvlious Dec 20 '14 at 16:24
  • Thanks, I got this dll file from my teacher, but there's no corresponding lib file, how can I generate a lib file from the dll file. – Tom Dec 20 '14 at 16:41
  • You cannot generate a lib file from the DLL file. But you don't need a lib file if you are using LoadLibrary to load the DLL. – ScottMcP-MVP Dec 20 '14 at 17:20
  • @CaptainObvlious Well whatdaya know! Thanks. – ScottMcP-MVP Dec 20 '14 at 18:11