Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a Delphi DLL from C# produces unexpected results
    primarykey
    data
    text
    <p>I have a Delphi DLL that I did not write, but need to call from a C# ASP.NET 3.5 app. Here is the function definition I got from the developers:</p> <pre><code>function CreateCode(SerialID : String; StartDateOfYear, YearOfStartDate, YearOfEndDate, DatePeriod : Word; CodeType,RecordNumber,StartHour,EndHour : Byte) : PChar; external 'CreateCodeDLL.dll'; </code></pre> <p>And here is my C# code:</p> <pre><code>[DllImport( "CreateCodeDLL.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.Ansi)] public static extern IntPtr CreateCode( string SerialID, UInt16 StartDateOfYear, UInt16 YearOfStartDate, UInt16 YearOfEndDate, UInt16 DatePeriod, Byte CodeType, Byte RecordNumber, Byte StartHour, Byte EndHour); </code></pre> <p>And finally, my call to this method: </p> <pre><code>//The Inputs String serialID = "92F00000B4FBE"; UInt16 StartDateOfYear = 20; UInt16 YearOfStartDate = 2009; UInt16 YearOfEndDate = 2009; UInt16 DatePeriod = 7; Byte CodeType = 1; Byte RecordNumber = 0; Byte StartHour = 15; Byte EndHour = 14; // The DLL call IntPtr codePtr = CodeGenerator.CreateCode(serialID, StartDateOfYear, YearOfStartDate, YearOfEndDate, DatePeriod, CodeType, RecordNumber, StartHour, EndHour); // Take the pointer and extract the code in a string String code = Marshal.PtrToStringAnsi(codePtr); </code></pre> <p>Every time I re-compile this exact code and run it, it returns a different value. The expected value is a 10-digit code comprised of numbers. The returned value is actually 12 digits. </p> <p>The last important piece of information is that I have a test .EXE that has a GUI that allows me to test the DLL. Every test using the .EXE returns the same 10-digit number (the expected result).</p> <p>So, I have to believe that I have declared my call to the DLL incorrectly. Thoughts?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload