Opened 5 years ago

Closed 4 years ago

#7612 closed feature request (wontfix)

Automatically start the haskell runtime in DllMain on windows -shared

Reported by: schyler Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.1
Keywords: Cc:
Operating System: Windows Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


On Windows DllMain is called when a dll is loaded into a process via LoadLibrary (or even when it's loaded as a regular dependency).

Currently a stub, such as this, is required to be linked with all haskell dll compilations on windows that plan to have their functions called from non-haskell origins;

#include <windows.h>
#include <Rts.h>


static char* args[] = { "ghcDll", NULL };
                       /* N.B. argv arrays must end with NULL */
   ( HANDLE hModule
   , DWORD reason
   , void* reserved
  if (reason == DLL_PROCESS_ATTACH) {
      /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
      startupHaskell(1, args, __stginit_Adder);
      return TRUE;
  return TRUE;

see outdated documentation, but still mostly applicable:

I personally think this is a tad silly and could be easily improved. Ideally, DllMain should automatically detect if the runtime is running, and if not, automatically start it up so that the dll's ordinals can all be called freely. This could be a stub automatically added by ghc when the windows -shared is used.

An extension to this would be to add a pragma/flag to allow people to execute code inside DllMain (dllMain, a pragma, maybe?) after the runtime has started. One extremely easy way to do this would be to assume main as being :: Int -> IO Int where the first bound argument is the attach reason when windows -shared is used.

This would GREATLY improve how easy it is to call haskell dll's from other languages, even .net origins, etc.

Change History (5)

comment:1 Changed 5 years ago by simonpj

difficulty: Unknown

We'd LOVE anyone who knows about Windows and DLLs to improve the GHC experience on that platform. Thank you.

comment:2 Changed 5 years ago by simonmar

Resolution: wontfix
Status: newclosed

You're looking at a very old version of the docs, the latest is here: Note in particular that you cannot initialise Haskell from DllMain(), and the function to call is hs_init, not startupHaskell.

I'm not sure if it's possible to do what you want. You can't call hs_init from DllMain, and you can't create a thread that will call it later. So I'll close this ticket, but if you think it can be done please re-open and describe how.

comment:3 Changed 5 years ago by schyler

Resolution: wontfix
Status: closednew

I will do some personal investigation. This feature could be easily implemented but the hs_init routine would need to be audited such that it does not make any non-Kernel32 calls.

comment:4 Changed 5 years ago by simonmar

Status: newinfoneeded

I'll move the ticket into the infoneeded state, but I think you'll find that hs_init touches a lot of code (including creating threads), so it's not feasible to make it safe to use from DllMain.

comment:5 Changed 4 years ago by igloo

Resolution: wontfix
Status: infoneededclosed

I think it's best to just close this ticket, but of course please feel free to reopen it or file a new one if you find that this is possible after all.

Note: See TracTickets for help on using tickets.