<div dir="ltr">I do not think i can help you, but my FFi to functions with similar signature looks like this:<br><br>C definition:<br><br>PDFLIB_API int PDFLIB_CALL<br>PDF_begin_document(PDF *p, const char *filename, int len, const char *optlist);<br><br>haskell FFI:<br><br>c_beginDocument :: Pdf -> String -> String -> IO Int<br>c_beginDocument pdf a2 a3 =<br>  withCString a2 $ \a2' -><br>  withCString a3 $ c_beginDocument'_ pdf a2' 0<br><br>foreign import ccall safe "Pdflib.chs.h PDF_begin_document" c_beginDocument'_ :: Pdf -> Ptr CChar -> CInt -> Ptr CChar -> IO Int<br><br>The difference as you see, i'm using withCString to convert from ahskell to C and i'm passing 0 as the string size.<br><br><br>On Friday, March 14, 2014 3:24:23 PM UTC-7, Miroslav Karpis wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div style="word-wrap:break-word;color:rgb(0,0,0);font-size:14px;font-family:Calibri,sans-serif"><div><span style="font-family:Calibri;font-size:medium">Hi, please can you help me with following?</span><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium">I have a call to an external dll (via ffi) which if executed from ghci works fine. </div><div style="font-family:Calibri;font-size:medium">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'.</div><div style="font-family:Calibri;font-size:medium"><div><br></div><div><br></div><div><br></div><div><div>main = do</div><div><span style="white-space:pre"> </span>let param = "FilePath"</div><div><span style="white-space:pre">      </span>let value = "C:/dev/misc/haskell/services/<wbr>FM"</div><div><span style="white-space:pre">    </span>result <- liftIO $ FM.setmodulestring param value</div><div><span style="white-space:pre">  </span>return "done"</div></div><div><br></div><div><div style="font-family:Calibri,sans-serif;font-size:14px">setmodulestring :: String -> String -> IO CInt</div><div style="font-family:Calibri,sans-serif;font-size:14px">setmodulestring param value = do</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">     </span>let cParamLength = fromIntegral $ length param ::CInt</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">    </span>    cValueLength = fromIntegral $ length value ::CInt</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">  </span>    setVarInArray = (-1)::CInt</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre"> </span>alloca $ \cParam -> do</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">     </span>alloca $ \cValue -> do</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">             </span>result <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArray</div><div style="font-family:Calibri,sans-serif;font-size:14px"><span style="white-space:pre">          </span>return result</div></div></div><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium">If I try also with following, the behaviour is the same:</div><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium"><div>setmodulestring2 :: String -> String -> IO CInt</div><div>setmodulestring2 param value = do</div><div><span style="white-space:pre">     </span>cParam <- newCWString param</div><div><span style="white-space:pre">        </span>cValue <- newCWString value</div><div><span style="white-space:pre">        </span>let cParamLength = fromIntegral $ length param ::CInt</div><div><span style="white-space:pre"> </span>    cValueLength = fromIntegral $ length value ::CInt</div><div><span style="white-space:pre">       </span>    setVarInArray = (-1)::CInt </div><div><span style="white-space:pre">        </span>result <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArray</div><div><span style="white-space:pre">  </span>free cParam</div><div><span style="white-space:pre">   </span>free cValue</div><div><span style="white-space:pre">   </span>return res</div></div><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium">Any comments/ideas more than appreciated.</div><div style="font-family:Calibri;font-size:medium"><br></div><div style="font-family:Calibri;font-size:medium">Cheers,</div><div style="font-family:Calibri;font-size:medium">Miro</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>
</blockquote></div>