|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Win32API - FindFirstFile() in VB.NETI 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 "vighnesh" <vighn***@nannacomputers.com> schrieb: Your declarations are wrong. 'Long' is a 64-bit type in VB.NET, and thus > 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. '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/>
Show quote
Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message I'm not the OP, but I am happy for this answer. System.IO.Directory is DOG news:%23GhIDzPcGHA.3344@TK2MSFTNGP03.phx.gbl... > "vighnesh" <vighn***@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 SLOW. Thank You Verymuch
Regards Vighneswar Show quoteHide quote "Tom Scales" <tjsca***@gmail.com> wrote in message news:oW%6g.3553$ba.3093@tornado.tampabay.rr.com... > > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message > news:%23GhIDzPcGHA.3344@TK2MSFTNGP03.phx.gbl... >> "vighnesh" <vighn***@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. >
How getting eventhandler attached to an event?
Simple word count single to string problem Error setting CSV to display text My app as system process Simple TCP communications examples Thread terminating instantly How to move the mouse around in vb.net Move emails (.msg files) between an Outlook inbox and a file direc How to simulate mouse click in vb.net |
|||||||||||||||||||||||