Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What data types to use in C# for parameters in a Delphi DLL?

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
(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...

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
 
It doesn't seem to like my 'bool' type, so what should I use instead?

"Bool" is a long boolean (LongBool). Four bytes. Treat it like an unsigned or signed double word and you should be fine.


Delphi DWORD - Equivalent to C# int <-- ?

Four byte signed integer. Usually.

Delphi Boolean - Do not use in DLL's

You can. Delphi Boolean is a one byte flag. Pass it as a Byte in the function prototype and typecast it to "Boolean" within the Delphi function and you'll be fine.

Also, how do I account for Const and Var parameters? Such as...

Usually the difference (80% of the time, but not important in cases where a pointer is passed anyway) is that a VAR parameter is passed as a pointer to the original value, while other values have the value itself passed in separate storage. For the prototype you posted last, "Size" is passed as a pointer to the original value.

There should be nothing in Delphi that you can't do with C# and vice versa.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
you can save all the trouble if you just expose a COM interface.
That way the DLL can be consumed in *any* language (that knows about COM)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thank you much! Both of you two are all over these forums and have answered almost everything I've asked, do y'all work for Tek-Tips or something? If not, you should be..

But again, I haven't a clue how to declare const and var in C# as in my original Delphi function...

Code:
  function GetSomeData(const Input: PChar; Output: PChar; var Size: DWORD): Bool; StdCall;

I know that's a C# question...

JD Solutions
 
I think jmeckly already gave a response on that subject in the C# forum.
the C# counterpart for the delphi var keyword is 'ref'.
I stand with my original recommendation, just create a COM interface.
Importing COM objects is plain easy in .NET (or other environments).

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thank you...

I was thinking of building an ActiveX library. Delphi has automation to create an ActiveX component based on anything inherited from a TWinControl, and you can make them hidden as well. I'm plugging this API into a website actually in VisualStudio 2010 with ASP.NET 4.0 with C#.

JD Solutions
 
I was thinking of building an ActiveX library.

That's what I'm suggesting? not?

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
lol Yes it is, and that's exactly what I've started doing :p

JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top