
Better still, could you add a section to that page about DLLs and Excel? That'd be useful for others.
Simon
From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users-bounces@haskell.org] On Behalf Of Olivier Boudry
Sent: 30 November 2007 18:09
To: glasgow-haskell-users@haskell.org
Subject: Re: GHC generated dll makes Excel crash on exit.
Sorry if I'm talking to myself, but I found a solution and thought it could be interesting for other people who need to call GHC created DLLs from Excel.
The solution is based on the information found in : http://haskell.org/haskellwiki/GHC/Using_the_FFI#Debugging_Haskell_DLLs.
As suggested, I added two extra functions in my dllMain.c file (adder_Begin, adder_End) and removed the startupHaskell call from the dllMain function.
adder_Begin contains the startupHaskell call and adder_End the shutdownHaskell. dllMain just returns true. I also adapted the Excel VBA code to call adder_Begin on a WorkBook_Open event and adder_End on a WorkBook_BeforeClose event.
The VBA looks like this:
Public functions in a new "Module1" module (cannot declare Public functions in the ThisWorkbook module):
Public Declare Function adder Lib " adder.dll" Alias "adder@8" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Function adder_Begin Lib "adder.dll" () As Boolean
Public Declare Sub adder_End Lib "adder.dll" ()
Private functions the the "ThisWorkbook" module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
adder_End
End Sub
Private Sub Workbook_Open()
adder_Begin
End Sub
The GHC dllMain.c looks like this:
#include