简体   繁体   English

如何在delphi 7中使用setlocaltime?

[英]How to use setlocaltime in delphi 7?

A program containing changing time routine does change time when it runs in Borland Delphi 7 IDE.包含更改时间例程的程序在 Borland Delphi 7 IDE 中运行时确实会更改时间。 But it does not change time when it runs independently (outside the Delphi IDE).但是当它独立运行(在Delphi IDE之外)时,它不会改变时间。 I am using windows 7. Can you please help?我正在使用 Windows 7。你能帮忙吗? 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.我的超能力告诉我,您正在使用管理权限运行 Delphi 7 IDE。

If so, these are inherited by your application's process when it is started from the IDE.如果是这样,当您的应用程序从 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.但是,当然,当您通过在 Windows 资源管理器(例如)中双击其图标来运行您的应用程序时,它会以未提升的权限执行。

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.因为每次使用 Windows API 函数时,都会检查其返回值。 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 .要获取扩展错误信息,请调用GetLastError

So you should do所以你应该做

if not SetLocalTime(st) then
  RaiseLastOSError

which on my system tells me that I have insufficient privileges.这在我的系统上告诉我我没有足够的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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