So this got asked [here](https://forum.nim-lang.org/t/3190) already. How to 
define a DllMain Entrypoint without emitting raw c code? My example doesn't 
works. Looking at the raw cache .c file it also doesn't looks correct at all.
    
    
    import winim/inc/[windef, wincon]
    
    proc mainThread(hModule: HINSTANCE) =
      AllocConsole()
      discard stdout.reopen("CONOUT$", fmWrite)
      echo "Hello World"
    
    proc entryPoint(hinstDLL: HINSTANCE, fwdreason: DWORD, lpvReserved: 
LPVOID): BOOL {.stdcall, exportc: "DllMain".} =
      if fwdreason == DLL_PROCESS_ATTACH:
        var t: Thread[HINSTANCE]
        t.createThread(mainThread, hinstDLL)
      
      result = TRUE
    
    
    Run

This example works. But I'm not sure what risks it would be to spawn a nim 
function in a raw windows thread.
    
    
    proc mainThread {.exportc.} =
      echo "Hello World"
    
    {.emit:
      ["""
      BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID 
lpvReserved) {
        switch (fwdreason) {
        case DLL_PROCESS_ATTACH:
          NimMain();
          CloseHandle(
            CreateThread(0, 0, (LPTHREAD_START_ROUTINE)mainThread, hinstDLL, 0, 
0)
          );
        case DLL_PROCESS_DETACH:
          break;
        }
        return 1;
      }
      """]
    .}
    
    Run

Reply via email to