用GetFileTime 对user32.dll 、gdi32.dll 等API相关的几个DLL创建日期 取得结果不正确?

ewang11 2008-05-19 02:38:41
用GetFileTime 可以得到文件的创建日期信息,但对user32.dll 、gdi32.dll 等API相关的DLL的的创建日期 结果却是错误的
SetFileTime 也是一样

...全文
246 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ewang11 2008-06-02
  • 打赏
  • 举报
回复
并没有完全解决呀 各位老大帮忙看一下
ewang11 2008-05-22
  • 打赏
  • 举报
回复
hFile = CreateFile(StrPathName,GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)

上面的参数还是不能用SetFileTime对当前运行中的程序日期或winmm.dll的日期进行修改,感觉没有足够的权限

下面是我的代码
Public Function SetFileTimeCrated(StrPathName As String, FileSystemTime As SYSTEMTIME) As Boolean
'设置文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME

hFile = OpenFile(StrPathName, OF, OF_READWRITE Or OF_SHARE_DENY_NONE) '这样可以对自身的程序的日期进行修改,不能实现对API相关DLL的日期的修改
' hFile = CreateFile(StrPathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
'hFile = CreateFile(StrPathName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)


If hFile = 0 Then
'If hFile = -1 Then
MsgBox "系统文件出错"
Exit Function
End If

Retval = GetFileTime(hFile, CreatedFT, AccessedFT, ModifiedFT)

SystemTimeToFileTime FileSystemTime, udtLocalTime
LocalFileTimeToFileTime udtLocalTime, udtFileTime
Retval = SetFileTime(hFile, udtFileTime, AccessedFT, ModifiedFT)

CloseHandle hFile
End Function
ewang11 2008-05-22
  • 打赏
  • 举报
回复
我先试试,最终目的是通过SetFileTime把修改后的日期再写回去
dingdongX 2008-05-21
  • 打赏
  • 举报
