我有一个Delphi DLL,我没有写,但需要从C#ASP.NET 3.5应用程序调用。下面是函数定义我从开发商得到:从C#调用Delphi DLL会产生意想不到的结果
function CreateCode(SerialID : String;
StartDateOfYear, YearOfStartDate, YearOfEndDate, DatePeriod : Word;
CodeType,RecordNumber,StartHour,EndHour : Byte) : PChar;
external 'CreateCodeDLL.dll';
,这里是我的C#代码:
[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);
最后,我调用此方法:
//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);
每次我重新编译这个确切的代码并运行它,它返回一个不同的值。期望值是由数字组成的10位数字代码。返回的值实际上是12位数字。
最后一个重要的信息是我有一个测试.EXE,它有一个GUI,允许我测试DLL。每个使用.EXE的测试都会返回相同的10位数字(预期结果)。
所以,我必须相信我已经错误地声明了我对DLL的调用。思考?
帕斯卡作为呼叫类型是行不通的,因为Delphi使用寄存器呼叫(也称为__fastcall未__msfastcall)。 – 2009-02-05 21:28:25
根据http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx .net不支持任何类型的FastCall。所以这意味着他必须要求他们更改pascal代码? – PetriW 2009-02-05 21:34:36