<div dir="ltr">still no luck ;-( Everything works well under ghci but if I compile it with ghc I get seg. fault<div><br></div><div>ghc version 7.6.3</div><div><br></div><div>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. </div>
<div><br><div>I have tried compiling with: </div><div>   ghc -O --make Service.hs -L. -lmodel</div><div>   ghc --make Service.hs -L. -lmodel<br></div><div>   ghc -o service Service.hs -L. -lmodel<br></div><div><br></div><div>
I have tried to call ffi function with:</div><div>setmodulestring1 param value = do<span class="" style="white-space:pre">        </span></div><div><span class="" style="white-space:pre">   </span>let param = cParamLength = fromIntegral $ length param ::CInt</div>
<div><span class="" style="white-space:pre">    </span>    cValueLength = fromIntegral $ length value ::CInt</div><div><span class="" style="white-space:pre">      </span>    setVarInArray = (-1)::CInt</div><div><span class="" style="white-space:pre">     </span>B.useAsCString (B.pack "FilePath") $ \cParam -> do</div>
<div><span class="" style="white-space:pre">    </span>B.useAsCString (B.pack "C:/dev/misc/haskell/services/FM") $ \cValue -> do</div><div><span class="" style="white-space:pre">             </span>result <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArray</div>
<div><span class="" style="white-space:pre">            </span>return result </div></div><div><br></div><div>or with this:</div><div><div>setmodulestring :: String -> String -> IO CInt</div><div>setmodulestring param value = do</div>
<div><span class="" style="white-space:pre">    </span>let param = "FilePath"</div><div><span class="" style="white-space:pre">   </span>let value = "C:/dev/misc/haskell/services/FM"</div><div><span class="" style="white-space:pre">    </span>cParam <- newCString param</div>
<div><span class="" style="white-space:pre">    </span>cValue <- newCString value</div><div><span class="" style="white-space:pre">      </span>let cParamLength = fromIntegral $ length param ::CInt</div><div><span class="" style="white-space:pre">      </span>    cValueLength = fromIntegral $ length value ::CInt</div>
<div><span class="" style="white-space:pre">    </span>    setVarInArray = (-1)::CInt </div><div><span class="" style="white-space:pre">    </span>result <- c_setmodulestring cParam cParamLength cValue cValueLength setVarInArray</div>
<div><span class="" style="white-space:pre">    </span>free cParam</div><div><span class="" style="white-space:pre">        </span>free cValue</div><div><span class="" style="white-space:pre">        </span>return result</div></div><div><br>
</div><div><br></div><div>my main is:</div><div><div>main = do</div><div><span class="" style="white-space:pre">  </span>let param = "FilePath"</div><div><span class="" style="white-space:pre">   </span>let value = "C:/dev/misc/haskell/services/FM"</div>
<div><span class="" style="white-space:pre">    </span>result <- liftIO $ FM.setmodulestring param value</div><div><span class="" style="white-space:pre">       </span>return "done"</div></div><div><br></div><div>and still no luck - works in ghci and crashes as exe</div>
<div><br></div><div>interfacing the same library from a c++ code:</div><div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, helvetica, sans-serif" color="#444444">extern "C" int __stdcall setmodulestring(char* param,unsigned int </font><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">length,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">char</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">* valuein,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">unsigned int </span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">valuelength,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">int </span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">index);</span></div>
<div style="font-family:arial,sans-serif;font-size:13px"><div title="Page 25"><pre style="white-space:pre-wrap"><font face="arial, helvetica, sans-serif" color="#444444">interfacing it from Haskell:</font></pre><pre style="white-space:pre-wrap">
<span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">foreign import ccall "setmodulestring"  c_setmodulestring :: CString -> CInt -> </span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">CString</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif"> -> CInt -> CInt -> IO CInt</span><br>
</pre></div></div></div><div><br></div><div>please any ideas?</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 15, 2014 at 4:48 PM, Miro Karpis <span dir="ltr"><<a href="mailto:miroslav.karpis@gmail.com" target="_blank">miroslav.karpis@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><font face="arial, helvetica, sans-serif" color="#444444">thank you very much so far and sorry for my not 100% understanding ;-). </font><div>
<font face="arial, helvetica, sans-serif" color="#444444">The dll is an external software - I have no control over it.</font><div>
<font face="arial, helvetica, sans-serif" color="#444444"><br></font></div><div><font face="arial, helvetica, sans-serif" color="#444444">This is how I interface it from c++ program:</font></div><div><font face="arial, helvetica, sans-serif" color="#444444"><br>

</font></div><div><font face="arial, helvetica, sans-serif" color="#444444">extern "C" int __stdcall setmodulestring(char* param,unsigned int </font><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">length,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">char</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">* valuein,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">unsigned int </span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">valuelength,</span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">int </span><span style="color:rgb(68,68,68);font-family:arial,helvetica,sans-serif">index);</span></div>

<div><div title="Page 25"><div><div><pre><font face="arial, helvetica, sans-serif" color="#444444"><br></font></pre><pre><font face="arial, helvetica, sans-serif" color="#444444">And this is how I'm trying to interface it from Haskell (with the seg-fault):</font></pre>

<pre><font face="arial, helvetica, sans-serif" color="#444444">foreign import ccall "setmodulestring"  c_setmodulestring :: Ptr Char -> CInt -> Ptr Char -> CInt -> CInt -> IO CInt<span class="HOEnZb"><font color="#888888"><br>
</font></span></font></pre><span class="HOEnZb"><font color="#888888">
</font></span></div></div></div></div><span class="HOEnZb"><font color="#888888"><div>m.</div></font></span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 15, 2014 at 3:32 AM, Brandon Allbery <span dir="ltr"><<a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>On Fri, Mar 14, 2014 at 7:29 PM, Branimir Maksimovic <span dir="ltr"><<a href="mailto:branimir.maksimovic@gmail.com" target="_blank">branimir.maksimovic@gmail.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div>You are passing wchar_t* and function
      expects char*.</div></div></blockquote><div><br></div></div><div>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....</div>


<div><br></div><div>Some awareness of one's platform conventions tends to be helpful when doing FFI. It seems to be absent here. :/</div><div><br></div></div><div>-- <br><div dir="ltr"><div>brandon s allbery kf8nh                               sine nomine associates</div>


<div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>                                  <a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div><div>unix, openafs, kerberos, infrastructure, xmonad        <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div>


</div>
</div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>