Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Automation with Haskell gives a seg fault

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 ...
like image 822
jinkou2 jinkou2 Avatar asked Jan 26 '13 13:01

jinkou2 jinkou2


1 Answers

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).

like image 128
Fyor Nikowski Avatar answered Oct 14 '22 06:10

Fyor Nikowski