Calling Delphi DLL from C# not passing integer correctly.

S

silentbazz

I have method in a Delphi library (Delphi 2007) that is declared thus:

function ReturnValuation(OurRef : integer) : double; export;

In my C# console application (Visual Studio 2005, .NET 2.0) I use the
DLLImport attribute to declare this method:

[DllImport(@"C:\PP\AutoValLibrary.dll", EntryPoint =
"ReturnValuation", CallingConvention = CallingConvention.StdCall)]
public static extern double ReturnValuation(int OurRef);

In my C# code I do the following:

int OurRef = 829366;
progress = ReturnValuation(OurRef).ToString();

However when I get the Delphi DLL to output the integer that has been
passed in, I see that it thinks it has been passed 9539304.

Quite simply I am confused as to why this is happening, I have tried
to simplify what's passed into the DLL as I know there are issues with
certain types between C# and Delphi, but I honestly thought I'd be
okay with an integer?

Can anyone spot what might be wrong with my code
 
B

Ben Voigt [C++ MVP]

I have method in a Delphi library (Delphi 2007) that is declared thus:

function ReturnValuation(OurRef : integer) : double; export;

In my C# console application (Visual Studio 2005, .NET 2.0) I use the
DLLImport attribute to declare this method:

[DllImport(@"C:\PP\AutoValLibrary.dll", EntryPoint =
"ReturnValuation", CallingConvention = CallingConvention.StdCall)]
public static extern double ReturnValuation(int OurRef);

In my C# code I do the following:

int OurRef = 829366;
progress = ReturnValuation(OurRef).ToString();

However when I get the Delphi DLL to output the integer that has been
passed in, I see that it thinks it has been passed 9539304.

Quite simply I am confused as to why this is happening, I have tried
to simplify what's passed into the DLL as I know there are issues with
certain types between C# and Delphi, but I honestly thought I'd be
okay with an integer?

Can anyone spot what might be wrong with my code


How big is a Delphi integer? Maybe you need C# short, which is the same as
VB6 Integer?

Is Delphi using pass by value by default?
 
P

Peter Morris

Integer in Delphi is a signed 32 bit number.

I don't recall, is there a way to explicitly tell Delphi to use StdCall on
that method, just to be sure?
 
D

Doug Forster

Hi Peter,

Try this:

function ReturnValuation(i: integer): double; stdcall;

stcall is NOT the default in Delphi

Cheers
Doug Forster
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top