[Haskell-cafe] ffi call works fine in ghci but not as ghc compiledsegfault
Branimir Maksimovic
branimir.maksimovic at gmail.com
Fri Mar 14 23:29:06 UTC 2014
You are passing wchar_t* and function expects char*.
On 03/15/2014 12:23 AM, Miro Karpis wrote:
> here comes the c definition:
>
> int setmodulestring(char* parameter, int parameterLength, char* value,
> int valueLength, int setVarInArray);
>
>
> In ghci the main and setmodulestring functions works fine
>
>
> On Sat, Mar 15, 2014 at 12:14 AM, Branimir Maksimovic
> <branimir.maksimovic at gmail.com <mailto:branimir.maksimovic at gmail.com>>
> wrote:
>
> On 03/14/2014 11:24 PM, 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 = do
>> let param = "FilePath"
>> let value = "C:/dev/misc/haskell/services/FM"
>> result <- liftIO $ FM.setmodulestring param value
>> return "done"
>>
>> setmodulestring :: String -> String -> IO CInt
>> setmodulestring param value = do
>> let cParamLength = fromIntegral $ length param ::CInt
>> cValueLength = fromIntegral $ length value ::CInt
>> setVarInArray = (-1)::CInt
>> alloca $ \cParam -> do
>> alloca $ \cValue -> do
>> result <- c_setmodulestring cParam cParamLength cValue
>> cValueLength setVarInArray
>> return result
>>
> This does not seems correct (or incomplete).
>
>
>> If I try also with following, the behaviour is the same:
>>
>> setmodulestring2 :: String -> String -> IO CInt
>> setmodulestring2 param value = do
>> cParam <- newCWString param
>> cValue <- newCWString value
>> let cParamLength = fromIntegral $ length param ::CInt
>> cValueLength = fromIntegral $ length value ::CInt
>> setVarInArray = (-1)::CInt
>> result <- c_setmodulestring cParam cParamLength cValue
>> cValueLength setVarInArray
>> free cParam
>> free cValue
>> return res
>>
> This one may be correct but without, seeing C code (where it probably
> segfaults) I cannot say anything further.
> exact ffi declaration of c_setmodulestring would be usefull, too,
> to compare with C function.
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org <mailto:Haskell-Cafe at haskell.org>
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140315/c2be102f/attachment.html>
More information about the Haskell-Cafe
mailing list