|
-
August 24th, 2001, 11:54 AM
#1
filetimetosystemtime won't work
Keep getting an error " Bad dll calling convention" Here's a sample of the code
Dim findinfo as WIN32_FIND_DATA
Dim hsearch as Long
Dim success as Long
Dim localtime as FILETIME
Dim systime as SYSTEMTIME
Dim retval as Long
Dim gFile as string
'create a buffer
findinfo.cFileName = string(MAX_PATH, 0)
'find the first file
hsearch = FtpFindFirstFile(hConnection, "*.*", findinfo, 0, 0)
'if there's no file, then exit sub
If hsearch = 0 then
Debug.print "(no files matched search parameter)"
End
End If
gFile = Left(findinfo.cFileName, InStr(1, findinfo.cFileName, string(1, 0), vbBinaryCompare) - 1)
Do
'create a buffer
findinfo.cFileName = string(MAX_PATH, 0)
' Convert UTC FILETIME to local SYSTEMTIME and display the date:
retval = FileTimeToLocalFileTime( findinfo.ftCreationTime, localtime)
'**********HERE'S WHERE I get THE error
retval = FileTimeToSystemTime(localtime, systime)
'*****************************************
Debug.print "date:"; systime.wMonth; "-"; _
systime.wDay; "-"; systime.wYear
'find the next file
success = InternetFindNextFile(hsearch, findinfo)
'show the filename
gFile = Left(findinfo.cFileName, InStr(1, findinfo.cFileName, string(1, 0), vbBinaryCompare)) '- 1)
Loop Until success = 0
'close the search handle
InternetCloseHandle hsearch
Exit Sub
Am I missing something. I'm new at using API and am trying to learn this stuff.
-
August 24th, 2001, 12:39 PM
#2
Re: filetimetosystemtime won't work
Here is a sample that works. Taken directly from MSDN article Q154821.
Add a command button and a text box to a form. Paste this code into the forms Grneral declaration section
option Explicit
private Const OF_READ = &H0
private Const OF_SHARE_DENY_NONE = &H40
private Const OFS_MAXPATHNAME = 128
private Type OFSTRUCTREC
cBytes as Byte
fFixedDisk as Byte
nErrCode as Integer
Reserved1 as Integer
Reserved2 as Integer
szPathName(OFS_MAXPATHNAME) as Byte
End Type
private Type FILETIMEREC
dwLowDateTime as Long
dwHighDateTime as Long
End Type
private Type SYSTEMTIMEREC
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 Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime as FILETIMEREC, lpSystemTime as SYSTEMTIMEREC) as Long
private Declare Function GetFileTime Lib "kernel32" (byval _
hFile as Long, lpCreationTime as FILETIMEREC, lpLastAccessTime _
as FILETIMEREC, lpLastWriteTime as FILETIMEREC) as Long
private Declare Function OpenFile Lib "kernel32" (byval lpFileName as _
string, lpReOpenBuff as OFSTRUCTREC, byval wStyle as Long) as Long
private Declare Function hread Lib "kernel32" Alias "_hread" _
(byval hFile as Long, lpBuffer as Any, byval lBytes as Long) as Long
private Declare Function lclose Lib "kernel32" Alias "_lclose" (byval _
hFile as Long) as Long
Sub Form_Load()
Command1.Caption = "&get file access time"
Text1.Text = "C:\AUTOEXEC.BAT"
End Sub
private Sub Command1_Click()
Dim sInpFile as string
Dim hFile as Integer
Dim FileStruct as OFSTRUCTREC
Dim iRC as Integer
Dim CreationTime as FILETIMEREC
Dim LastAccessTime as FILETIMEREC
Dim LastWriteTime as FILETIMEREC
Dim SystemTime as SYSTEMTIMEREC
sInpFile = Trim(Text1.Text)
' check that the file exists
If len(Dir(sInpFile)) = 0 then
MsgBox "Can't find the file", vbExclamation
Exit Sub
End If
' Open it to get a stream handle
hFile = OpenFile(sInpFile, FileStruct, OF_READ Or OF_SHARE_DENY_NONE)
If hFile = 0 then
MsgBox "Can't open the file", vbExclamation
Exit Sub
End If
If GetFileTime(hFile, CreationTime, _
LastAccessTime, LastWriteTime) then
' massage time into format that we can use
If Not FileTimeToSystemTime(LastAccessTime, SystemTime) then
print "Year of file :" & SystemTime.wYear
print "Month of File :" & SystemTime.wMonth
print "Day of File :" & SystemTime.wDay
else
MsgBox "FileTimeToSystemTime Failed"
End If
else
MsgBox "GetFileTime Failed"
End If
iRC = lclose(hFile)
End Sub
John G
-
August 24th, 2001, 02:48 PM
#3
Re: filetimetosystemtime won't work
Your sample works but it does not work with what I'm trying to do and I think I know why. When I debug print gfile it gives me ".. ". How do I remove this. I thought it was a carriage return but it's not. Any ideas.
-
August 24th, 2001, 03:49 PM
#4
Re: filetimetosystemtime won't work
The "bad Dll Calling convention" error indicates to me you have a screwed up structure somewhere. maybe a Integer where a Long should be defined which is usualle the case when using a 16 bit structure in a 32 bit environment.
At any rate here is another sample more in line with what you are trying to do. It uses FILETIMETOLOCALTIME followed by FILETIMETOSYSTEMTIME in the same sequence you are using. The only difference is where it gets its input structure from (GetFileTime instead of your FtpFindFirstFile.
'This program needs a Dialog box, named CDBox1
' (to add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
private Type FILETIME
dwLowDateTime as Long
dwHighDateTime as Long
End Type
private Type SHFILEOPSTRUCT
hWnd as Long
wFunc as Long
pFrom as string
pTo as string
fFlags as Integer
fAborted as Boolean
hNameMaps as Long
sProgress as string
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 Const GENERIC_WRITE = &H40000000
private Const OPEN_EXISTING = 3
private Const FILE_SHARE_READ = &H1
private Const FILE_SHARE_WRITE = &H2
private Const FO_DELETE = &H3
private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (byval lpExistingFileName as string, byval lpNewFileName as string, byval bFailIfExists as Long) as Long
private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (byval lpPathName as string, lpSecurityAttributes as Long) as Long
private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (byval lpFileName as string) as Long
private Declare Function GetFileSize Lib "kernel32" (byval hFile as Long, lpFileSizeHigh as Long) as Long
private Declare Function GetFileTime Lib "kernel32" (byval hFile as Long, lpCreationTime as FILETIME, lpLastAccessTime as FILETIME, lpLastWriteTime as FILETIME) as Long
private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (byval lpExistingFileName as string, byval lpNewFileName as string) as Long
private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (byval lpFileName as string, byval dwDesiredAccess as Long, byval dwShareMode as Long, lpSecurityAttributes as Long, byval dwCreationDisposition as Long, byval dwFlagsAndAttributes as Long, byval hTemplateFile as Long) as Long
private Declare Function CloseHandle Lib "kernel32" (byval hObject as Long) as Long
private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp as SHFILEOPSTRUCT) as Long
private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime as FILETIME, lpSystemTime as SYSTEMTIME) as Long
private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime as FILETIME, lpLocalFileTime as FILETIME) as Long
private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim lngHandle as Long, SHDirOp as SHFILEOPSTRUCT, lngLong as Long
Dim Ft1 as FILETIME, Ft2 as FILETIME, SysTime as SYSTEMTIME
'set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Raise an error when the user pressed cancel
CDBox.CancelError = true
'Show the 'Open File'-dialog
CDBox.ShowOpen
'Create a new directory
CreateDirectory "C:\KPD-Team", byval &H0
'Copy the selected file to our new directory
CopyFile CDBox.filename, "C:\KPD-Team\" + CDBox.FileTitle, 0
'Rename the file
MoveFile "C:\KPD-Team\" + CDBox.FileTitle, "C:\KPD-Team\test.kpd"
'Open the file
lngHandle = CreateFile("C:\KPD-Team\test.kpd", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, byval 0&, OPEN_EXISTING, 0, 0)
'get the file's size
MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."
'get the fil's time
GetFileTime lngHandle, Ft1, Ft1, Ft2
'Convert the file time to the local file time
FileTimeToLocalFileTime Ft2, Ft1
'Convert the file time to system file time
FileTimeToSystemTime Ft1, SysTime
MsgBox "The selected file was created on" + Str$(SysTime.wMonth) + "/" + LTrim(Str$(SysTime.wDay)) + "/" + LTrim(Str$(SysTime.wYear))
'Close the file
CloseHandle lngHandle
'Delete the file
DeleteFile "C:\KPD-Team\test.kpd"
With SHDirOp
.wFunc = FO_DELETE
.pFrom = "C:\KPD-Team"
End With
'Delete the directory
SHFileOperation SHDirOp
End
End Sub
John G
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|