none
using the /delayload linker switch with a custom DLL

    Question

  • Hello,

    I have a custom DLL that can be linked against correctly.

    I'm now trying to use the delay load feature of Visual Studio in order to load the DLL, but that does not work for me. I have therefore tried to compare with delay loading USER32.DLL, which works.

    I have the following code :

    #pragma comment (lib, "delayimp.lib")
    #pragma comment (linker, "/delayload:user32.dll")
    #pragma comment (linker, "/delayload:sample.dll")

    The build fails with unresolved external symbols, and I keep getting the warning :
    LNK4199: /DELAYLOAD:sample.dll ignored; no imports found from sample.dll

    I have tried compare using dumpbin /exports and cannot see any difference :

    From user32.lib and user32.dll
    _MessageBoxA@16  and MessageBoxA

    From sample.lib and sample.dll
    _a_sample_function@0 and a_sample_function

    The function is declared in my code as :
    extern "C" void __stdcall a_sample_function(void);

    I don't know what I'm missing.
    Can anyboy help me?

    Cheers.
    Maxime.
    Tuesday, October 04, 2005 2:25 PM

Answers

  • Maxime: your function also needs to be marked with __declspec(dllexport).
    Tuesday, October 04, 2005 2:57 PM
    Moderator

All replies

  • Maxime: your function also needs to be marked with __declspec(dllexport).
    Tuesday, October 04, 2005 2:57 PM
    Moderator
  • Well,

    That's how it is declared when I compile the very DLL I want delay loaded (sample.dll):
    --- snip sample.cpp ---
    extern "C" __declspec(dllexport) void __stdcall a_sample_function(void)
    { ...}
    --- snip sample.cpp ---

    I also use a .def file that transforms the exported symbol, to better match what I observe from user32.dll :
    --- snip sample.def ---
    EXPORTS
        a_sample_function
    --- snip sample.def ---

    But in order to build the DLL (or EXE) that performs the delay loading, I wrote the declaration like shown in my original post:
    --- snip main.cpp ---
    #pragma comment (lib, "delayimp.lib")
    #pragma comment (linker, "/delayload:sample.dll")
    extern "C" void __stdcall a_sample_function(void);
    --- snip main.cpp

    Do you mean that I also have to have __declspec(dllexport) in the declaration above?

    It used to work all right, albeit without delay loading, when the lines read:
    --- snip main.cpp ---
    #pragma comment (lib, "sample.lib")
    extern "C" void __stdcall a_sample_function(void);
    --- snip main.cpp

    Tuesday, October 04, 2005 7:32 PM
  • On the input side you need to use:

    extern "C" __declspec(dllimport) void __stdcall a_sample_function();
    Tuesday, October 04, 2005 7:48 PM
    Moderator
  • All right.

    That didn't do it. But I found the solution:

    The trick is to have both the regular linking and the delay load linker switch. Otherwise, it does not work. So, in my case, the solution was to declare on the input side :

    #pragma comment (lib, "sample.lib")
    #pragma comment (lib, "delayimp.lib")
    #pragma comment (linker, "/delayload:sample.dll")
    extern "C" void __stdcall a_sample_function();

    I'm sorry for the confusion.
    Thanks for taking the time to answer what was probably a newbie question.

    Maxime.
    Wednesday, October 05, 2005 5:49 AM