如何在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;
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;
解决方案 »
- 如何获取access数据库中表的列名,并且能根据第几列这样来引用该列的数据。
- 给一个中国象棋棋谱管理软件搜索思路!高分相赠!
- 有什么好方法连接远程的SQL SERVER
- [求助]怎样能把表1的某一数据关联到表2中?
- 直接将当前记录编程第N条记录,TClientDataSet,咱整?『五点半之前结帖』
- 小第刚学一天DELPHI,大家帮帮忙吧(100分)----->在线等!!!!
- 请问表格和图标怎么做?
- 新手请教如何编程实现目录共享?谢谢(无内容)
- ???????大问题。。。。。那位知道Delphi6企业版的SN?
- 放在窗体上的ClientDataSet如何一次清空?
- 怎么把OleContainer1里的WORD文档第一行内容要出来?
- 内存中有两个字符串string1和string2,CPU要对它门按位异或,知到string1的内容和地址,怎么能得到string2的地址和内容?
DateTimeToSystemTime(DateTime,systemtime); //把Delphi的TDateTime格式转化为API的TSystemTime格式
SetLocalTime(SystemTime); //设置系统时间
GetLocalTime(SystemTime); //读取系统时间
DateTime:=SystemTimeToDateTime(SystemTime); //把API的TSystemTime格式 转化为 Delphi的TDateTime格式
Edit2.Text:=DateTimeToStr(DateTime); //显示当前系统的时间