GHC generated dll makes Excel crash on exit.

Simon Peyton-Jones simonpj at microsoft.com
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.

Simon

From: glasgow-haskell-users-bounces at haskell.org [mailto:glasgow-haskell-users-bounces at haskell.org] On Behalf Of Olivier Boudry
Sent: 30 November 2007 18:09
To: glasgow-haskell-users at 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 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)
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20071203/e7ae3b59/attachment.htm


More information about the Glasgow-haskell-users mailing list