Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Nov 21, 2019 4:10 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: problem with unicode to list folders using API calls
PostPosted: Thu Sep 08, 2011 9:40 am 
Offline

Joined: Thu Nov 08, 2007 4:59 am
Posts: 392
Location: Germany
Hello,

I have the following code that scans for folders and subfolders.
It works as long as I use the ANSI version of the API calls :
FindFirstFileA
FindNextFileA
memoryblock = New MemoryBlock(318) //ANSI
memoryblock.CString(44)

But if I change the following elements to UNICODE the code doesnt work anymore:
FindFirstFileW
FindNextFileW
memoryblock = New MemoryBlock(592) //UNICODE
memoryblock.WString(44)


It seems that the problem is in FindFirstFileW - it always returns -1
But why ?

How could I change the following code to work with unicode?
You can uncomment the UNICODE lines to test it.
I use Win XP

Example:
add a listbox1 to the window, then run:
dim f as FolderItem
f = GetFolderItem ("d:\testdir")

If f <> Nil Then
ListBox1.DeleteAllRows
list_dirs (f.AbsolutePath)
end



Sub list_dirs(sAbsolutePath as string)

'Declare Function FindFirstFileW Lib "Kernel32" ( name As CString, data As Ptr ) As Integer //UNICODE
'Declare Function FindNextFileW Lib "Kernel32" ( handle As Integer, data As Ptr ) As Boolean
Declare Function FindFirstFileA Lib "Kernel32" ( name As CString, data As Ptr ) As Integer //ANSI
Declare Function FindNextFileA Lib "Kernel32" ( handle As Integer, data As Ptr ) As Boolean
Declare Sub FindClose Lib "Kernel32" ( handle As Integer )
Declare Function GetLastError Lib "kernel32" () As Integer

Dim sfolder As String
Dim handle As Integer
dim iLastErrro as Integer
Dim mb As MemoryBlock

mb = New MemoryBlock(318) //ANSI
'mb = New MemoryBlock(592) //UNICODE

sfolder = sAbsolutePath + "*.*"


'handle = FindFirstFileW(sfolder, mb) //UNICODE (always -1)
handle = FindFirstFileA(sfolder, mb) //ANSI


Do

If (Bitwise.BitAnd(mb.Long(0), 16) = 16) Then //we have a folder

'**UNICODE
'if mb.WString(44) <> "." and mb.WString(44) <> ".." then
'listbox1.AddRow sAbsolutePath + mb.WString(44) +"\"
'listbox1.Refresh
'
''**recursive call
'list_dirs (sAbsolutePath + mb.WString(44) + "\") //UNICODE
'end


'**ANSI
if mb.CString(44) <> "." and mb.CString(44) <> ".." then
listbox1.AddRow sAbsolutePath + mb.CString(44) +"\"
listbox1.Refresh

'**recursive call
list_dirs sAbsolutePath + mb.CString(44) + "\" //ANSI
end

End

'Loop Until Not FindNextFileW( handle, mb ) //UNICODE
Loop Until Not FindNextFileA( handle, mb ) //ANSI

'**
Call FindClose(handle)

End Sub


Top
 Profile  
Reply with quote  
 Post subject: Re: problem with unicode to list folders using API calls
PostPosted: Thu Sep 08, 2011 9:49 am 
Offline
Real Software Engineer

Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7858
Location: Canada, Alberta, Near Red Deer
Have you tried checking if the function is available ?
It may not be.
http://docs.realsoftware.com/index.php/ ... nAvailable

_________________
Norman Palardy (Real Software)


Top
 Profile  
Reply with quote  
 Post subject: Re: problem with unicode to list folders using API calls
PostPosted: Thu Sep 08, 2011 10:51 am 
Offline

Joined: Thu Nov 08, 2007 4:59 am
Posts: 392
Location: Germany
yes, I did :
System.IsFunctionAvailable("FindFirstFileW", "kernel32.dll") is true


Top
 Profile  
Reply with quote  
 Post subject: Re: problem with unicode to list folders using API calls
PostPosted: Thu Sep 08, 2011 1:06 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Use a WString instead of CString in your declare:
'Declare Function FindFirstFileW Lib "Kernel32" ( name As WString, data As Ptr ) As Integer   //UNICODE

Otherwise, the name you pass will be mangled (narrow characters instead of wide characters).


Top
 Profile  
Reply with quote  
 Post subject: Re: problem with unicode to list folders using API calls
PostPosted: Thu Sep 08, 2011 1:32 pm 
Offline

Joined: Thu Nov 08, 2007 4:59 am
Posts: 392
Location: Germany
ah..yes, works! Thanks


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group