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 <windows.h>
#include <Rts.h>
#define __ADDER_DLL_EXPORT
#define ADDER_API _declspec(dllexport)
extern void __stginit_Adder(void);
static char* args[] = { "ghcDll", NULL };
/* N.B. argv arrays must end with NULL */
BOOL STDCALL DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
){
return TRUE;
}
ADDER_API BOOL adder_Begin(){
startupHaskell(1, args, __stginit_Adder);
return HS_BOOL_TRUE;
}
ADDER_API void adder_End(){
shutdownHaskell();
}
If somebody wants the code and Excel file, just ask, I'll be happy to provide it.
Thanks,
Olivier.