(Already posted this in the C# Forums)
I am building an API DLL in Delphi for a system I'm working on. This includes many functions, such as...
'Output' is a parameter to get back the result string from the function. 'Size' is the given and returned size of 'Output'. The result Bool identifies if the function completed successfully.
In Delphi implementation, I have it down...
However, in C# I am having some trouble figuring out what types to use.
This is how I thought to do it, but I'm far from familiar with C#...
It doesn't seem to like my 'bool' type, so what should I use instead?
And finally how to handle the data like I have to in Delphi?
I know these type relations:
Delphi String - Do not use in DLL's
Delphi PChar - Equivalent to C# string
Delphi DWORD - Equivalent to C# int <-- ?
Delphi Boolean - Do not use in DLL's
Delphi Bool - Equivalent to C# bool
Also, how do I account for Const and Var parameters? Such as...
JD Solutions
I am building an API DLL in Delphi for a system I'm working on. This includes many functions, such as...
Code:
function GetSomeData(Output: PChar; var Size: DWORD): Bool; StdCall;
var
Str: String;
begin
Result:= False;
try
Str:= 'Some string obtained by the function';
if assigned(Output) then begin
if Output <> nil then begin
Size:= Length(Str);
StrPCopy(Output, Str);
end else begin
Size:= 0;
end;
end;
Result:= True;
except
on e: exception do begin
Result:= False;
end;
end;
end;
'Output' is a parameter to get back the result string from the function. 'Size' is the given and returned size of 'Output'. The result Bool identifies if the function completed successfully.
In Delphi implementation, I have it down...
Code:
function GetSomeData(Output: PChar; var Size: DWORD): Bool; StdCall; External 'MyDLL.dll';
function GetData: String;
var
Buf: String;
BufLen: DWORD;
begin
Result:= '';
Buf:= '';
BufLen:= 255;
SetLength(Buf, BufLen);
if GetSomeData(PChar(Buf), BufLen) then begin
SetLength(Buf, BufLen);
Result:= Buf;
end;
end;
However, in C# I am having some trouble figuring out what types to use.
This is how I thought to do it, but I'm far from familiar with C#...
Code:
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern bool GetSomeData(string Output, int Size);
It doesn't seem to like my 'bool' type, so what should I use instead?
And finally how to handle the data like I have to in Delphi?
I know these type relations:
Delphi String - Do not use in DLL's
Delphi PChar - Equivalent to C# string
Delphi DWORD - Equivalent to C# int <-- ?
Delphi Boolean - Do not use in DLL's
Delphi Bool - Equivalent to C# bool
Also, how do I account for Const and Var parameters? Such as...
Code:
function GetSomeData(const Input: PChar; Output: PChar; var Size: DWORD): Bool; StdCall;
JD Solutions