Encapsulate a Windows message loop in a DLL

I would like to have a DLL with window creation and management code in such a way that the developer could just add the named.hh header and load the DLL to be able to instantiate the window.

#include "dllheader.h" 

void user_main();

main = user_main; // attach user main to the dll callback

int user_main() {
    Window *w = new Window();
}

on the dll side the code should look like

void (*main)() = NULL;

int WinMain(...) {
   if(main)
       main(); // call the user defined funcion
   while(!done) {
       if(messageWaiting()) {
           processMessage();
       }
   }

}

Why? because I want to expand the window wrapper and avoid the user typing the WinMain entry point. But the DLL project has the main DLL and the win32 project, which uses the DLL complaint if the linker does not find the winMain entry point.

Is there a known solution for this kind of arrangement?

+3
source share
2 answers

Win32 ( WinMain). , DLL, EXE. . , EXE.

, . , DLL - :

int WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow )
{
    return WrapperDllMain( hInstance, hPrev, lpCmdLine, nCmdShow, &user_main );
}

. DLL. ( ).

+3

WinMain() DLL. , , . , DLL, :

--- app ---

#include "dllheader.h" 

int user_main()
{
  ...
}

int WinMain(...)
{
  return dll_main(&user_main);
}


--- dll ---

int dll_main(void (*user_main)())
{
  if(user_main)
    user_main(); // call the user defined funcion

  while(!done)
  {
    if(messageWaiting())
    {
      processMessage();
    }
  }
  return 0;
}
+1

Source: https://habr.com/ru/post/1713111/


All Articles