GHC generated dll makes Excel crash on exit.

Simon Peyton-Jones simonpj at
Mon Dec 3 03:10:42 EST 2007

Better still, could you add a section to that page about DLLs and Excel? That'd be useful for others.


From: glasgow-haskell-users-bounces at [mailto:glasgow-haskell-users-bounces at] On Behalf Of Olivier Boudry
Sent: 30 November 2007 18:09
To: glasgow-haskell-users at
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 :

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