GHC generated dll makes Excel crash on exit.

Olivier Boudry olivier.boudry at
Fri Nov 30 13:09:25 EST 2007

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

The solution is based on the information found in :

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 at 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)
End Sub

Private Sub Workbook_Open()
End Sub

The GHC dllMain.c looks like this:

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

#define ADDER_API _declspec(dllexport)

extern void __stginit_Adder(void);

static char* args[] = { "ghcDll", NULL };
                        /* N.B. argv arrays must end with NULL */

                       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(){

If somebody wants the code and Excel file, just ask, I'll be happy to
provide it.


-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Glasgow-haskell-users mailing list