
I am new to using the Haskell FFI, and have been trying to implement the example in section 11.6 of the GHC user's guide. I have finally gotten to the point where my dll compiles (there is a missing space in the mainDll.h code at line 4:12) using GHC 6.6.1, and used the declare statement to expose the "adder" function in Excel VBA. My VBA Code is given below: Private Declare Function adder Lib " adder.dll" Alias "adder@8" (ByVal x As Integer, ByVal y As Integer) As Integer Private Sub test() Debug.Print adder(1, 2) End Sub My problem is this: The function works fine (the immediate window displays 3), but when I terminate Excel, I get an application error ("The instruction at ... referenced memory at ... . The memory could not be read."). Does anyone else have any experience calling Haskell from VBA that might be relevant?

A while ago I built a Haskell DLL that had to be invoked from Excel.
For some reason (I do not remember exactly why) it did not work directly,
so I wrote a small wrapper DLL in C++ that invoked Haskell functions.
The wrapper DLL was invoked by Excel via COM.
As regards your example, the only thing that springs to my mind is:
check that the calling convention is set correctly (that is, to stdcall
instead of ccall).
Cheers,
Cyril
2007/6/19, Lewis-Sandy, Darrell
I have finally gotten to the point where my dll compiles (there is a missing space in the mainDll.h code at line 4:12) using GHC 6.6.1, and used the declare statement to expose the "adder" function in Excel VBA. My VBA Code is given below:
Private Declare Function adder Lib " adder.dll" Alias "adder@8" (ByVal x As Integer, ByVal y As Integer) As Integer
Private Sub test()
Debug.Print adder(1, 2)
End Sub
My problem is this:
The function works fine (the immediate window displays 3), but when I terminate Excel, I get an application error ("The instruction at … referenced memory at … . The memory could not be read."). Does anyone else have any experience calling Haskell from VBA that might be relevant?

There is a number of problems, I'm not sure which one you are encountering.
Here are some that I remember:
The sample C code doesn't shut down the ghc runtime properly when the DLL
is unloaded. This causes a timer interrupt to jump into the void. This is
easily fixed with a couple of more lines of C.
The ghc runtime installs handlers for various things that it shouldn't touch
when used as a DLL.
-- Lennart
On 6/19/07, Lewis-Sandy, Darrell
I am new to using the Haskell FFI, and have been trying to implement the example in section 11.6 of the GHC user's guide.
I have finally gotten to the point where my dll compiles (there is a missing space in the mainDll.h code at line 4:12) using GHC 6.6.1, and used the declare statement to expose the "adder" function in Excel VBA. My VBA Code is given below:
Private Declare Function adder Lib " adder.dll" Alias "adder@8" (ByVal x As Integer, ByVal y As Integer) As Integer
Private Sub test()
Debug.Print adder(1, 2)
End Sub
*My problem is this:*
The function works fine (the immediate window displays 3), but when I terminate Excel, I get an application error ("The instruction at … referenced memory at … . The memory could not be read."). Does anyone else have any experience calling Haskell from VBA that might be relevant?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

We don't recommend calling shutdownHaskell() from DllMain(). Some information here: http://haskell.org/haskellwiki/GHC/Using_the_FFI#Debugging_Haskell_DLLs It should be safe to call shutdownHaskell() (aka hs_exit()) *before* unloading a DLL, and before exiting the program. Cheers, Simon Lennart Augustsson wrote:
There is a number of problems, I'm not sure which one you are encountering. Here are some that I remember:
The sample C code doesn't shut down the ghc runtime properly when the DLL is unloaded. This causes a timer interrupt to jump into the void. This is easily fixed with a couple of more lines of C.
The ghc runtime installs handlers for various things that it shouldn't touch when used as a DLL.
-- Lennart
On 6/19/07, *Lewis-Sandy, Darrell *
mailto:darrelll@amgen.com> wrote: I am new to using the Haskell FFI, and have been trying to implement the example in section 11.6 of the GHC user's guide.
I have finally gotten to the point where my dll compiles (there is a missing space in the mainDll.h code at line 4:12) using GHC 6.6.1, and used the declare statement to expose the "adder" function in Excel VBA. My VBA Code is given below:
Private Declare Function adder Lib " adder.dll" Alias "adder@8" (ByVal x As Integer, ByVal y As Integer) As Integer
Private Sub test()
Debug.Print adder(1, 2)
End Sub
*My problem is this:*
The function works fine (the immediate window displays 3), but when I terminate Excel, I get an application error ("The instruction at … referenced memory at … . The memory could not be read."). Does anyone else have any experience calling Haskell from VBA that might be relevant?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org mailto:Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Cyril Schmidt
-
Lennart Augustsson
-
Lewis-Sandy, Darrell
-
Simon Marlow