Excel Automation with Haskell gives a seg fault

ExcelHaskellComOle

Excel Problem Overview


I can fire up Excel with the following script. But in ghci (7.4.1) I have a segmentation fault when I run it.

I don't know where to search from now. I don't have this error if I remove the line

workSheets <- workBook #  propertyGet_0 "Worksheets"

Here is the code. May be I forgot something. I read the source code of com.hs here, but it doesn't give me any clue.

import System.Win32.Com 
import System.Win32.Com.Automation
--
-- createObjectExcel 
-- coming from Automation.hs and com.hs
--

iidIDispatch_unsafe  = mkIID "{00020400-0000-0000-C000-000000000046}"

createObjExl :: IO (IDispatch ()) 
createObjExl = do
    clsidExcel <- clsidFromProgID "Excel.Application"
    pExl <- coCreateInstance clsidExcel  Nothing LocalProcess iidIDispatch_unsafe
    return pExl


fichierTest2 = "E:/Programmation/haskell/Com/qos1.xls"

main = coRun $ do 
    pExl <- createObjExl
    workBooks <- pExl #  propertyGet_0 "Workbooks"
    workBook <- workBooks #  propertyGet_1 "Open" fichierTest2
    workSheets <- workBook #  propertyGet_0 "Worksheets"

    workBooks # method_1_0 "Close" (0::Int)
    pExl # method_0_0 "Quit"

    mapM release [workSheets,workBook, workBooks, pExl]

Edit with Gonzalez's advice I tried debugging, but no information arose. I tried the code by hand in ghci, and it seemed that the guilty party was the release function.

When I entered these in ghci, I got the segmentation fault:

*Main> coInitialize
*Main> pExl <- createObjExl
*Main> release pExl
0

Now if I hit "pExl" I have a reference. Shouldn't that be set to Null?

*Main> pExl
<interface pointer = 0x020844cc>

*Main> coUnInitialize
*Main> :q
leaving Ghci
Segmentation Fault/access violation ...

Excel Solutions


Solution 1 - Excel

You might be calling the workSheets method from within a static function. Try moving it out.

Or, have you tried explicitly declaring the data type of 'Worksheets'

WorkSheets :: Int -> Int or (whatever type is should be).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjinkou2 jinkou2View Question on Stackoverflow
Solution 1 - ExcelFyor NikowskiView Answer on Stackoverflow