Alright, I was writing a program to automatically generate DECLARE statements for any SUBs/FUNCTIONs in a file/in a group of files and I needed to use three Windows API functions: FindFirstFile, FindNextFile and FindClose, as follows:
Code:
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Integer
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Integer, lpFindFileData As WIN32_FIND_DATA) As Integer
Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Integer) As Integer
These functions take a parameter of type WIN32_FIND_DATA (who requires FILETIME as well):
Code:
Type FILETIME
dwLowDateTime As uinteger
dwHighDateTime As uinteger
End Type
Type WIN32_FIND_DATA
dwFileAttributes As uinteger
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As uinteger
nFileSizeLow As uinteger
dwReserved0 As uinteger
dwReserved1 As uinteger
cFileName As String * 260 - 1
cAlternate As String * 14 - 1
End Type
Now, I've got my functions and their UDTs. So let's give them a call:
Code:
Dim FindHandle As Integer
Dim FindData As WIN32_FIND_DATA
FindHandle = FindFirstFile("*.*", FindData)
If FindHandle <> -1 Then
Do
' I need to print the file name:
Print FindData.cFileName
Loop While FindNextFile(FileHandle, FindData)
End If
However, that code does not work... the filenames printed are not the whole filenames - they are missing their first four characters. I suspect this is because the strings in FB are stored as (size, data), where size is a four-byte long integer. Since Windows copies strings directly to their address, it copies the first four characters into this unaccessible area of the string. So I had to work around this by doing the following:
Code:
' MAX_PATH is usually 260
Dim A As String * MAX_PATH Pointer
' Point to the actual string Windows stored, not the cut version:
A = VarPtr(FindData.cFileName) - Len(Long)
Print *A
This worked just fine. This took me quite some time to figure out, though, and could be a serious pain in Windows programming with FB. It's just a suggestion, but might it be possible to have a CSTRING data type, that stores its strings as NULL-terminated values? It'd be a little weird, but it would work.