Search code examples
winapivisual-c++dllimportdllexport

How to dll import and export HINSTANCE from a .dll in c++?


I am trying to get the HINSTANCE of a .dll, but I get this error:

error LNK2019: unresolved external symbol "__declspec(dllimport) struct HINSTANCE__ * m_instance" (__imp_?m_instance@@3PEAUHINSTANCE__@@EA) referenced in function...

This is the code of the .dll:

#include <Windows.h>

__declspec(dllexport) HINSTANCE m_instance;

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID)
{
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
        m_instance = (HINSTANCE)hModule;
    }

    return TRUE;
}

This is the code of the application:

__declspec(dllimport) HINSTANCE m_instance;

// use it for whatev

In summary, I need a way to get the HINSTANCE from my .dll into my .exe.


Solution

  • In summary, I need a way to get the HINSTANCE from my .dll into my .exe.

    But, why??? Once the .exe has loaded the .dll into its memory, the .exe already has access to the .dll's HINSTANCE, from the return value of either LoadLibrary/Ex() or GetModuleHandle(). So, there is never a need to have a .dll export its own HINSTANCE.