2 getdata
long getdata (long n1, char *command , long n2, char * getdata, char * device)
n1 命令长度
command 命令字(1字节机号+1字节命令+0x55+0x55+0x00)
n2 接收数据长度
getdata 接收数据缓冲区
device 串口名(如"COM1","COM2"等大写) 返回值: 0 成功
1 发送机号错
2 写命令字错或超时
3 收数据超时
4 收数据错
請問我要在delphi中調用getdate()函數,應該在delphi中如何傳遞getdate()的參數,先謝
long getdata (long n1, char *command , long n2, char * getdata, char * device)
n1 命令长度
command 命令字(1字节机号+1字节命令+0x55+0x55+0x00)
n2 接收数据长度
getdata 接收数据缓冲区
device 串口名(如"COM1","COM2"等大写) 返回值: 0 成功
1 发送机号错
2 写命令字错或超时
3 收数据超时
4 收数据错
請問我要在delphi中調用getdate()函數,應該在delphi中如何傳遞getdate()的參數,先謝
function getdata (n1: LongInt; command: PChar; n2: LongInt; getdata: PChar; device: PChar): LongInt; stdcall; external 'xxxx.dll' name 'getdata';然后就可调用了。
有2点说明:
1、要搞清楚getdata函数在dll中的调用方式,我假定其为stdcall(大多数属stdcall);
2、如果传递string到PChar,将string类型变量用PChar(变量)传递就行了
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall; { Create a new exception class to reflect a failed DLL load }
EDLLLoadError = class(Exception); TMainForm = class(TForm)
lblDate: TLabel;
btnGetCalendar: TButton;
procedure btnGetCalendarClick(Sender: TObject);
end;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
LibHandle : THandle;
ShowCalendar: TShowCalendar;
begin { Attempt to load the DLL }
LibHandle := LoadLibrary('CALENDARLIB.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@ShowCalendar = nil) then
lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;end.
unit DLLFrm;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm)
calDllCalendar: TCalendar;
procedure calDllCalendarDblClick(Sender: TObject);
end;{ Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
{$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
try
DLLForm.Caption := ACaption;
DLLForm.ShowModal;
Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
finally
DLLForm.Free;
end;
end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
begin
Close;
end;end.