473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Win32API - FindFirstFile() in VB.NET

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
3 10545
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Smith | last post by:
hi all, again.... having another problem with python COMs, I run a python script on a win2000 machine, that uses win32com.client and it runs perfectly, whereas when I come to run it on a win98...
5
by: Gary Richardson | last post by:
I'm trying to use win32api.SetCursorPos() to position the cursor in a Tkinter canvas window. I.e.: from Tkinter import * import win32api root = Tk() canvas = Canvas(root, width=400,...
2
by: Rich Strang | last post by:
Hi group I have just got the FindFirstFile function working in my small tutorial app, the problem is that I can only seem to get it working when I hard code the search string. I have defined a...
2
by: tom | last post by:
Hello, I want to know if FindFirstFile() can do more as I expect. FindFirstFile() can take the arguement of "*" or "?", for example, FindFirstFile("result.*.txt", &FileData); I want to...
1
by: Paulo Eduardo | last post by:
Hi, all! We are devleping one app that will use disk access with FindFirstFile and FindNextFile for it. Can someone expose me how works FindFirstFile and FindNextFile in disk access? Does the...
2
by: LewS | last post by:
My application uses FindFirstFile for searching for specific file types entered by a user. Entries like c:\myfiles\*.txt and \\myserver\shareddrive\myfiles\*.txt work okay, but when I use a UNC...
9
by: Comcast Newsgroups | last post by:
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...
1
by: gudiya | last post by:
hello everybody I am having a very wierd problem.i am developing a win ce applicationin which I am marshalling win ap function findfirstfile and findnextfile in c# .My problem is it reads the file...
3
by: gudiya | last post by:
hi, My code works but i am missing the first 2 characters of file name. Any idea why???? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.