<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Better still, could you add a section to that page about DLLs
and Excel? That’d be useful for others.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Simon<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'>
<div>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'> glasgow-haskell-users-bounces@haskell.org
[mailto:glasgow-haskell-users-bounces@haskell.org] <b>On Behalf Of </b>Olivier
Boudry<br>
<b>Sent:</b> 30 November 2007 18:09<br>
<b>To:</b> glasgow-haskell-users@haskell.org<br>
<b>Subject:</b> Re: GHC generated dll makes Excel crash on exit.<o:p></o:p></span></p>
</div>
</div>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal style='margin-bottom:12.0pt'>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.<br>
<br>
The solution is based on the information found in : <a
href="http://haskell.org/haskellwiki/GHC/Using_the_FFI#Debugging_Haskell_DLLs">http://haskell.org/haskellwiki/GHC/Using_the_FFI#Debugging_Haskell_DLLs</a>.<br>
<br>
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. <br>
<br>
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. <br>
<br>
The VBA looks like this:<br>
<br>
Public functions in a new "Module1" module (cannot declare Public
functions in the ThisWorkbook module):<o:p></o:p></p>
<div style='margin-left:30.0pt'>
<p class=MsoNormal>Public Declare Function adder Lib " adder.dll"
Alias "adder@8" (ByVal x As Long, ByVal y As Long) As Long<br>
Public Declare Function adder_Begin Lib "adder.dll" () As Boolean<br>
Public Declare Sub adder_End Lib "adder.dll" () <o:p></o:p></p>
</div>
<p class=MsoNormal style='margin-bottom:12.0pt'><br>
Private functions the the "ThisWorkbook" module:<o:p></o:p></p>
<div style='margin-left:30.0pt'>
<p class=MsoNormal>Private Sub Workbook_BeforeClose(Cancel As Boolean)<br>
adder_End<br>
End Sub<br>
<br>
Private Sub Workbook_Open() <br>
adder_Begin<br>
End Sub<o:p></o:p></p>
</div>
<p class=MsoNormal style='margin-bottom:12.0pt'><br>
The GHC dllMain.c looks like this:<o:p></o:p></p>
<div style='margin-left:30.0pt'>
<p class=MsoNormal>#include <windows.h><br>
#include <Rts.h><br>
<br>
#define __ADDER_DLL_EXPORT<br>
#define ADDER_API _declspec(dllexport) <br>
<br>
extern void __stginit_Adder(void);<br>
<br>
static char* args[] = { "ghcDll", NULL };<br>
/* N.B. argv arrays must end with NULL */<br>
<br>
BOOL STDCALL DllMain( HANDLE hModule, <br>
DWORD ul_reason_for_call, <br>
LPVOID lpReserved<br>
){<br>
return TRUE;<br>
}<br>
<br>
ADDER_API BOOL adder_Begin(){<br>
startupHaskell(1, args, __stginit_Adder);<br>
return HS_BOOL_TRUE;<br>
}<br>
<br>
ADDER_API void adder_End(){ <br>
shutdownHaskell();<br>
}<o:p></o:p></p>
</div>
<p class=MsoNormal><br>
If somebody wants the code and Excel file, just ask, I'll be happy to provide
it.<br>
<br>
Thanks,<br>
<br>
Olivier.<o:p></o:p></p>
</div>
</div>
</body>
</html>