[Haskell-cafe] ffi call works fine in ghci but not as ghc compiledsegfault

Miro Karpis miroslav.karpis at gmail.com
Sun Mar 16 06:31:41 UTC 2014


still no luck ;-( Everything works well under ghci but if I compile it with
ghc I get seg. fault

ghc version 7.6.3

I found out that when I load my ghci without any .o and .hi files ghci
function calls works fine. If I then compile my code with ghc and restart
my ghci (.o and .hi files now exists) the ghci crashes.

I have tried compiling with:
   ghc -O --make Service.hs -L. -lmodel
   ghc --make Service.hs -L. -lmodel
   ghc -o service Service.hs -L. -lmodel

I have tried to call ffi function with:
setmodulestring1 param value = do
let param = cParamLength = fromIntegral $ length param ::CInt
    cValueLength = fromIntegral $ length value ::CInt
    setVarInArray = (-1)::CInt
B.useAsCString (B.pack "FilePath") $ \cParam -> do
B.useAsCString (B.pack "C:/dev/misc/haskell/services/FM") $ \cValue -> do
result <- c_setmodulestring cParam cParamLength cValue cValueLength
setVarInArray
return result

or with this:
setmodulestring :: String -> String -> IO CInt
setmodulestring param value = do
let param = "FilePath"
let value = "C:/dev/misc/haskell/services/FM"
cParam <- newCString param
cValue <- newCString 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 result


my main is:
main = do
let param = "FilePath"
let value = "C:/dev/misc/haskell/services/FM"
result <- liftIO $ FM.setmodulestring param value
return "done"

and still no luck - works in ghci and crashes as exe

interfacing the same library from a c++ code:
extern "C" int __stdcall setmodulestring(char* param,unsigned int length,
char* valuein,unsigned int valuelength,int index);

interfacing it from Haskell:

foreign import ccall "setmodulestring"  c_setmodulestring :: CString
-> CInt -> CString -> CInt -> CInt -> IO CInt


please any ideas?




On Sat, Mar 15, 2014 at 4:48 PM, Miro Karpis <miroslav.karpis at gmail.com>wrote:

> thank you very much so far and sorry for my not 100% understanding ;-).
> The dll is an external software - I have no control over it.
>
> This is how I interface it from c++ program:
>
> extern "C" int __stdcall setmodulestring(char* param,unsigned int length,
> char* valuein,unsigned int valuelength,int index);
>
>
> And this is how I'm trying to interface it from Haskell (with the seg-fault):
>
> foreign import ccall "setmodulestring"  c_setmodulestring :: Ptr Char -> CInt -> Ptr Char -> CInt -> CInt -> IO CInt
>
> m.
>
>
> On Sat, Mar 15, 2014 at 3:32 AM, Brandon Allbery <allbery.b at gmail.com>wrote:
>
>> On Fri, Mar 14, 2014 at 7:29 PM, Branimir Maksimovic <
>> branimir.maksimovic at gmail.com> wrote:
>>
>>>  You are passing wchar_t* and function expects char*.
>>>
>>
>> FWIW they asked this before (possibly on a different list) and, absent
>> the C information, I pointed out that Win32 API functions use CWString.
>> This was apparently taken to mean that CWString was the only option....
>>
>> Some awareness of one's platform conventions tends to be helpful when
>> doing FFI. It seems to be absent here. :/
>>
>> --
>> brandon s allbery kf8nh                               sine nomine
>> associates
>> allbery.b at gmail.com
>> ballbery at sinenomine.net
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140316/241f2311/attachment.html>


More information about the Haskell-Cafe mailing list