V
vighnesh
Hello Everyone
I am developing a project in VB.NET in which I need to use Win32 API
function FindFirstFile()/FindNextFile()/FindClose()
The same code is wroking fine in VB 6.0 , but when I used the code in VB.NET
its giving an error saying that "Object reference is not set to an instance"
at the line where the FindFirstFIle() API is used. Can Anybody tell me why
its happing like this.
Thanks in Advance .
Here I am posting the code I wrote along with the error I got.
Module ProgramShortcuts
Public Structure mctFileSearchResults
Dim FileCount As Long
Dim FileSize As Decimal
Dim Files() As String
End Structure
Private Const MAX_PATH = 260
Private Const MAXDWORD = &HFFFF
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Structure FILETIME
Dim dwLowDateTime As Long
Dim dwHighDateTime As Long
End Structure
Private Structure WIN32_FIND_DATA
Dim dwFileAttributes As Long
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Long
Dim nFileSizeLow As Long
Dim dwReserved0 As Long
Dim dwReserved1 As Long
Dim cFileName As String 'line added by Vighneswar
'''Dim cFileName As String 'line commented by Vighneswar
'''dim cAlternate As String * 14 'line commented by Vighneswar
Dim cAlternate As String
End Structure
Private Declare Function FindFirstFile Lib "kernel32" Alias
"FindFirstFileA" (ByVal lpFileName As String, ByVal lpFindFileData As
WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias
"FindNextFileA" (ByVal hFindFile As Long, ByVal lpFindFileData As
WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As
Long) As Long
Public Sub FileSearchA(ByVal sPath As String, ByVal sFileMask As String,
ByRef taFiles As mctFileSearchResults, _
Optional ByVal bRecursive As Boolean = False, Optional ByVal iRecursionLevel
As Long = -1)
'''On Error GoTo Hell
Dim sFilename As String
Dim sFolder As String
Dim iFolderCount As Long
Dim aFolders() As String
Dim aFileMask() As String
Dim iSearchHandle As Long
Dim WFD As WIN32_FIND_DATA
Dim bContinue As Long : bContinue = True
Dim Ret As Long, X As Long
WFD.cFileName = Space(MAX_PATH) 'Lines added by Vighneswar
WFD.cAlternate = Space(14)
Try
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
' Search for subdirectories first and save'em for later
' --------------------------
If bRecursive Then
iSearchHandle = FindFirstFile(sPath & "*.*", WFD) '
Here I got the Error "Object Reference Not Set to an Instacnce of Object"
If iSearchHandle <> INVALID_HANDLE_VALUE Then
Do While bContinue
If (InStr(WFD.cFileName, Chr(0)) > 0) Then WFD.cFileName =
Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
sFolder = Trim(WFD.cFileName)
If (sFolder <> ".") And (sFolder <> "..") Then ' Ignore the
current and encompassing directories
If WFD.dwFileAttributes And vbDirectory Then
iFolderCount = iFolderCount + 1
ReDim Preserve aFolders(iFolderCount)
aFolders(iFolderCount) = sFolder
End If
End If
bContinue = FindNextFile(iSearchHandle, WFD) 'Get next
subdirectory.
Loop
bContinue = FindClose(iSearchHandle)
End If
End If
' --------------------------
bContinue = True
' Walk through this directory and sum file sizes.
' --------------------------
' FindFirstFile takes one type at a time, so we'll loop the search for as
many extensions as specified
aFileMask = Split(sFileMask, ";")
For X = 0 To UBound(aFileMask)
' Make sure it's all formatted
If Left$(aFileMask(X), 1) = "." Then aFileMask(X) = "*" & aFileMask(X)
If Left$(aFileMask(X), 2) <> "*." Then aFileMask(X) = "*." &
aFileMask(X)
iSearchHandle = FindFirstFile(sPath & aFileMask(X), WFD)
If iSearchHandle <> INVALID_HANDLE_VALUE Then
Do While bContinue
If (InStr(WFD.cFileName, Chr(0)) > 0) Then WFD.cFileName =
Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
sFilename = Trim$(WFD.cFileName)
' It's a file, right?
If (sFilename <> ".") And (sFilename <> "..") And
(Not (WFD.dwFileAttributes And vbDirectory) = vbDirectory) Then
With taFiles
.FileSize = .FileSize + (WFD.nFileSizeHigh *
MAXDWORD) + WFD.nFileSizeLow
.FileCount = .FileCount + 1
ReDim Preserve .Files(.FileCount)
.Files(.FileCount) = sPath & sFilename
End With
End If
bContinue = FindNextFile(iSearchHandle, WFD) ' Get next
file
Loop
bContinue = FindClose(iSearchHandle)
End If
Next
' --------------------------
' If there are sub-directories,
If iFolderCount > 0 Then
' And if we care,
If bRecursive Then
If iRecursionLevel <> 0 Then ' Recursively walk into them...
iRecursionLevel = iRecursionLevel - 1
For X = 1 To iFolderCount
FileSearchA(sPath & aFolders(X) & "\", sFileMask, taFiles,
bRecursive, iRecursionLevel)
Next
End If
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'''Hell:
End Sub
End Module
Regards ............ Vighneswar
I am developing a project in VB.NET in which I need to use Win32 API
function FindFirstFile()/FindNextFile()/FindClose()
The same code is wroking fine in VB 6.0 , but when I used the code in VB.NET
its giving an error saying that "Object reference is not set to an instance"
at the line where the FindFirstFIle() API is used. Can Anybody tell me why
its happing like this.
Thanks in Advance .
Here I am posting the code I wrote along with the error I got.
Module ProgramShortcuts
Public Structure mctFileSearchResults
Dim FileCount As Long
Dim FileSize As Decimal
Dim Files() As String
End Structure
Private Const MAX_PATH = 260
Private Const MAXDWORD = &HFFFF
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Structure FILETIME
Dim dwLowDateTime As Long
Dim dwHighDateTime As Long
End Structure
Private Structure WIN32_FIND_DATA
Dim dwFileAttributes As Long
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Long
Dim nFileSizeLow As Long
Dim dwReserved0 As Long
Dim dwReserved1 As Long
Dim cFileName As String 'line added by Vighneswar
'''Dim cFileName As String 'line commented by Vighneswar
'''dim cAlternate As String * 14 'line commented by Vighneswar
Dim cAlternate As String
End Structure
Private Declare Function FindFirstFile Lib "kernel32" Alias
"FindFirstFileA" (ByVal lpFileName As String, ByVal lpFindFileData As
WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias
"FindNextFileA" (ByVal hFindFile As Long, ByVal lpFindFileData As
WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As
Long) As Long
Public Sub FileSearchA(ByVal sPath As String, ByVal sFileMask As String,
ByRef taFiles As mctFileSearchResults, _
Optional ByVal bRecursive As Boolean = False, Optional ByVal iRecursionLevel
As Long = -1)
'''On Error GoTo Hell
Dim sFilename As String
Dim sFolder As String
Dim iFolderCount As Long
Dim aFolders() As String
Dim aFileMask() As String
Dim iSearchHandle As Long
Dim WFD As WIN32_FIND_DATA
Dim bContinue As Long : bContinue = True
Dim Ret As Long, X As Long
WFD.cFileName = Space(MAX_PATH) 'Lines added by Vighneswar
WFD.cAlternate = Space(14)
Try
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
' Search for subdirectories first and save'em for later
' --------------------------
If bRecursive Then
iSearchHandle = FindFirstFile(sPath & "*.*", WFD) '
Here I got the Error "Object Reference Not Set to an Instacnce of Object"
If iSearchHandle <> INVALID_HANDLE_VALUE Then
Do While bContinue
If (InStr(WFD.cFileName, Chr(0)) > 0) Then WFD.cFileName =
Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
sFolder = Trim(WFD.cFileName)
If (sFolder <> ".") And (sFolder <> "..") Then ' Ignore the
current and encompassing directories
If WFD.dwFileAttributes And vbDirectory Then
iFolderCount = iFolderCount + 1
ReDim Preserve aFolders(iFolderCount)
aFolders(iFolderCount) = sFolder
End If
End If
bContinue = FindNextFile(iSearchHandle, WFD) 'Get next
subdirectory.
Loop
bContinue = FindClose(iSearchHandle)
End If
End If
' --------------------------
bContinue = True
' Walk through this directory and sum file sizes.
' --------------------------
' FindFirstFile takes one type at a time, so we'll loop the search for as
many extensions as specified
aFileMask = Split(sFileMask, ";")
For X = 0 To UBound(aFileMask)
' Make sure it's all formatted
If Left$(aFileMask(X), 1) = "." Then aFileMask(X) = "*" & aFileMask(X)
If Left$(aFileMask(X), 2) <> "*." Then aFileMask(X) = "*." &
aFileMask(X)
iSearchHandle = FindFirstFile(sPath & aFileMask(X), WFD)
If iSearchHandle <> INVALID_HANDLE_VALUE Then
Do While bContinue
If (InStr(WFD.cFileName, Chr(0)) > 0) Then WFD.cFileName =
Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
sFilename = Trim$(WFD.cFileName)
' It's a file, right?
If (sFilename <> ".") And (sFilename <> "..") And
(Not (WFD.dwFileAttributes And vbDirectory) = vbDirectory) Then
With taFiles
.FileSize = .FileSize + (WFD.nFileSizeHigh *
MAXDWORD) + WFD.nFileSizeLow
.FileCount = .FileCount + 1
ReDim Preserve .Files(.FileCount)
.Files(.FileCount) = sPath & sFilename
End With
End If
bContinue = FindNextFile(iSearchHandle, WFD) ' Get next
file
Loop
bContinue = FindClose(iSearchHandle)
End If
Next
' --------------------------
' If there are sub-directories,
If iFolderCount > 0 Then
' And if we care,
If bRecursive Then
If iRecursionLevel <> 0 Then ' Recursively walk into them...
iRecursionLevel = iRecursionLevel - 1
For X = 1 To iFolderCount
FileSearchA(sPath & aFolders(X) & "\", sFileMask, taFiles,
bRecursive, iRecursionLevel)
Next
End If
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'''Hell:
End Sub
End Module
Regards ............ Vighneswar