Delphi:向TFileTime添加常量

-1 delphi unsigned casting int64

我是Delphi的新手,我需要在项目中向TFileTime添加一些常数,该常数记录了64位值的上下部分。如何在Delphi中做到这一点?我只在C ++中找到代码,但是我不知道在Delphi中如何使unsigned int64(ULONGLONG)生成,而且我也不知道如何将其转换为longword(DWORD):

ULONGLONG qwResult;

// Copy the time into a quadword.
qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

// Add constant
qwResult += constant;

// Copy the result back into the FILETIME structure.
ft.dwLowDateTime  = (DWORD) (qwResult & 0xFFFFFFFF );
ft.dwHighDateTime = (DWORD) (qwResult >> 32 );
Run Code Online (Sandbox Code Playgroud)

谢谢

Dav*_*nan 5

FILETIME结构定义为:

typedef struct _FILETIME {
  DWORD dwLowDateTime;
  DWORD dwHighDateTime;
} FILETIME, *PFILETIME;
Run Code Online (Sandbox Code Playgroud)

因此,由于Windows在Little Endian上运行,因此此结构的布局与64位整数值兼容。

因此,您可以将转换TFileTimeUInt64,进行算术运算,然后回退。像这样:

function IncrementedFileTime(const Value: TFileTime; const Incr: UInt64): TFileTime;
begin
  Result := TFileTime(UInt64(Value) + Incr);
end;
Run Code Online (Sandbox Code Playgroud)

现在,FILETIME记录文档显示:

不建议您从FILETIME结构中添加和减去值以获得相对时间。而是应将文件时间的低位和高位部分复制到ULARGE_INTEGER结构,对QuadPart成员执行64位算术,然后将LowPart和HighPart成员复制到FILETIME结构中。

不要将指向FILETIME结构的指针转换为ULARGE_INTEGER *或__int64 *值,因为它可能导致64位Windows上的对齐错误。

这是目标上的问题,其中对准错误会导致严重故障。例如Itanium。但是,在x86和x64上,我的答案中的代码很好,因为这些体系结构不会针对对齐错误发出硬故障。同样好,因为Delphi编译器的对齐方式不是很好。