64-bit version of Scan / Search directory and sub directories using Mapbasic and Windows API

308 views
Skip to first unread message

Nicholas G Lawrence

unread,
Aug 29, 2016, 1:58:25 AM8/29/16
to mapi...@googlegroups.com

Hello all,

 

I have successfully used code suggested by this list to scan a folder and return a string array of *.tab files.

 

However, I have just now attempted to use the same code in a script compiled in MapBasic v15.2 (64 bit) for me to run in 64-bit MapInfo. And the script fails. It does not generate an error, just returns a bad result (blanks). My MapInfo is running on 64-bit Windows 7

 

Considering that the code does include references to 32 bit for example, “WIN32_FIND_DATA” and “kernel32” Should the script be upgraded to 64 bit?

 

Cheers,

Nick Lawrence


***********************************************************************
WARNING: This email (including any attachments) may contain legally
privileged, confidential or private information and may be protected by
copyright. You may only use it if you are the person(s) it was
intended to be sent to and if you use it in an authorised way. No one
is allowed to use, review, alter, transmit, disclose, distribute, print
or copy this email without appropriate authority.

If this email was not intended for you and was sent to you by mistake,
please telephone or email me immediately, destroy any hardcopies of
this email and delete it and any copies of it from your computer
system. Any right which the sender may have under copyright law, and
any legal privilege and confidentiality attached to this email is not
waived or destroyed by that mistake.

It is your responsibility to ensure that this email does not contain
and is not affected by computer viruses, defects or interference by
third parties or replication problems (including incompatibility with
your computer system).

Opinions contained in this email do not necessarily reflect the
opinions of the Department of Transport and Main Roads,
or endorsed organisations utilising the same infrastructure.
***********************************************************************

Uffe Kousgaard

unread,
Aug 29, 2016, 2:20:47 AM8/29/16
to mapi...@googlegroups.com
Yes. A key thing may be using IntPtr rather than Integer for pointers returned from the winapi.
But hard to say without seeing the code.

Regards
Uffe Kousgaard

Peter Horsbøll Møller

unread,
Aug 29, 2016, 3:15:02 AM8/29/16
to mapi...@googlegroups.com

The InPtr is one change that will affect this. Another is the just introduced Align keyword.

Especially working with strings in a struct that is being passed to and from a DLL can be tricky.

 

I ended up implementing my own FindFiles functions using .NET instead of using the WinAPI.

Mostly because this would like me run my applications in older versions of MapInfo Pro than just 15.2.

 

These are the methods that I created:

Declare Method FILEFindFilesInFolder               '**Searches thru this specific folder only, no subfolders

                             Class "FILELib.MIController"  Lib "FILELib.dll"   Alias "FindFilesInFolder"

                             (                              ByVal sPath As String                                             'Path to search

                                                          , ByVal sMask As String                                           'Mask to use, eg. "*.*", "*.tab" etc

                             ) As Integer

Declare Method FILEFindFilesInFolders             '**Searches thru this specific folder and all subfolders

                             Class "FILELib.MIController"  Lib "FILELib.dll"   Alias "FindFilesInFolders"

                             (                              ByVal sPath As String                                             'Path to search

                                                          , ByVal sMask As String                                           'Mask to use, eg. "*.*", "*.tab" etc

                             ) As Integer

 

Declare Method FILEGetFindFilesFileName

                             Class "FILELib.MIController"  Lib "FILELib.dll"   Alias "GetFindFilesFileName"

                             (                              ByVal nFileItem As Integer                                   'File number to get the name full path of

                             ) As String                                                                                                                                                           'Returns full path for the file number, 1 is the first file in the list

Declare Method FILEGetFindFilesFileNames

                             Class "FILELib.MIController"  Lib "FILELib.dll"   Alias "GetFindFilesFileNames"

                             (                              arrFileNames() As String                                       'Array to hold all the filenames selected

                             ) As Integer

 

The first two file search thru either the folder or the folder and all subfolder.

The last two are used to getting the file names from the search result.

 

The methods are implemented in the FileLib.dll that can be found on github:

https://github.com/PeterHorsbollMoller/mbLibrary

 

It’s part of my mbLibrary but you can also just use these four declares and the dll alone.

 

