<p>Am 15.03.2014 00:08 schrieb "Fred van Stappen" <<a href="mailto:fiens@hotmail.com">fiens@hotmail.com</a>>:<br>
><br>
> > Note: if your MyString is valid through the complete lifetime it could <br>
> > be used you can also use "Result := PChar(MyString);"<br>
><br>
> Hum, to be sure that i understand...<br>
><br>
> type<br>
> TStringClass = class(TObject)<br>
> Name: AnsiString; //// does not change<br>
> SomeText: AnsiString; ///// may change..<br>
> end;<br>
><br>
> var<br>
> AStringClass : TStringClass;<br>
><br>
> ....<br>
><br>
> function GetName: PChar; cdecl ;<br>
> begin <br>
> result := PChar(AStringClass.Name);<br>
> end;<br>
><br>
> function GetSomeText: PChar; cdecl ;<br>
> var<br>
> MyString: AnsiString ;<br>
> begin <br>
> MyString := AStringClass.SomeText ;<br>
><br>
> Result := GetMem(Length(MyString) + 1); <br>
> Move(@MyString[1], Result, Length(MyString)); <br>
> Result[Length(MyString)] := 0; <br>
> end; <br>
><br>
> Is it OK ?</p>
<p>You don't need the MyString variable, you can simply use AStringClass.SomeText. The variable was just for illustrations.</p>
<p>And you should definitely document the results of which of your libraries must be freed and which must not.</p>
<p>Additionally I would suggest to store the pointer values of the allocated PChar results (e.g. TList or specialize TFPGList<PChar>) so that you can use this inside your exported FreeString function to check whether this is indeed a pointer that you need to free.</p>
<p>Regards,<br>
Sven</p>