C
Comcast Newsgroups
Hello everyone,
I am programming in Visual Studio 2002, VB.net. I typically stick to
version 1.0 of the framework for compatibility reasons. Recently, I ran
into an issue that I couldn't solve with the framework. I am working with
file paths that exceed that MAX_PATH ANSI limitation in the framework. I
have to Windows API Unicode functions.
I am having an issue with the FindFirstFile API function. I am using it to
determine whether a file or directory exists. Interestingly, if I pass a
directory with a "*" wildcard at the end, the function does return a handle
to the directory. For example, \\?\C:\temp\* returns a valid handle. If I
pass a file, such as \\?\C:\temp\test.xls, I cannot obtain a handle, and the
FindFirstFile returns INVALID_HANDLE_VALUE.
I have the FindFirstFile function declared as follows:
<DllImport("kernel32", EntryPoint:="FindFirstFileW",
CharSet:=CharSet.Unicode, SetLastError:=True, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Shared Function FindFirstFile(ByVal lpFileName As String, <Out()> ByVal
lpFindFileData As WIN32_FIND_DATA) As IntPtr
WIN32_FIND_DATA is wrapped in a Class and so is FILETIME. This allows me
to pass both types as references to the API function, which I don't believe
I can do with structures.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class WIN32_FIND_DATA'
Private Const MAX_PATH As Integer = 1024
Public dwFileAttributes As Long
Public ftCreationTime As FILETIME
Public ftLastAccessTime As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Long
Public nFileSizeLow As Long
Public dwReserved0 As Long
Public dwReserved1 As Long
<VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> Public cFileName As String ' MAX_PATH - full filename
<VBFixedString(14), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
Public cAlternateFileName As String ' Dos Name
.........
End Class
I then create a new object of WIN32_FIND_DATA class, and pass it to the
FindFirstFile function, with a string path of the file as such:
lFileHdl = FindFirstFile("\\?\" & sFile, lpFindFileData)
lFileHdl is of type IntPtr and is always -1, unless I pass a Directory with
a "*" wildcard at the end. I know the file exists.
Can anyone shine some light on this?
Thank you in advance,
dmitry
I am programming in Visual Studio 2002, VB.net. I typically stick to
version 1.0 of the framework for compatibility reasons. Recently, I ran
into an issue that I couldn't solve with the framework. I am working with
file paths that exceed that MAX_PATH ANSI limitation in the framework. I
have to Windows API Unicode functions.
I am having an issue with the FindFirstFile API function. I am using it to
determine whether a file or directory exists. Interestingly, if I pass a
directory with a "*" wildcard at the end, the function does return a handle
to the directory. For example, \\?\C:\temp\* returns a valid handle. If I
pass a file, such as \\?\C:\temp\test.xls, I cannot obtain a handle, and the
FindFirstFile returns INVALID_HANDLE_VALUE.
I have the FindFirstFile function declared as follows:
<DllImport("kernel32", EntryPoint:="FindFirstFileW",
CharSet:=CharSet.Unicode, SetLastError:=True, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Shared Function FindFirstFile(ByVal lpFileName As String, <Out()> ByVal
lpFindFileData As WIN32_FIND_DATA) As IntPtr
WIN32_FIND_DATA is wrapped in a Class and so is FILETIME. This allows me
to pass both types as references to the API function, which I don't believe
I can do with structures.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class WIN32_FIND_DATA'
Private Const MAX_PATH As Integer = 1024
Public dwFileAttributes As Long
Public ftCreationTime As FILETIME
Public ftLastAccessTime As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Long
Public nFileSizeLow As Long
Public dwReserved0 As Long
Public dwReserved1 As Long
<VBFixedString(MAX_PATH), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH)> Public cFileName As String ' MAX_PATH - full filename
<VBFixedString(14), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
Public cAlternateFileName As String ' Dos Name
.........
End Class
I then create a new object of WIN32_FIND_DATA class, and pass it to the
FindFirstFile function, with a string path of the file as such:
lFileHdl = FindFirstFile("\\?\" & sFile, lpFindFileData)
lFileHdl is of type IntPtr and is always -1, unless I pass a Directory with
a "*" wildcard at the end. I know the file exists.
Can anyone shine some light on this?
Thank you in advance,
dmitry