I have dll which accepts Pointer to array of bytes from C++ and try to move this data into AnsiString the following way
procedure Convert(const PByteArr: Pointer; ArrSize: Cardinal); export; cdecl;
var
Buf: AnsiString;
begin
SetString(Buf, PAnsiChar(PByteArr^), ArrSize);
end;
If I call this method from Delphi
procedure Try;
var
M: TMemoryStream;
Arr: TBytes;
begin
M := TMemoryStream.Create;
try
M.LoadFromFile('myfile.dat');
SetLength(Arr, M.Size);
M.Position := 0;
M.Read(Arr[0], M.Size);
finally
M.Free;
end;
Convert(@Arr, Length(Arr));
end;
it works fine, but from c++ if gives AV on SetString.
Please help me with this.
From RredCat:
Let me add some explanation to Yuriy’s question:
First of all about languages that we use. We need to call Delphi dll in C# project. I’ve created C++\CLI layer (proxy) for this purposes.
Now about C++\CLI code in header file:
HINSTANCE hDelphiDLL;
typedef void (*pPBDFUNC)(byte* aBytes, int size);
pPBDFUNC Convert;
In cpp I set Convert in constructor:
hDelphiDLL = LoadLibrary(<path to dll>);
if(NULL != hDelphiDLL ){
pPBDFUNC clb= GetProcAddress(HMODULE(hDelphiDLL), "Convert");
if(NULL != clb){
Convert= pPBDFUNC (clb);
}...
And last one method that I call from C#:
void Class1::Test(byte* aBytes, int size){
Convert(aBytes,size);
}
Your Delphi code passes a pointer to pointer to array of bytes. Simplify it to use just a pointer to array of bytes:
and call it as
or
which is the same.