一个c/s系统怎么在c端获得s端机器的时间。并把这个时间设置为c端的系统时间。
为什么我用以下代码得到的时间不是服务端的系统时间。是不是s端数据库时间并不等于s端的系统时间?S端是MSSQL.function GetServerDateTime: string;
var
  aqSys: TADOQuery;
begin
  result := '';
  aqSys := TADOQuery.Create(nil);
  try
    aqSys.Connection :=form1.ADOConnection1;
    aqSys.SQL.Text := 'SELECT GETDATE() as sd';   
    try
      aqSys.Open;
      result := FormatDateTime('yyyy-mm-dd hh:nn:ss', aqSys.FieldByName('sd').AsDateTime);
    except
      raise;
    end;
  finally
    FreeAndNil(aqSys);
  end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
  SystemTime: TSystemTime;
begin
  if GetServerDateTime = '' then Exit;
  DateTimeToSystemTime(StrToDatetime(GetServerDateTime), SystemTime);
  SetSystemTime(SystemTime);
end;

解决方案 »

  1.   

    函数没有问题,我也是这样用的.
    就看你下面的这个click对不对了.
      

  2.   

    这段代码,我试过,没问题的:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ExtCtrls, FR_View, FR_Class;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)
        Label1: TLabel;
        BitBtn1: TBitBtn;
        procedure BitBtn1Click(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.BitBtn1Click(Sender: TObject);
    var
      t: TDateTime;
    begin
      ServerTime('172.21.165.16', t);
      label1.Caption := DateTimeToStr(t);
      showmessage(formatdatetime('yyyy/mm/dd hh:nn:ss',t));end;end.
      

  3.   

    问题已经解决是SetSystemTime函数的问题。时区不对。