How to access a values in Array of Integer/String created in a DLL - VCL 32bits test
--- RAD 11.1 Alexandria --- DLL Delphi 32bits (for tests) ------ array of integer/string values... --- Client Delphi 32bits ------ reviewing the values created on DLL
IMPORTANT OBSERVATION ON USE OF TYPE "MANAGEMENT" (like strings...)
{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.
Important note about VCL usage: when this DLL will be implicitly loaded and this DLL uses TWicImage / TImageCollection created in any unit initialization section, then Vcl.WicImageInit must be included into your library's USES clause. }
此帖子包含附件: 大小:51.8K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.
Important note about VCL usage: when this DLL will be implicitly loaded and this DLL uses TWicImage / TImageCollection created in any unit initialization section, then Vcl.WicImageInit must be included into your library's USES clause. }
uses Sharemem, // <---------- System.SysUtils, System.Classes, uUnitWithArrayFunction in 'uUnitWithArrayFunction.pas';
{$R *.res}
begin
end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
type TMyArrayIntegerToReturn = TArray<integer>; TMyArrayStringToReturn = TArray<string>;
function MyArrayIntegerReturned: TMyArrayIntegerToReturn; // stdcall or cdecl; ? function MyArrayStringReturned: TMyArrayStringToReturn; // stdcall or cdecl; ?
function MyArrayIntegerReturned: TMyArrayIntegerToReturn; var MyArray: TMyArrayIntegerToReturn; begin result := []; // for var i: integer := 0 to 100 do MyArray := MyArray + [i]; // result := MyArray; end;
function MyArrayStringReturned: TMyArrayStringToReturn; var MyArray: TMyArrayStringToReturn; begin result := []; // for var i: integer := 65 to 122 do MyArray := MyArray + [Chr(i) + '_HelloWorld']; // result := MyArray; end;
end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Memo2: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
type TMyArrayIntegerReturned = TArray<integer>; // same type in DLL TMyArrayStringReturned = TArray<string>; TMyFuncInteger = function: TMyArrayIntegerReturned; // stdcall or cdecl; ? TMyFuncString = function : TMyArrayStringReturned;
procedure TForm1.Button1Click(Sender: TObject); var MyDLLHandle : THandle; MyArrayIntegerReturned: TMyArrayIntegerReturned; MyArrayStringReturned : TMyArrayStringReturned; begin MyDLLHandle := 0; @MyFuncIntegerOnDLL := nil; @MyFuncStringOnDLL := nil; // MyDLLHandle := LoadLibrary(PWideChar(MyDLLFilePath)); // if (MyDLLHandle > 0) then try @MyFuncIntegerOnDLL := GetProcAddress(MyDLLHandle, PWideChar(MyFuncArrayIntegerReturned)); @MyFuncStringOnDLL := GetProcAddress(MyDLLHandle, PWideChar(MyFuncArrayStringReturned)); // if Assigned(MyFuncIntegerOnDLL) then begin for var MyItem in MyFuncIntegerOnDLL do Memo1.Lines.Add(MyItem.ToString); end; // if Assigned(MyFuncStringOnDLL) then begin for var MyItem in MyFuncStringOnDLL do Memo2.Lines.Add(MyItem); end; finally FreeLibrary(MyDLLHandle); end; end;
initialization
ReportMemoryLeaksOnShutdown := true;
finalization
end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
now, if the values ar "stored on memory on DLL", just export the values like:
var // global access on DLL MyListValuesOrArray : ??? type
...
now, just create a function to export this variable !!!
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!