Problem exporting Haskell to C via a DLL in GHC 6.6
Brian Hulley
brianh at metamilk.com
Tue Feb 6 17:39:44 EST 2007
SevenThunders wrote:
> Brian Hulley wrote:
>> SevenThunders wrote:
>>> DllMain
>>> if (reason == DLL_PROCESS_DETACH) {
>>> shutdownHaskell();
>>> return TRUE;
>>> }
>> The above *may* be the problem: it is unsafe to do anything in
>> DllMain that...
>>
>> 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().
>
> So in your scheme the Begin() would call the startupHaskell function
> and the End() call the shutdown Haskell?
Yes.
> Or would the Begin initiate
> the linking to the specific DLL using LoadLibrary?
Since Begin would be a function exported by the DLL, Windows would ensure
that the DLL was loaded when it is first called from your application if it
was not already loaded so there would be no need for an explicit call to
LoadLibrary.
> and then End
> specifically unload the library; or both?
I wouldn't bother explicitly unloading the library - I'd leave this up to
Windows. The importance of using an End function is that you can ensure that
the call to shutdown Haskell happens at a time when all DLLs needed by
Haskell are still available, whereas using DllMain to do the shutdown call
is dangerous because DllMain may be invoked when some DLL necessary for
Haskell to shutdown has already been unloaded by Windows.
Ideally there would also be some way to call Begin/End when using the DLL
from Matlab but I don't know anything about Matlab so can't help with this.
A quick hack to enable you to use the DLL safely from your application
(using Begin/End) as well as unsafely from Matlab (relying on DllMain to
shutdown Haskell), would just be to have a flag in the DLL to keep track of
whether you've already shut Haskell down. Then in the case for
process_detach you could just check this so that shutdown would only be
called from DllMain as a last resort.
>
> 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?
I don't know - hopefully someone else may be able to answer this question.
Best regards, Brian.
--
http://www.metamilk.com
More information about the Glasgow-haskell-users
mailing list