【发布时间】:2009-02-05 21:14:27
【问题描述】:
我有一个我没有编写的 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,它有一个允许我测试 DLL 的 GUI。使用 .EXE 的每个测试都返回相同的 10 位数字(预期结果)。
所以,我必须相信我错误地声明了对 DLL 的调用。想法?
【问题讨论】: