3

I've not done any windows programming for a few years now and I'm a bit rusty on how to use dllimport. I have something along the lines of:

extern "C"
{
__declspec(dllimport) int myFunct();
}

int main()
{
   cout<<myFunct();
}

I get 2 unresolved externals linker errors and a warning that /DELAYLOAD:myLib.dll was ignored because no imports from it were found. What am I missing here? I thought that I can add the DLL to the delay load path, and the linker will figure to use it for the dll import??

This is a 3rd party DLL which comes without .h or .lib file.

ventsyv
  • 3,316
  • 3
  • 27
  • 49

5 Answers5

3

If you want to link DLL at runtime, you don't need to import symbols at all.

Just declare a function pointer type, then LoadLibrary()/LoadLibraryEx(), and finally retrieve function pointer from GetProcAddress().

Example (error handling is ommitted for brevity):

typedef int (*MyFunct_t)();
auto myDLL = LoadLibrary("mydll.dll");
auto MyFunct = (MyFunct_t)GetProcAddress(myDLL, "MyFunct");
MyFunct();

(this code is only to show general procedure, it was never compiled and tested and could contain typos and syntax errors, feel free to edit this post to fix them)

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
2

__declspec(dllimport) tells the compiler that a symbol is imported, but it doesn't tell where from. To specify that, you need to link with the import library that accompanies the DLL. If you don't have one, you can have the linker generate it for you by providing a definition file.

ach
  • 2,314
  • 1
  • 13
  • 23
  • I thought I can tell the compiler to delay load it at run time? I can do it programatically by calling LoadLibrary but I thought there is a way to get the compiler to do the work for me? – ventsyv Nov 17 '14 at 06:04
  • @ventsyv, `/DELAYLOAD` is an option of linker, not compiler. There is no conflict between using a .def-file and specifying `/DELAYLOAD`. – ach Nov 17 '14 at 06:10
1

I get 2 unresolved externals linker errors and a warning that /DELAYLOAD:myLib.dll...

This is a 3rd party DLL which comes without .h or .lib file.

Try adding the following to your source file:

#pragma comment(lib, "myLib")
extern int myFunct();

The pragma comment places an entry for the library in the object file. The second provides a declaration for the function.

From your comments, you might also be talking about /INCLUDE (Force Symbol References). You can place that in a source file with pragma comment, too.

If needed, you can create an import lib. See How To Create Import Libraries Without .OBJs or Source and How to generate an import library (LIB-file) from a DLL?.

Or, use LoadLibrary and GetrocAddress.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • The pragma you propose serves quite another purpose: it instructs the linker to add the specified static library into linkage. It might be used to conveniently link with the DLL import library without bothering to specify it on the linker command line - if OP had one. – ach Nov 17 '14 at 06:18
0

You can use, dllimport like this,

extern "C"
{
__declspec(dllimport) int myFunct();
}

Just do remember myFunct should be dllexport from where you have defined it.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
0

I did it this way:

1) Added a file MyLibDll.h to project

#pragma once

#if MYLIB_EXPORTS 
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif

2) Use this in library header files as

#include "MyLibDll.h"

    class MYLIB_API myClass
{

}

This can be used with methods to.

3) Added compiler option /D MYLIB_EXPORTS in .dll project.

Not sure if this is the best solution, but it worked for me.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • It's a 3rd party dll which has no .h or .lib files. – ventsyv Nov 17 '14 at 05:46
  • @ventsyv What IDE/compiler do you use? AFAIK, you need at least a .lib file to use extern .dll in Visual Studio. Also, my answer is totally pointless in this case. – Pavel Oganesyan Nov 17 '14 at 05:50
  • VisualStudio 2012. I don't need a .lib, I've done this before but can't remember the linker options. – ventsyv Nov 17 '14 at 05:53
  • @ventsyv - if these are additional requirements, then you should add them to your question. Otherwise, Pavel answered your question. – jww Nov 17 '14 at 05:59