By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Win32API - FindFirstFile() in VB.NET

P: n/a
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




May 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"vighnesh" <vi******@nannacomputers.com> schrieb:
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.


Your declarations are wrong. 'Long' is a 64-bit type in VB.NET, and thus
'Long's from VB6 need to be changed to 'Int32' or 'Integer'. I am curious
why you are not using 'System.IO.Directory' and its shared methods for this
purpose instead.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

May 6 '06 #2

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"vighnesh" <vi******@nannacomputers.com> schrieb:
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.


Your declarations are wrong. 'Long' is a 64-bit type in VB.NET, and thus
'Long's from VB6 need to be changed to 'Int32' or 'Integer'. I am curious
why you are not using 'System.IO.Directory' and its shared methods for
this purpose instead.

--
M S Herfried K. Wagner

I'm not the OP, but I am happy for this answer. System.IO.Directory is DOG
SLOW.
May 6 '06 #3

P: n/a
Thank You Verymuch

Regards
Vighneswar

"Tom Scales" <tj******@gmail.com> wrote in message
news:oW****************@tornado.tampabay.rr.com...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"vighnesh" <vi******@nannacomputers.com> schrieb:
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.


Your declarations are wrong. 'Long' is a 64-bit type in VB.NET, and thus
'Long's from VB6 need to be changed to 'Int32' or 'Integer'. I am
curious why you are not using 'System.IO.Directory' and its shared
methods for this purpose instead.

--
M S Herfried K. Wagner

I'm not the OP, but I am happy for this answer. System.IO.Directory is
DOG SLOW.

May 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.