<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>> Date: Fri, 14 Mar 2014 08:20:46 -0700<br><div>> From: leledumbo_cool@yahoo.co.id<br>> To: fpc-pascal@lists.freepascal.org<br>> Subject: Re: [fpc-pascal] Library.StringFunction() : PChar => NO<br>> <br>> > It will be the memory manager of the library who deals with the PChar<br>> result.<br>> So difficult for the main application to manage the memory used by PChar.<br>> <br>> It's not a problem, as long as you provide an API to dispose the memory used<br>> by the returned PChar. The responsibility of calling this API is delegated<br>> to the application.<br>> <br>> > MyStrTest(inStr:Pchar; outStr:pChar; size:integer):integer; cdecl; <br>> > var <br>> > MyRealResult :string;<br>> > begin<br>> > MyRealResult := inStr + ' added by function ';<br>> > Result := length(myRealResult);<br>> > if size >= result then move(OutStr[0], MyRealResult[1], result);<br>> > end;<br>> <br>> Wrong usage of Move<br>> (http://www.freepascal.org/docs-html/rtl/system/move.html). First parameter<br>> is source, second is destination. So you have to swap them in above<br>> function.<br>> <br>> > function testLib(InputText : string) : string ;<br>> > var<br>> > MyStrOut : string;<br>> > MyLen : integer;<br>> > begin<br>> > MyLen := MyStrTest( @InputText[1],@MyStrOut[1], 0);<br>> > SetLEngth(MyStrOut, mylen+1); <br>> > FillChar(MyStrOut[1], MyLen+1,0);<br>> > MyStrTest(@InputText[1], @MyStrOut[1], 0);<br>> > result := MyStrOut ;<br>> > end;<br>> <br>> Wrong usage of the MyStrTest. Here you give Size parameter of value 0, while<br>> in MyStrTest... see that last if statement? Guess yourself. Moreover, after<br>> the first call, you set the length and then filling the space with 0. The<br>> 2nd MyStrTest call doesn't differ from the first.<br>> <br>> From the code, I can guess what that good guru means. First call he just<br>> wants to get the length, therefore giving size 0. He then allocates enough<br>> storage and re-call the function, but this time he seems to have a typo. The<br>> 2nd MyStrTest call should have size parameter set to MyLen.<br><br>@ leledumbo, like always => brilliant => Tetras * Megas thanks. ;-)<br></div> </div></body>
</html>