如何在window 2000 server中,登錄用戶是來賓用戶,在delphi中如何更改系統時間?我做了兩個函數,可無法更改系統時間,源代碼如下:
function SetPrivilege(sPrivilegeName : string; bEnabled : boolean ):boolean;
var
  TPPrev,
  TP         : TTokenPrivileges;
  Token      : THandle;
  dwRetLen   : DWord;
  WinVersion : TOSVersionInfo;
begin
  Result := False;
  { These API calls only work on a WIndows NT machine, return true
    automatically if running on Windows 95/98 }
  WinVersion.dwOSVersionInfoSize := SizeOf(OSVERSIONINFO);
  if GetVersionEx(WinVersion) then
  begin
    if WinVersion.dwPlatformId = VER_PLATFORM_WIN32_NT then
    begin
      if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES
                      or TOKEN_QUERY, Token) then
      begin
        TP.PrivilegeCount := 1;
        if(LookupPrivilegeValue(Nil, PChar(sPrivilegeName),
                                TP.Privileges[ 0 ].LUID))then
        begin
          if( bEnabled )then
          begin
            TP.Privileges[ 0 ].Attributes  := SE_PRIVILEGE_ENABLED;
          end else
          begin
            TP.Privileges[ 0 ].Attributes  := 0;
          end;
          dwRetLen := 0;
          Result := AdjustTokenPrivileges(Token, False, TP,SizeOf( TPPrev ),
                    TPPrev, dwRetLen );
        end;
        CloseHandle( Token );
      end;
    end
    else
      result := True;
  end;
end;function SetPCSystemTime(tDati: TDateTime): DWord;
var
   tSetDati: TDateTime;
   vDatiBias: Variant;
   tTZI: TTimeZoneInformation;
   tST: TSystemTime;
begin
   { Windows stores it's date and time in UTC format (Coordinated Universal
     Time). We need to get the adjustment required for the current time zone
     apply it to the incoming date and time and set the time in UTC format     Before we can set the system time in Windows NT we also need to grant
     ourselves the security priveleges to do it}   if(SetPrivilege('SeSystemtimePrivilege', True ))then
   begin
     GetTimeZoneInformation(tTZI);
     vDatiBias := tTZI.Bias / 1440;
     tSetDati := tDati + vDatiBias;
     with tST do
     begin
        wYear := StrToInt(FormatDateTime('yyyy', tSetDati));
        wMonth := StrToInt(FormatDateTime('mm', tSetDati));
        wDay := StrToInt(FormatDateTime('dd', tSetDati));
        wHour := StrToInt(FormatDateTime('hh', tSetDati));
        wMinute := StrToInt(FormatDateTime('nn', tSetDati));
        wSecond := StrToInt(FormatDateTime('ss', tSetDati));
        wMilliseconds := 0;
     end;
     if SetSystemTime(tST) then
     begin
        { Windows API documentation states that you should
          notify all top level windows that you have changed
          the date and time }
        PostMessage(HWND_TOPMOST, WM_TIMECHANGE, 0, 0);
        SetPCSystemTime := 0;
     end
     else
        SetPCSystemTime := GetLastError;
     SetPrivilege('SeSystemtimePrivilege',False);
   end
   else
     SetPCSystemTime := GetLastError;
end;

解决方案 »

  1.   

    DateTime:=now();//获得时间(TDateTime格式)
      DateTimeToSystemTime(DateTime,systemtime);  //把Delphi的TDateTime格式转化为API的TSystemTime格式
      SetLocalTime(SystemTime);         //设置系统时间
      GetLocalTime(SystemTime);       //读取系统时间
      DateTime:=SystemTimeToDateTime(SystemTime);  //把API的TSystemTime格式 转化为 Delphi的TDateTime格式
      Edit2.Text:=DateTimeToStr(DateTime); //显示当前系统的时间