Problem exporting Haskell to C via a DLL in GHC 6.6

SevenThunders mattcbro at
Tue Feb 6 13:52:05 EST 2007

Brian Hulley wrote:
> SevenThunders wrote:
>> Before I post this as a bug, I thought I'd check to make sure I'm not
>> doing something wrong.
>> DllMain
>>   ( 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_Bad);
>>      return TRUE;
>>  }
>>  if (reason == DLL_PROCESS_DETACH) {
>>        shutdownHaskell();
>>        return TRUE;
>>  }
>>  return TRUE;
>> }
> The above *may* be the problem: it is unsafe to do anything in DllMain
> that 
> may involve loading a DLL, (which therefore includes a lot of the standard 
> platform sdk functions, some of which Haskell may need to use to 
> start/sthurdown) because the order in which DllMain is called when Windows 
> loads/unloads DLLs is undefined - see platform sdk docs for more info.
> Instead of trying to start/shutdown Haskell from DllMain, I'd export a 
> Begin() and End() function from the DLL and explicitly call these from
> your 
> application's main().
> Hope this helps,
> Brian.
> -- 
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at

Interesting.  Perhaps I should try that.  The problem is that I found I had
to add the explicit shutdown in the Dll when calling Haskell from Matlab! 
Apparently it would cause Matlab to crash after using the DLL. 

 So in your scheme the Begin() would call the startupHaskell function and
the End() call the shutdown Haskell?  Or would the Begin initiate the
linking to the specific DLL using LoadLibrary? and then End specifically
unload the library; or both?

Another question I have is, is it possible to create a statically linked
Haskell library that can be linked using MS VC tools?  Also I must say I am
a bit confused about the use of the routine __stginit_Bad.  Suppose I had
multiple Haskell modules each with their own functions to export.  Which
__stginit_??? routine do I use?

Thanks for the help.

View this message in context:
Sent from the Haskell - Glasgow-haskell-users mailing list archive at

More information about the Glasgow-haskell-users mailing list