Peter Horsbøll Møller

Pitney Bowes

--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to
mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.




Nicholas G Lawrence

unread,
Aug 29, 2016, 9:30:14 PM8/29/16
to mapi...@googlegroups.com

 

 

 

Declare Sub GetFileNames (byval sPath As String, byval sFilespec As String, sList() As String)

Define MAX_PATH 260

Type FILETIME

    dwLowDateTime As Integer

    dwHighDateTime As Integer

End Type

Type WIN32_FIND_DATA

    dwFileAttributes As Integer

    ftCreationTime As FILETIME

    ftLastAccessTime As FILETIME

    ftLastWriteTime As FILETIME

    nFileSizeHigh As Integer

    nFileSizeLow As Integer

    dwReserved0 As Integer

    dwReserved1 As Integer

    cFileName As String * MAX_PATH

    cAlternate As String * 14

End Type

Define INVALID_HANDLE_VALUE -1

Declare Function FindFirstFile Lib "kernel32"

    Alias "FindFirstFileA" (ByVal lpFileName As String, lpfindFileData As WIN32_FIND_DATA) As Integer

Declare Function FindNextFile Lib "kernel32"

    Alias "FindNextFileA" (ByVal hFindFile As Integer, lpFindFileData As WIN32_FIND_DATA) As Integer

Declare Function FindClose Lib "kernel32"

    Alias "FindClose" (ByVal hFindFile As Integer) As Integer

 

 

Sub GetFileNames (ByVal sPath As String, ByVal sFilespec As String, sList() As String)

Dim hFindFile, nStatus As Integer

Dim f As WIN32_FIND_DATA

Dim i As Integer

 

hFindFile = FindFirstFile (sPath + sFilespec, f)

If hFindFile <> INVALID_HANDLE_VALUE Then

    Do

       If (f.dwFileAttributes \ 16) Mod 2 = 0 Then

          i = i + 1

          ReDim sList(i)

          sList(i) = f.cFilename

       End If

       nStatus = FindNextFile (hFindFile, f)

    Loop While nStatus = 1

End If

nStatus = FindClose (hFindFile)

End Sub

 

 

And the command is;

Dim sTables() As String

Dim sDir As String

 

Call GetFileNames(sDir, "*.tab", sTables)

 

 

 

 

Cheers,

Nick Lawrence

 

 

 

 

 

 

 

From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of Uffe Kousgaard
Sent: Monday, 29 August 2016 4:21 PM
To: mapi...@googlegroups.com
Subject: Re: [MI-L] 64-bit version of Scan / Search directory and sub directories using Mapbasic and Windows API

 

Yes. A key thing may be using IntPtr rather than Integer for pointers returned from the winapi.

--

Nicholas G Lawrence

unread,
Aug 29, 2016, 10:33:27 PM8/29/16
to mapi...@googlegroups.com

Hi Peter,

 

I understand the flexibility of your FileLib.dll, but for me, I want a solution that is portable in that the functionality is completely within the mapbasic code.

 

Cheers,

Nick Lawrence

From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of Uffe Kousgaard


Sent: 29. august 2016 08:21

To: mapi...@googlegroups.com
Subject: Re: [MI-L] 64-bit version of Scan / Search directory and sub directories using Mapbasic and Windows API

 

Yes. A key thing may be using IntPtr rather than Integer for pointers returned from the winapi.

--

Peter Horsbøll Møller

unread,
Aug 30, 2016, 2:06:13 AM8/30/16
to mapi...@googlegroups.com

Hi Nick

 

You need to modify your structs and declares to something along these lines:

 

'64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments

Type FILETIME

        dwLowDateTime As Integer

        dwHighDateTime As Integer

End Type

Type WIN32_FIND_DATA Align4          'Used on 64 bit systems, Align[n] is a new keyword with MapBasic 15.2.2

            dwFileAttributes         As Integer

            ftCreationTime           As FILETIME

            ftLastAccessTime         As FILETIME

            ftLastWriteTime          As FILETIME

            nFileSizeHigh            As Integer

            nFileSizeLow             As Integer

            dwReserved0              As Integer

            dwReserved1              As Integer

            cFileName                As String * MAX_PATH

            cAlternate               As String * 14

