Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Can DLL use functions from main application?

Back to Forum: Tech Off
  • User profile image
    artemfrolov

    Hello

    I am working on some kind of plugin functionality. Plugins are DLLs (dynamically _loaded_ libraries in that case). But my DLLs need to call functions from the main application (for some legacy reasons they are not in another DLL). At that point:
    * I declared these functions as __declspec(dllexport) in the complilation of the main application (let's say 'app')
    * I declared these functions as __declspec(dllimport) in the compilation of .dll
    * I got app.exp file

    Now the question: how I tell linker to actually use this .exp file during creation of .dll, in order not to get errors like that
    error LNK2019: unresolved external symbol __imp__someFunction referenced in function _someLibFunction

    I guess that should be doable because I was able to get that working in uhhh... other operating system and other compiler.

    Thank you
    /Artem

  • User profile image
    Sk4rlath

    From my experience, the way you let the DLL call the main app's functions is via callbacks and function pointers.

    // DLL
    void (*g_callback)();
    
    void SetCallbackFoo(void (*callback)())
    {
        g_callback = callback;
    }
    
    void DoSomething()
    {
        // do stuff
    
        // call callback
        g_callback();
    }
    
    // Main
    void SomeFunc()
    {
        SetCallback(DLLCallback);
    }
    
    void DLLCallback()
    {
        // do something
    }

    You can extend this sort of pattern to an object that you pass to the DLL or even a whole other DLL that the main program supplies. But for the most part I think these callbacks will get the job done.

  • User profile image
    artemfrolov

    Sk4rlath, thank you for your reply, but this is not exactly what I want.

    I would have to set up very large number of callbacks in that case. I want link.exe to do this job for me, that's why I asked how to use this .exp file.

  • User profile image
    Srdjan

    You can generate LIB when you build application by passing switch /IMPLIB:"Test.lib" to the linker (in Visual Studio, go to Project Properties > Linker > Advanced > Import Library and enter YourApp.lib.

    Then, in DLL, add it to Linker > Input > Additional Dependencies...

  • User profile image
    artemfrolov

    Thank you very much, Srdjan!! That's exactly what I needed to know and it works. Building import library _along_ with the main application was not obvious to me. I just thought that .exp file is needed :/

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.