GHC generated dll makes Excel crash on exit.

Hi all, I need to call an Haskell module from Excel and as an exercise I started to play with the example given in GHC documentation: http://www.haskell.org/ghc/dist/current/docs/users_guide/win32-dlls.html I could build the DLL without problem but encountered a problem when using it. I created an Excel document using the dll. If I open the Excel document, do something that will trigger a function call in the DLL, then Excel will crash on Exit. On the opposite if I open the same document and changes cells not related to the function the program closes properly. I does not crash directly on the function call but later, just when I close Excel. As it crashes when closing Excel I suspect something is wrong when unloading the dll. I modified the dllMain.c code to handle the DLL_PROCESS_DETACH event but it doesn't solve the problem. } else if (reason == DLL_PROCESS_DETACH) { shutdownHaskell(); return TRUE; } I attached the code to this e-mail. I built the dll this way: ghc -fglasgow-exts -c adder.hs ghc -c dllMain.c ghc -shared -o adder.dll adder.o adder_stub.o dllMain.o Any idea on what could go wrong? Another event to handle? Thanks, Olivier.

Just a short addition to my previous e-mail: I just found an old thread which looks very similar to the problem I just described: http://www.nabble.com/GHC-6.4.1-and-Win32-DLLs:-Bug-in-shutdownHaskell--t120... I also tried to prevent the RTS from installing signal handers (it was mentionned in bug report #804 as something that could crash a dll). I put 'char *ghc_rts_opts = "--install-signal-handlers=no";' in a c file and linked it to the DLL. But it doesn't solve the problem.

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

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

On 12/3/07, Simon Peyton-Jones
Better still, could you add a section to that page about DLLs and Excel? That'd be useful for others.
Simon
Simon, As the page is part of the GHC documentation I cannot edit it. I put my notes in a new wiki page : http://haskell.org/haskellwiki/How_to_use_a_Haskell_Dll_from_Excel I hope this is appropriate. Best regards, Olivier.

It's not part of the Haskell documentation! The FFI page
http://haskell.org/haskellwiki/GHC/Using_the_FFI
is part of the "contributed documentation", linked from here:
http://haskell.org/haskellwiki/GHC
So it absolutely is a Wiki, and should be editable by you (assuming you log into the wiki).
If not, let's see why not. It'd be better to put your new material in with the other FFI stuff; it'll be easier to find that way.
THanks
SImon
From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users-bounces@haskell.org] On Behalf Of Olivier Boudry
Sent: 03 December 2007 14:28
To: Simon Peyton-Jones
Cc: glasgow-haskell-users@haskell.org
Subject: Re: GHC generated dll makes Excel crash on exit.
On 12/3/07, Simon Peyton-Jones

On Dec 3, 2007 10:07 AM, Simon Peyton-Jones
It's not part of the Haskell documentation! The FFI page
http://haskell.org/haskellwiki/GHC/Using_the_FFI
is part of the "contributed documentation", linked from here:
http://haskell.org/haskellwiki/GHC
So it absolutely is a Wiki, and should be editable by you (assuming you log into the wiki).
If not, let's see why not. It'd be better to put your new material in with the other FFI stuff; it'll be easier to find that way.
THanks
SImon
Simon, I was talking about this page: http://www.haskell.org/ghc/dist/current/docs/users_guide/win32-dlls.htmlwhic... is more Win32/Excel specific and is part of the GHC documentation. But I agree it's easier to keep everything in one place so I will move my stuff to the link you mentioned. Best regards, Olivier.

I moved my stuff to: http://haskell.org/haskellwiki/GHC/Using_the_FFI#Beware_of_dllMain.28.29_-_E... Cheers, Olivier.

Olivier Boudry wrote:
On Dec 3, 2007 10:07 AM, Simon Peyton-Jones
mailto:simonpj@microsoft.com> wrote: It's not part of the Haskell documentation! The FFI page
http://haskell.org/haskellwiki/GHC/Using_the_FFI
is part of the "contributed documentation", linked from here:
http://haskell.org/haskellwiki/GHC
So it absolutely is a Wiki, and should be editable by you (assuming you log into the wiki).
If not, let's see why not. It'd be better to put your new material in with the other FFI stuff; it'll be easier to find that way.
THanks
SImon
Simon,
I was talking about this page: http://www.haskell.org/ghc/dist/current/docs/users_guide/win32-dlls.html which is more Win32/Excel specific and is part of the GHC documentation.
But I agree it's easier to keep everything in one place so I will move my stuff to the link you mentioned.
Yes, I moved some of the wiki documentation into the user's guide before 6.8.1, but didn't delete the wiki at the time because 6.8.1 wasn't released yet. Now we have duplication, and I guess I (or someone at least) should sort it out... Cheers, Simon

Simon,
While we're at it, I found a typo in the
http://www.haskell.org/ghc/docs/latest/html/users_guide/win32-dlls.htmldocum...
page. In
ghc-6.6 the flag to build DLLs was --mk-dll (two leading minus signs), the
new one is -shared (one leading minus sign) but in the doc it's written
"--shared"
ghc ––shared -o foo.dll bar.o baz.o wibble.a -lfooble
should be
ghc –shared -o foo.dll bar.o baz.o wibble.a -lfooble
There are 6 occurences of what smells like a copy/paste bug ;-)
Cheers,
Olivier.
On Dec 4, 2007 5:42 AM, Simon Marlow
Olivier Boudry wrote:
On Dec 3, 2007 10:07 AM, Simon Peyton-Jones
mailto:simonpj@microsoft.com> wrote: It's not part of the Haskell documentation! The FFI page
http://haskell.org/haskellwiki/GHC/Using_the_FFI
is part of the "contributed documentation", linked from here:
http://haskell.org/haskellwiki/GHC
So it absolutely is a Wiki, and should be editable by you (assuming you log into the wiki).
If not, let's see why not. It'd be better to put your new material in with the other FFI stuff; it'll be easier to find that way.
THanks
SImon
Simon,
I was talking about this page: http://www.haskell.org/ghc/dist/current/docs/users_guide/win32-dlls.html which is more Win32/Excel specific and is part of the GHC documentation.
But I agree it's easier to keep everything in one place so I will move my stuff to the link you mentioned.
Yes, I moved some of the wiki documentation into the user's guide before 6.8.1, but didn't delete the wiki at the time because 6.8.1 wasn't released yet. Now we have duplication, and I guess I (or someone at least) should sort it out...
Cheers, Simon
participants (3)
-
Olivier Boudry
-
Simon Marlow
-
Simon Peyton-Jones