Forum > General
Universal Library Headers
Fred vS:
Hum, sorry, im already back... :-[
Is that ok (i mean easy-exportable) ?
--- Code: --- MyFunctionStr: function(Str: Pchar): Pchar; cdecl;
--- End code ---
[EDIT] : Hum, just find that from http://stackoverflow.com
Pascal code :
--- Code: ---procedure DLL_Message(Location:PChar;AIntValue :integer);stdcall;
external 'DLLTest.dll';
--- End code ---
C# code:
--- Code: ---DllImport(
"DLLTest.dll",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi,
EntryPoint = "DLL_Message"
)]
public static extern void DLL_Message(
[MarshalAs(UnmanagedType.LPStr)] string Location,
int AIntValue
);
--- End code ---
PS : Ooops, poor C developers, your syntax is so complicated vs Pascal :-X
taazz:
No don't do that. Study the Windows API and see how it returns pchars.
Your way is using the dlls memory manager to allocate memory for the PChar, this means that your application can not free that memory it needs to be freed by the dll. If you want your application to be able to free the pchars then you have to let the application allocate them too. So in your case go for a procedure something along the lines of
--- Code: ---Function MyStrTest(inStr:Pchar; outStr:pChar; size:integer):integer;
const
MyRealResult :string = 'This is a test for returning strings';
begin
Result := length(myRealResult);
if size >= result then move(OutStr[0], MyRealResult[1], result);
end;
--- End code ---
you call it like this
--- Code: ---var
MyLen : integer;
MyStr : string;
begin
MyLen := MyStrTest(nil, @MyStr[1], 0);
SetLEngth(MyStr, mylen+1);
FillChar(MyStr[1], MyLen+1,0);
MyStrTest(nil, @MyStr[1], 0);
ShowMessage(MyStr);
end
--- End code ---
Fred vS:
@ Taazz : many thanks.
I will deeply study your code.
Fred vS:
--- Quote --- this means that your application can not free that memory it needs to be freed by the dll
--- End quote ---
Hum, it is exactly what i want... I want that the dll takes care of everything...
Even freed the memory...
taazz:
Impossible, when is the memory to be freed? when is the string not used anymore? Can you know? Should you care? it is the application prerogative to keep it or free it when ever it wants to, otherwise you either provide a function to free that memory when ever the user wants or keep allocating more and more memory for the same data again and again until the library is unloaded.
Navigation
[0] Message Index
[#] Next page
[*] Previous page