GetFileTime和FileTimeToSystemTime的问题 (60分)

  • 主题发起人 主题发起人 ili
  • 开始时间 开始时间
I

ili

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
var<br>&nbsp; FileHandle : Integer;<br>&nbsp; CreationTime, LastAccessTime, LastWriteTime: PFileTime;<br>&nbsp; CTime: PSystemTime;<br>begin<br>&nbsp; FileHandle := FileOpen(Setting.FileName, fmOpenWrite or fmShareDenyNone);<br>&nbsp; GetFileTime(FileHandle, CreationTime, LastAccessTime, LastWriteTime);<br>&nbsp; FileTimeToSystemTime(LastWriteTime, CTime); &nbsp;// 463行<br>&nbsp; ...<br>end;
<br><br>Setting.FileName是我指定的文件,string类型;<br>现在出现错误:[red][Error] Main.pas(463): Incompatible types: '_FILETIME' and 'PFileTime'[/red]<br>怎么回事?我查遍了delphi的sdk帮助和msdn,觉得都没错啊~~<br>帮忙!
 
function FileTimeToSystemTime(const lpFileTime: TFileTime; var lpSystemTime: TSystemTime): BOOL; stdcall;<br>{$EXTERNALSYM FileTimeToSystemTime}<br><br>是TSystemTime<br>Delphi里的变参传递时就是指针,所以很多在API里是指针的参数,在Delphi封装后就不用指针了,要注意奥
 
不太明白你的意思~,因为function GetFileTime(hFile: THandle;<br>&nbsp; lpCreationTime, lpLastAccessTime, lpLastWriteTime: PFileTime): BOOL; stdcall;<br>{$EXTERNALSYM GetFileTime},这个函数需要指针类型啊。如果我把类型改为不用指针类型,结果仍错,如下:<br>
代码:
var<br>&nbsp; FileHandle : Integer;<br>&nbsp; CreationTime, LastAccessTime, LastWriteTime: FileTime; &nbsp;//这里改了<br>&nbsp; CTime: SystemTime; &nbsp;//这里改了<br>begin<br>&nbsp; FileHandle := FileOpen(Setting.FileName, fmOpenWrite or fmShareDenyNone);<br>&nbsp; GetFileTime(FileHandle, CreationTime, LastAccessTime, LastWriteTime); &nbsp;//462行<br>&nbsp; FileTimeToSystemTime(LastWriteTime, CTime);<br>&nbsp; ...<br>end;
<br>[red][Error] Main.pas(462): Incompatible types: '_FILETIME' and 'PFileTime'<br>[Error] Main.pas(462): Incompatible types: '_FILETIME' and 'PFileTime'<br>[Error] Main.pas(462): Incompatible types: '_FILETIME' and 'PFileTime'[/red]<br><br>如果改为如下,编译通过,但运行错误:<br>
代码:
var<br>&nbsp; FileHandle : Integer;<br>&nbsp; CreationTime, LastAccessTime, LastWriteTime: PFileTime;<br>&nbsp; LastWriteTime2: TFileTime; &nbsp;//加了个变量<br>&nbsp; CTime: TSystemTime;<br>begin<br>&nbsp; FileHandle := FileOpen(Setting.FileName, fmOpenWrite or fmShareDenyNone);<br>&nbsp; GetFileTime(FileHandle, CreationTime, LastAccessTime, LastWriteTime);<br>&nbsp; LastWriteTime2 := LastWriteTime^; &nbsp;//运行时错误就在这儿,错误是[red]EAccessViolation[/red]<br>&nbsp; FileTimeToSystemTime(LastWriteTime2, CTime);<br>&nbsp; ...<br>end;
怎么回事?
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;FileHandle : Integer;<br>&nbsp;CreationTime, LastAccessTime, LastWriteTime: PFileTime; &nbsp;//这里改了<br>&nbsp;CTime: TSystemTime; &nbsp;//这里改了<br>&nbsp;FileName : string;<br>begin<br>&nbsp;FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);<br>&nbsp;GetMem(CreationTime,SizeOf(PFileTime));//没有给指针分配内存<br>&nbsp;GetMem(LastAccessTime,SizeOf(PFileTime));<br>&nbsp;GetMem(LastWriteTime,SizeOf(PFileTime));<br>&nbsp;GetFileTime(FileHandle, CreationTime, LastAccessTime, LastWriteTime); &nbsp;//462行<br>&nbsp;FileTimeToSystemTime(LastWriteTime^, CTime);<br>&nbsp;ShowMessage(IntToSTr(CTime.wYear));<br>&nbsp;FreeMem(CreationTime);<br>&nbsp;FreeMem(LastAccessTime);<br>&nbsp;FreeMem(LastWriteTime);<br>end;
 
非常感谢realLearning。<br>[brown]但还有个问题,就是这样显示出来的时间(不管是三个里的哪个),总是1601-1-1 1:25:54,不管我把Setting.FileName设为哪个文件~<br>怎么回事?[/brown]<br>[red]不好意思,刚才有个参数搞错了~:p[/red]
 
给你两个转换函数。<br><br>function FileTimeToDateTime(FileTime: TFileTime): TDateTime;<br>var<br>&nbsp; SysTime &nbsp; &nbsp; &nbsp;: TSystemTime;<br>begin<br>&nbsp; if not FileTimeToSystemTime(FileTime, SysTime) then<br>&nbsp; &nbsp; raise EConvertError.CreateFmt('FileTimeToSystemTime failed. ' +<br>&nbsp; &nbsp; &nbsp; 'Error code %d', [GetLastError]);<br>&nbsp; with SysTime do<br>&nbsp; &nbsp; Result := EncodeDate(wYear, wMonth, wDay) +<br>&nbsp; &nbsp; &nbsp; EncodeTime(wHour, wMinute, wSecond, wMilliseconds);<br>end;<br><br>function DateTimeToFileTime(DateTime: TDateTime): TFileTime;<br>var<br>&nbsp; SysTime &nbsp; &nbsp; &nbsp;: TSystemTime;<br>begin<br>&nbsp; with SysTime do<br>&nbsp; begin<br>&nbsp; &nbsp; DecodeDate(DateTime, wYear, wMonth, wDay);<br>&nbsp; &nbsp; DecodeTime(DateTime, wHour, wMinute, wSecond, wMilliseconds);<br>&nbsp; &nbsp; wDayOfWeek := DayOfWeek(DateTime);<br>&nbsp; end;<br>&nbsp; if not SystemTimeToFileTime(SysTime, Result) then<br>&nbsp; &nbsp; raise EConvertError.CreateFmt('SystemTimeToFileTime failed. ' +<br>&nbsp; &nbsp; &nbsp; 'Error code %d', [GetLastError]);<br>end;<br>
 
多人接受答案了。
 
后退
顶部