• Advertisement
Sign in to follow this  

dll import and export

This topic is 3956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There's something I don't understand with __declspec(dllimport) and __declspec(dllexport). Let's say I've a DLL named Utils.dll utils.h
extern "C" __declspec(dllexport) int SomeFunction ();

utils.cpp
int SomeFunction ()
{
  return 1;
}

then I've another DLL named DLLThatUseUtils.dll DLLThatUseUtils.h
extern "C" __declspec(dllimport) int SomeFunction ();

Now, inside DLLThatUseUtils I should be able to call SomeFunction() beacause I've imported but: 1) If i don't link Utils.dll with DLLThatUseUtils.dll I get the "unresolved external symbol" 2) If i link...well if I link which is the purpose of __dllimport ? If I staticaly link the 2 dlls, then there's no need to __dllimport SomeFunction, isnt'it ? Thank you

Share this post


Link to post
Share on other sites
Advertisement
If you want to load the dll during runtime, for a Plugin system for example you must use LoadLibrary and GetProcAddress.

You need to tell the linker that the function is not in one of DLLThatUseUtils object files, and you need to tell it in which dll it is, thus the need of __declspec(dllimport) and the need to link against the dll.

If you are bothered with the need to use __declspec and you're not doing something that requires runtime dll loading, you can use a .def file here and here.

Share this post


Link to post
Share on other sites
Thank you for reply but I still don't get it.
BTW, I'm not talking about runtime loading the dll (which is something I understand).

I'm talking about the use/mean of __desclspec(dllimport).

If I staticaly link a DLL with another DLL or with and exe, there is no need to dllimport any functions.
All I need is __dllexport (or to use a .def file), include the right .h and the compiler will work just fine.
So I still don't understand the use of __declspec(dllimport)

Share this post


Link to post
Share on other sites
Quote:
Original post by Giallanon
If I staticaly link a DLL with another DLL or with and exe, there is no need to dllimport any functions.
All I need is __dllexport (or to use a .def file), include the right .h and the compiler will work just fine.
So I still don't understand the use of __declspec(dllimport)


I think you're either misunderstanding something here or using the wrong terminology, you cannot by definition "statically link to a dll", you can link to a static library (.lib) or link to a dynamic library (.dll), so "statically linking to a dll" is an oxymoron. The .lib file you link to when linking to a dll does not contain any object code and is just a helper to get to the dll.

I don't know exactly what are you doing here, as far as I understand it, you do need __declspec(dllimport) to let the linker know that it should look for the function or resource address during the program loading stage, rather than hardcode it to a known piece of object code even if linking against the "helper" lib file rather than the static one.

In short, __declspec(dllimport) is needed to let the dynamic linker know that it should look for the resource in an external PE file (exe/dll), if you link to a static library, the object code for the function is internally copied into the executable, so no need for __declspec(dllimport).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement