简体   繁体   中英

How to use setlocaltime in delphi 7?

A program containing changing time routine does change time when it runs in Borland Delphi 7 IDE. But it does not change time when it runs independently (outside the Delphi IDE). I am using windows 7. Can you please help? Thank you.

...
procedure TForm1.changetime;

begin
  DateSeparator:='-';
  ShortDateFormat:='yyyy-MM-dd';
  LongDateFormat:='yyyy''Year'',MM''Month'',dd''Date''';
  TimeSeparator:=':';
  DateTimetoString(date,'yyyy-mm-dd',now);
  DateTimetoString(time,'hh:nn:ss',now);
  cd:='2014-06-01 '+time;
  d1:=StrToDateTime(cd);
  DateTimeToSystemTime(d1,systemtime);
  SetLocalTime(SystemTime);
end;
...

My psychic powers tell me that you are running the Delphi 7 IDE with administrative privileges.

If so, these are inherited by your application's process when it is started from the IDE. But, of course, when you run your application by double-clicking its icon in Windows Explorer (for instance), it is executed with unelevated privileges.

And to change the system time, you need elevated privileges. This explains the observed behaviour.

When you want to start your application from outside the debugger, make sure to run it elevated. For instance, you can right-click its icon and select "Run as administrator".

Actually, you could almost have figured this out yourself. Because every time you use a Windows API function, you check its returned value. From the documentation :

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError .

So you should do

if not SetLocalTime(st) then
  RaiseLastOSError

which on my system tells me that I have insufficient privileges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM