I do not think i can help you, but my FFi to functions with similar signature looks like this:
C definition:
PDFLIB_API int PDFLIB_CALL
PDF_begin_document(PDF *p, const char *filename, int len, const char *optlist);
haskell FFI:
c_beginDocument :: Pdf -> String -> String -> IO Int
c_beginDocument pdf a2 a3 =
withCString a2 $ \a2' ->
withCString a3 $ c_beginDocument'_ pdf a2' 0
foreign import ccall safe "Pdflib.chs.h PDF_begin_document" c_beginDocument'_ :: Pdf -> Ptr CChar -> CInt -> Ptr CChar -> IO Int
The difference as you see, i'm using withCString to convert from ahskell to C and i'm passing 0 as the string size.
On Friday, March 14, 2014 3:24:23 PM UTC-7, Miroslav Karpis wrote:Hi, please can you help me with following?I have a call to an external dll (via ffi) which if executed from ghci works fine.If I compile my code with 'ghc -o fm Mycode.hs -L. -lextdll' and run it I get 'Segmentation fault/access violation in generated code'.main = dolet param = "FilePath"let value = "C:/dev/misc/haskell/services/FM"result <- liftIO $ FM.setmodulestring param valuereturn "done"setmodulestring :: String -> String -> IO CIntsetmodulestring param value = dolet cParamLength = fromIntegral $ length param ::CIntcValueLength = fromIntegral $ length value ::CIntsetVarInArray = (-1)::CIntalloca $ \cParam -> doalloca $ \cValue -> doresult <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArrayreturn resultIf I try also with following, the behaviour is the same:setmodulestring2 :: String -> String -> IO CIntsetmodulestring2 param value = docParam <- newCWString paramcValue <- newCWString valuelet cParamLength = fromIntegral $ length param ::CIntcValueLength = fromIntegral $ length value ::CIntsetVarInArray = (-1)::CIntresult <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArrayfree cParamfree cValuereturn resAny comments/ideas more than appreciated.
Cheers,Miro