delphi dll source code:
library dllres;
type
char10 = array[0..9] of char;
TMydata = packed record
id: Integer;
name: char10;
married: Boolean;
salary: Double;
end;
PMydata = ^TMydata;
const
RESSTR: array[0..4] of string = ('HELLO', 'COLOR', 'DELPHI', 'shared', 'library');
NO_RESULT= 'no result';
var
mydata: TMydata;
{$R *.res}
// Return string pointer
function getResStr(aindex: Integer): PChar; stdcall;
begin
if aindex < Length(RESSTR) then
begin
Result := pchar(RESSTR[aindex]);
end
else
begin
Result := pchar(NO_RESULT);
end;
end;
// Return to the structure pointer
function getMydata: PMydata; stdcall;
begin
with mydata do
begin
id := 123;
name := 'obama';
married := false;
salary := 1200;
end;
Result := @mydata;
end;
exports getResStr, getMydata;
begin
end.
C# call example:
class Invoke_Delphi_Dll_Exam
{
[DllImport("", CallingConvention = )]
public static extern IntPtr getResStr(int index);
[DllImport("", CallingConvention = )]
public static extern IntPtr getMydata();
public struct Mydata
{
public int id; //0
public string name; //4
public bool married; //24
public double salary; //25
public Mydata(byte[] data)
{
if (data != null && == 33) {
id = BitConverter.ToInt32(data, 0);
name = (data, 4, 20).Replace("\0",""); // Remove the 0 characters at the tail
married = (data, 24);
salary = (data, 25);
}
else {
id = 0;
name = ;
married = false;
salary = 0;
}
}
public override string ToString()
{
return ("id: {0}, name: {1}, married: {2}, salary: {3}",
id, name, married, salary);
}
}
private static void Main(string[] args)
{
((getResStr(0)));
byte[] data = new byte[33];
(getMydata(), data, 0, 33);
Mydata mydata = new Mydata(data);
(mydata);
}
}