回复
hFile = CreateFile(StrPathName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
上面 这个仅仅是读
-----------------------
Private Const GENERIC_WRITE As Long = &H40000000
hFile = CreateFile(StrPathName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
这个是写
----------------------
hFile = CreateFile(StrPathName,GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
这个是读写
VBAdvisor 2008-05-21
  • 打赏
  • 举报
回复


根本不需要CreateFile,打开就行了,结果是 2002-8-29 20:00:00

'Call:
Dim hfile As Long

hfile = GetFileHandle("C:\WINDOWS\system32\user32.dll", True)
Debug.Print FileCreationTime(hfile) '2002-8-29 20:00:00
CloseHandle hfile

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Declare Function GetFileTime Lib "kernel32" (ByVal hfile As Long, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As Long

Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function VariantTimeToSystemTime Lib "OLEAUT32.DLL" (ByVal vtime As Double, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToVariantTime Lib "OLEAUT32.DLL" (lpSystemTime As SYSTEMTIME, vtime As Double) As Long


Public Function FileCreationTime(ByVal hfile As Long) As Date
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME

' Fetch time stamp from open file
bSuccess = GetFileTime(hfile, dtFileTime, ByVal 0&, ByVal 0&)

' Convert FILETIME to a VB date value
FileCreationTime = FileTimeToDate(dtFileTime)

End Function

Public Function FileTimeToDate( _
FILETIME As FILETIME, _
Optional ByVal ConvertToLocal As Boolean = True) As Date

'===========================================================================
' FileTimeToDate - Converts FILETIME structure to a VB Date data type.
'
' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals
' since January 1, 1601. The VB Date data type is a floating point value
' where the value to the left of the decimal is the number of days since
' December 30, 1899, and the value to the right of the decimal represents
' the time.
'
' FileTime The FILETIME structure to convert.
' ConvertToLocal Optional. True to convert from UTC to local time.
'
' RETURNS A date/time value in the intrinsic VB Date data type.
'
'===========================================================================

Dim Success As Boolean
Dim SysTime As SYSTEMTIME
Dim ftdouble As Double

' Convert to FILETIME

Success = FileTimeToSystemTime(FILETIME, SysTime)
If Success Then
' Convert to a Variant date
Success = SystemTimeToVariantTime(SysTime, ftdouble)
End If

If Not Success Then
Err.Raise Err.Number, Err.Source, Err.Description
End If

' Return the result
FileTimeToDate = ftdouble

End Function
VBAdvisor 2008-05-21
  • 打赏
  • 举报
回复
详细见:
http://hi.baidu.com/vbadvisor/blog
VBAdvisor 2008-05-21
  • 打赏
  • 举报
回复
'对不起,少了GetFileHandle函数

Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
Private Declare Function IsWindowUnicode Lib "user32.dll" (ByVal hwnd As Long) As Long

Public Function GetFileHandle(ByVal FileName As String, bOpen As Boolean) As Long

' Function uses APIs to read/create files with unicode support

Const GENERIC_READ As Long = &H80000000
Const OPEN_EXISTING = &H3
Const FILE_SHARE_READ = &H1
Const GENERIC_WRITE As Long = &H40000000
Const FILE_SHARE_WRITE As Long = &H2
Const CREATE_ALWAYS As Long = 2
Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20
Const FILE_ATTRIBUTE_HIDDEN As Long = &H2
Const FILE_ATTRIBUTE_READONLY As Long = &H1
Const FILE_ATTRIBUTE_SYSTEM As Long = &H4

Dim Flags As Long, Access As Long
Dim Disposition As Long, Share As Long
Dim bISNT As Boolean


bISNT = (Not (IsWindowUnicode(GetDesktopWindow) = 0&))
If bOpen Then
Access = GENERIC_READ
Share = FILE_SHARE_READ
Disposition = OPEN_EXISTING
Flags = FILE_ATTRIBUTE_ARCHIVE Or FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_NORMAL _
Or FILE_ATTRIBUTE_READONLY Or FILE_ATTRIBUTE_SYSTEM
Else
Access = GENERIC_READ Or GENERIC_WRITE
Share = 0&
If bISNT Then
Flags = GetFileAttributesW(StrPtr(FileName))
Else
Flags = GetFileAttributesA(FileName)
End If
If Flags < 0& Then Flags = FILE_ATTRIBUTE_NORMAL
' CREATE_ALWAYS will delete previous file if necessary
Disposition = CREATE_ALWAYS
End If

If bISNT Then
GetFileHandle = CreateFileW(StrPtr(FileName), Access, Share, ByVal 0&, Disposition, Flags, 0&)
Else
GetFileHandle = CreateFileA(FileName, Access, Share, ByVal 0&, Disposition, Flags, 0&)
End If

If GetFileHandle = 0 Then
If isStringANSI(FileName) Then
' hFile should never be zero. It should be -1 (error) or a valid handle
' when hFile is zero, most likely API was called on a Win9x system
' so we will call the ANSI version and see if that returns a handle
GetFileHandle = CreateFileA(FileName, Access, Share, ByVal 0&, Disposition, Flags, 0&)
End If
End If

End Function
ewang11 2008-05-20
  • 打赏
  • 举报
回复
现在可以了
如果我用SetFileTime把修改后的语句再写回去,或者对当前程序的日期进行修改,用下面的参数好像不行

hFile = CreateFile(StrPathName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)
ewang11 2008-05-20
  • 打赏
  • 举报
回复
用上面的代码读取API几个DLL的创建日期结果都不正确, 读取其他文件的日期却正常
ewang11 2008-05-20
  • 打赏
  • 举报
回复
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long


Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName As String * OFS_MAXPATHNAME
End Type

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

'时区
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

Private Const OF_CREATE = &H1000 '
Private Const OF_DELETE = &H200
Private Const OF_PARSE = &H100
Private Const OF_PROMPT = &H2000
Private Const OF_READ = &H0
Private Const OF_READWRITE = &H2
Private Const OF_REOPEN = &H8000
Private Const OF_SHARE_COMPAT = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OF_SHARE_DENY_READ = &H30
Private Const OF_SHARE_DENY_WRITE = &H20
Private Const OF_SHARE_EXCLUSIVE = &H10
Private Const OF_VERIFY = &H400 '
Private Const OF_WRITE = &H1

Function GetFileTimeCreated(ByVal StrPathName As String) As SYSTEMTIME
'文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME

hFile = OpenFile(StrPathName, OF, OF_WRITE Or OF_SHARE_DENY_NONE)

If hFile = 0 Then
MsgBox "系统文件出错"
Exit Function
End If

Retval = GetFileTime(hFile, CreatedFT, AccessedFT, ModifiedFT)
FileTimeToLocalFileTime CreatedFT, udtLocalTime

FileTimeToSystemTime udtLocalTime, udtSystemTime
GetFileTimeCreated = udtSystemTime

CloseHandle hFile
End Function


下面为测试代码
Dim SysCreatTime As SYSTEMTIME
Dim StrDate As String
'SysCreatTime = GetFileTimeCreated("C:\test.txt") '读取这个能正常
SysCreatTime = GetFileTimeCreated("C:\WINDOWS\system32\user32.dll") '结果错误 显示为1601-1-1

StrDate = SysCreatTime.wYear & "-" & SysCreatTime.wMonth & "-" & SysCreatTime.wDay
MsgBox StrDate
of123 2008-05-20
  • 打赏
  • 举报
回复
权限问题吧?把文件复制到其他目录下试试。
dingdongX 2008-05-20
  • 打赏
  • 举报
回复
hFile = OpenFile(StrPathName, OF, OF_READ)
用OpenFile 这样使用才可以
dingdongX 2008-05-20
  • 打赏
  • 举报
回复
还有一个错误刚才忘记改正了
hFile = -1 为错误 无效句柄 不是0
dingdongX 2008-05-20
  • 打赏
  • 举报
回复
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME) As Long


Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName As String * OFS_MAXPATHNAME
End Type

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

'时区
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

Private Const OF_CREATE = &H1000 '
Private Const OF_DELETE = &H200
Private Const OF_PARSE = &H100
Private Const OF_PROMPT = &H2000
Private Const OF_READ = &H0
Private Const OF_READWRITE = &H2
Private Const OF_REOPEN = &H8000
Private Const OF_SHARE_COMPAT = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OF_SHARE_DENY_READ = &H30
Private Const OF_SHARE_DENY_WRITE = &H20
Private Const OF_SHARE_EXCLUSIVE = &H10
Private Const OF_VERIFY = &H400 '
Private Const OF_WRITE = &H1
Private Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Private Const GENERIC_READ = &H80000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3


Private Function GetFileTimeCreated(ByVal StrPathName As String) As SYSTEMTIME
'文件的创建时间
Dim Retval As Long, hFile As Long
Dim OF As OFSTRUCT
Dim CreatedFT As FILETIME, AccessedFT As FILETIME, ModifiedFT As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim udtLocalTime As FILETIME
Dim udtFileTime As FILETIME

' hFile = OpenFile(StrPathName, OF, OF_WRITE Or OF_SHARE_DENY_NONE)
hFile = CreateFile(StrPathName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0&)


If hFile = 0 Then
MsgBox "系统文件出错"
Exit Function
End If

Retval = GetFileTime(hFile, CreatedFT, AccessedFT, ModifiedFT)
FileTimeToLocalFileTime CreatedFT, udtLocalTime

FileTimeToSystemTime udtLocalTime, udtSystemTime
GetFileTimeCreated = udtSystemTime

CloseHandle hFile
End Function

Private Sub Form_Load()

Dim SysCreatTime As SYSTEMTIME
Dim StrDate As String
'SysCreatTime = GetFileTimeCreated("C:\test.txt") '读取这个能正常
SysCreatTime = GetFileTimeCreated("C:\WINDOWS\system32\user32.dll") '结果错误 显示为1601-1-1

StrDate = SysCreatTime.wYear & "-" & SysCreatTime.wMonth & "-" & SysCreatTime.wDay
MsgBox StrDate
End Sub

dingdongX 2008-05-19
  • 打赏
  • 举报
回复
user32.dll 创建时间 2004 | 8 | 23 | 16 | 0 | 0
dingdongX 2008-05-19
  • 打赏
  • 举报
回复
没发现你说的问题 ,贴一下你的结果或代码
你说的不是很清楚啊

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