type
TTODInfo = record
ElapsedTime: Integer; {number of seconds since 00:00:00 January 1, 1970}
Milliseconds: Integer; {number of milliseconds since last system reset}
Hours: Integer; {current hour (0-23)}
Minutes: Integer; {current minute (0-59)}
Seconds: Integer; {current second (0-59)}
Hunds: Integer; {current hundredth of a second (0-99)}
TimeZone: Integer; {time against GMT in minutes}
{west of Greenwich gives positive, east negative values}
{value of -1 means undefined time zone}
Interval: Integer; {clock tick interval in ten-thousandth of a second (0.0001 s)}
Day: Integer; {day of the month (1-31)}
Month: Integer; {month of the year (1-12)}
Year: Integer; {year}
Weekday: Integer; {day of the week (0-6) 0 = Sunday, 1 = Monday etc.}
end;
PTODInfo = ^TTODInfo;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
netapi32 = 'netapi32.dll';
function NetApiBufferFree(Buffer: Pointer): Integer; stdcall;
function NetRemoteTOD(UNCServerName: PWideChar; Info: Pointer): Integer; stdcall;
function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;
implementation
{$R *.dfm}
function NetApiBufferFree; external netapi32 name 'NetApiBufferFree';
function NetRemoteTOD; external netapi32 name 'NetRemoteTOD';
function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;
var
ServerName: PWideChar;
tod: PTODInfo;
Year, Month, Day, Hour, Min, Sec, MSec: Word;
begin
GetMem(ServerName, (Length(UNCServer) + 1) * SizeOf(WideChar));
try
ServerName := StringToWideChar(UNCServer, ServerName, Length(UNCServer) + 1);
Result := NetRemoteTOD(ServerName, @tod);
if Result = 0 then
begin
try
Year := tod^.Year;
Month := tod^.Month;
Day := tod^.Day;
Hour := tod^.Hours;
Min := tod^.Minutes;
Sec := tod^.Seconds;
MSec := tod^.Hunds * 10;
if tod^.TimeZone = -1 then {undefined timezone}
Stamp := EncodeDate(Year, Month, Day) +
EncodeTime(Hour, Min, Sec, MSec)
else
Stamp := EncodeDate(Year, Month, Day) +
EncodeTime(Hour, Min, Sec, MSec) - (tod^.TimeZone / 1440);
finally
NetApiBufferFree(tod);
end;
end;
finally
FreeMem(ServerName);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t: TDateTime;
begin
ServerTime('server', t);
label1.Caption := DateTimeToStr(t);
end;
//dll部分例子
library Dll;
uses
SysUtils,
Classes,
DllUnit in 'DllUnit.pas' {Form1};
{$R *.RES}
function CreateDllForm(AWoner:TComponent):Pointer; stdcall;
begin
Result:=TForm1.Create(AWoner);
end;
exports
CreateDllForm;
begin
end.
//调用方例子
unit Unit1;