views:

421

answers:

3

Hi

I am in desperate need of help, I need to manage an application dependency in Visual Studio. The application links to a DLL only on a specific version of windows, lets say Windows 7. and on other environments, the DLL should not be loaded. How will I be able to achieve that using DLL Delay Loading as this topic is completely new to me and there isn't any good references online for this particular matter.

Regards

+1  A: 

Your project can specify that a dll it depends upon should but be loaded when needed, by specifying it in the Linker/Input/Delay Loaded DLLs field. This setting can be different for different build configurations.

xtofl
Thx for the reply man but can you giva an example or code snippet if its applicable :)
Red Serpent
That's the point of delay loading: it's a configuration thing, not a code thing.
xtofl
The wording appears wrong: When you say "The DLL you're depending upon should have been linked with the delay-load capacity", you're seem to be referring to the creation of that DLL from its constituent .obj's. The /DELAY flag is actually applied to the module that does the _importing_, not the _imported_ module. That's also the reason that you can delay-load Windows 2000 DLLs, which would have been built without this support.
MSalters
@MSalters: You're right: the first option is for the dll to support _unloading_. Corrected for that, thanks.
xtofl
A: 

Instead of using delay loading, have you considered using dynamic loading with LoadLibrary and GetProcAddress? This is likely to be simpler to use.

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.

PGNSI pGNSI;
SYSTEM_INFO si;

ZeroMemory(&si, sizeof(SYSTEM_INFO));

pGNSI = (PGNSI) GetProcAddress(
   GetModuleHandle(TEXT("kernel32.dll")), 
   "GetNativeSystemInfo");
if(NULL != pGNSI)
   pGNSI(&si);
else GetSystemInfo(&si);
1800 INFORMATION
How is it simpler to write code that can be automatically generated by the linker?
xtofl
its fine if you only have 1 function you wish to call ... otherwise its a plain bad choice, imo.
Goz
-1 - the support for delay loading in the linker is specifically aimed at preventing you having to deal LoadLibrary() and GetProcAddress()!
Bids
+4  A: 

MSDN has a pretty good description here.

Basically what you are doing is settign the DLL in question to be in the delay load section. It will then not load that DLL until you make a call to a function that is in that DLL.

Goz