End Type

Declare Function WAPIFindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA"

                         ( ByVal lpFileName As String

                         , lpFindFileData As WIN32_FIND_DATA

                         ) As IntPtr

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

Declare Function WAPIFindNextFile Lib "kernel32.dll" Alias "FindNextFileA"

                         ( ByVal hFindFile As IntPtr

                         , lpFindFileData As WIN32_FIND_DATA

                         ) As Integer

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx

Declare Function WAPIFindClose Lib "kernel32.dll" Alias "FindClose"

                         ( ByVal hFindFile As IntPtr

                         ) As Integer

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364413(v=vs.85).aspx

 

Notice the IntPtr variable types and the Align4 keyword

 

I have attached my latest WINAPI.def that should work with MapInfo Pro 15.2.

I say should because I haven’t put it thru a complete test.

 

Peter Horsbøll Møller

Pitney Bowes

 

WINAPI 1522.def

Nicholas G Lawrence

unread,
Aug 31, 2016, 12:05:55 AM8/31/16
to mapi...@googlegroups.com

Hi Peter,

 

Ta for that

 

I shall now install the mapbasic v15.2.2 patch J

 

Cheers,

Nick Lawrence

 

From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of Peter Horsbøll Møller


Sent: Tuesday, 30 August 2016 4:06 PM
To: mapi...@googlegroups.com

Subject: RE: [MI-L] 64-bit version of Scan / Search directory and sub directories using Mapbasic and Windows API

 

Hi Nick

 

You need to modify your structs and declares to something along these lines:

 

'64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments

Type FILETIME

        dwLowDateTime As Integer

        dwHighDateTime As Integer

End Type

Type WIN32_FIND_DATA Align4          'Used on 64 bit systems, Align[n] is a new keyword with MapBasic 15.2.2

            dwFileAttributes         As Integer

            ftCreationTime           As FILETIME

            ftLastAccessTime         As FILETIME

            ftLastWriteTime          As FILETIME

            nFileSizeHigh            As Integer

            nFileSizeLow             As Integer

            dwReserved0              As Integer

            dwReserved1              As Integer

            cFileName                As String * MAX_PATH

            cAlternate               As String * 14

End Type

Declare Function WAPIFindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA"

                         ( ByVal lpFileName As String

                         , lpFindFileData As WIN32_FIND_DATA

                         ) As IntPtr

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

Declare Function WAPIFindNextFile Lib "kernel32.dll" Alias "FindNextFileA"

                         ( ByVal hFindFile As IntPtr

                         , lpFindFileData As WIN32_FIND_DATA

                         ) As Integer

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx

Declare Function WAPIFindClose Lib "kernel32.dll" Alias "FindClose"

                         ( ByVal hFindFile As IntPtr

                         ) As Integer

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa364413(v=vs.85).aspx

 

Notice the IntPtr variable types and the Align4 keyword

 

I have attached my latest WINAPI.def that should work with MapInfo Pro 15.2.

I say should because I haven’t put it thru a complete test.

 

Peter Horsbøll Møller

Pitney Bowes

 

From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of Nicholas G Lawrence


Sent: 30. august 2016 00:57
To: mapi...@googlegroups.com

Bipin Bisht

unread,
Jan 3, 2017, 10:07:51 AM1/3/17
to MapInfo-L
Hi All

I am also facing same problem in MapBasic 16 and MapInfo 16. I am not getting filename while handling FindFirstFile(name, fileinfo) except blank.
Please suggest me where I am wrong.

Thankyou
Bipin Bisht

Peter Horsbøll Møller

unread,
Jan 3, 2017, 10:12:06 AM1/3/17
to mapi...@googlegroups.com

Did you read thru the entire thread: https://groups.google.com/forum/#!topic/mapinfo-l/hjUb8_k6830

There are some suggestions for overcoming this.

 

Nick, were you able to make it work in 64 bit/MapBasic 15.2.2?

 

Peter Horsbøll Møller

Pitney Bowes

 

--

--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to


To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sancarn

unread,
Jan 4, 2017, 9:21:55 AM1/4/17
to MapInfo-L
If you wanted to use the DLL, you could always install the file.
Reply all
Reply to author
Forward
0 new messages