我有一个不是我编写的 Delphi DLL,但需要从 C# ASP.NET 3.5 应用程序调用。这是我从开发人员那里得到的函数定义:
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 的调用。想法?