-
Rename , Open, read Unicode files
Can anyone tell me please, how to Manipulate Unidcode files ?
For example, how to Rename an Arabic or chinese files ? in my debug mode, the name of such files appears like "C:?????????.extension" so how can i use the functions like Name, Open, Copy, Read, kill, get, put etc....on such files ?
I am asking because someone reported me a bug in my application that he is facing on arabic files, wich is normal, so i am wondering if it's easy to make my application support Arabic for example ?
-
Re: Rename , Open, read Unicode files
Unfortunatenaly VB's own filehandling methods are ANSI only. You need to either use Windows API for file handling or features from additional libraries for full Unicode support. Also, if you need to display something in your forms in Unicode then a lot needs to be done, you can't use the native VB6 controls as they are ANSI only. All the original OCX controls are ANSI too.
The only good news is that internally VB6 strings are Unicode (16-bit per character). As long as you can read stuff without any automatical ANSI <-> Unicode conversions into the strings then you can process Unicode in your code.
Searching for CreateFileW should get you started. It is used to open file handles, ie. you can both create and open files with it. Note that when passing strings to API calls you must use ByVal StrPtr() to avoid ANSI conversions.
-
Re: Rename , Open, read Unicode files
For a Windows system a "Unicode file" would be a file containing Unicode text. A file can also be named using Unicode naming, which is an entirely different thing.
What you have there probably an ANSI filename created on a system with different locale settings from yours. Actual Unicode filenames in Windows have a \\?\ prefix most places where they are used:
\\?\C:\outréfichier.txt
The problem you face in your program is that for compatibility with DOS and Win9x VB6 only used Unicode for internal operations. For anything dealing with its environment (like a file name for an Open statement) it translates the string to ANSI via the current locale/codepage settings before acting.
This is a lot like the overhead involved in calling the -A suffix API entrypoints instead of the -W versions.
The native file I/O statements in VB6 also translate data to/from ANSI on text operations for the same reason.
One place to start is the VB6 documentation. Here is one section covering some of the issues involved: International Issues.
-
Re: Rename , Open, read Unicode files
The FSO is another option for some things, since it can handle Windows "Unicode" (UTF-16LE) as easily as ANSI.
-
Re: Rename , Open, read Unicode files
Thanks Merri for your post.
What i understood from you is that if i want change a name of an arabic or chinese file (without showing it's name on my interface), it's not hard to be made by API ? If yes, can you give me an example of an api use to rename a Unicode file and how to read it's content ?
For user interface, you are telling that native one support only ANSI, so to get controls that support UNICODE, there is no free ocx or active x or something we can download ?
-
Re: Rename , Open, read Unicode files
As I explained above, those are almost certainly not Unicode file names. They are more likely to be ANSI filenames encoded using an Arabic locale setting. Thus the approach Merri suggested probably won't help, nor will the FSO.
There are multiple Arabic LCIDs as listed at http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
dilettante
The FSO is another option for some things, since it can handle Windows "Unicode" (UTF-16LE) as easily as ANSI.
I don't mind using fso, but how can i use it to rename a file that appears like ???????? on my system ?
I will try to give you an exampl so that you can understand me more :
My OS is in french
someone sent me 3-4 arabic files.
I add them to a listbox , all of them appears like ?????????????.extension
now suppose i want to loop through these files in the listbox, check if they exists on my system
suppose i have 3-4 files that i want to check if they exists ( for ANSI files , i can do something like :
Code:
fso.FileExists(list1.list(i) )
and than rename them using even VB6 function
how can i do this using UNICODE FILES ?
Code:
fso.FileExists(list1.list(i) )
That's First problem, rename the file !
Second problem is to extract data from it !!!
Thanks again to all of you guys
-
Re: Rename , Open, read Unicode files
Free controls are limited, I've made a few myself but as I haven't had the resources (= time) the controls have issues here and there, even if for the most part they do work. But sometimes they crash when they shouldn't (especially under IDE). You can also find some additional Unicode controls via Planet Source Code.
I'm dropping out of the filename discussion from now, because I don't have extensive knowledge on that area. I know one has Unicode filenames in NT based Windowses, but how these are handled would require me to do research to be sure. I can say that part of your problem is that you're storing the filenames in a listbox: when you put any string data to a control, it gets the lossy ANSI treatment. Same when you are reading the filenames using VB native methods. Both of these parts of the code need to be fixed.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
justgreat
Can anyone tell me please, how to Manipulate Unidcode files ?
For example, how to Rename an Arabic or chinese files?
<EVERYTHING IS POSSIBLE!!!>
I give you an example how to rename files in Unicode version:
Code:
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Type SHFILEOPSTRUCTW
hwnd As Long
wFunc As Long
pFrom As Long 'String
pTo As Long 'String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As Long 'String
End Type
Public Enum FOACTION
FO_MOVE = &H1
FO_COPY = &H2
FO_DELETE = &H3
FO_RENAME = &H4
End Enum
Public Enum FOFACTION
FOF_ALLOWUNDO = &H40
FOF_CONFIRMMOUSE = &H2
FOF_FILESONLY = &H80
FOF_MULTIDESTFILES = &H1
FOF_NO_CONNECTED_ELEMENTS = &H2000
FOF_NOCONFIRMATION = &H10
FOF_NOCONFIRMMKDIR = &H200
FOF_NOCOPYSECURITYATTRIBS = &H800
FOF_NOERRORUI = &H400
FOF_NORECURSION = &H1000
FOF_RENAMEONCOLLISION = &H8
FOF_SILENT = &H4
FOF_SIMPLEPROGRESS = &H100
FOF_WANTMAPPINGHANDLE = &H20
FOF_WANTNUKEWARNING = &H4000
End Enum
Private Declare Function SHFileOperationA Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function SHFileOperationW Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCTW) As Long
'bUseUnicode = m_bISNT Check the OS is NT or Win9x
Public Function RenameFile(ByVal sSourceFile As String, ByVal sDesFile As String,bUseUnicode As Boolean) As Boolean
Dim sfoW As SHFILEOPSTRUCTW
Dim sfo As SHFILEOPSTRUCT
Const Success As Long = 0
If bUseUnicode Then
With sfoW
.hwnd = 0
.wFunc = FO_RENAME
.pFrom = StrPtr(sSourceFile & Chr(0) & Chr(0))
.pTo = StrPtr(sDesFile & Chr(0))
.fFlags = FOF_ALLOWUNDO Or FOF_WANTNUKEWARNING Or FOF_RENAMEONCOLLISION 'FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI
End With
If SHFileOperationW(sfoW) = Success Then
RenameFile = True
Exit Function
End If
Else
With sfo
.hwnd = 0
.wFunc = FO_RENAME
.pFrom = sSourceFile
.pTo = sDesFile
.fFlags = FOF_ALLOWUNDO Or FOF_WANTNUKEWARNING Or FOF_RENAMEONCOLLISION 'FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI
End With
If SHFileOperationA(sfo) = Success Then
RenameFile = True
Exit Function
End If
End If
End Function
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
Merri
Free controls are limited, I've made a few myself but as I haven't had the resources (= time) the controls have issues here and there, even if for the most part they do work. But sometimes they crash when they shouldn't (especially under IDE). You can also find some additional Unicode controls via Planet Source Code.
Time is young lady's Breast ditch,push them to make it (look bigger).
时间象女人的乳沟,挤挤就有了。
-
Re: Rename , Open, read Unicode files
Thanks Merri and Jonney,
Reading Merri i felt it impossible lol
Reading Jonney I get a little hope now to make it works
Thanks to all for trying to help
Jonney, I tried your code, it didn't work, but i think it's not due to your code, but maybe it's related to how i am getting the file name ! I mean that, when i make the debug, the file name passed to your rename function as source file is appearing as ????????????????????????.
I used THE API TO GET a file and not the common dialog box, like mentionned here http://support.microsoft.com/?scid=k...61286&x=18&y=2
Is there any flag to put to get a file name in UNICODE with this api?
However i tried also to see if i can display arabic text on my form, so that users that use this langage could give an arabic name to their file name (supposing your RENAME WORKS), but couldn't make it work neither
Here is what i did :
-I have a french Windows
-I Went to the regional settings of windows (control panel...) and checked the option that install the langages written from right to left from windows CD...
-I added the arabic langage to my windows langages/keyboards so that now i have FR, EN and AR appearing next to the clock in the windows langage bar (next to the clock)
-I downloaded/INSTALLED from the net some free arabic fonts.
-I went to the VB6 IDE and i changed the font of label to one of these fonts ARABIC (traditional arabic for expl)
-I Typed some texts in the label, and it appears like Ô À Á Ç È Ê in the Caption property of the label, but if you look at the form, it appears like correct ARABIC Letters inside the label (good news i can display arabic labels)
-In my windows explorer or desktop now, Arabic files appears with arabic letters and not ???????, but if i make copy/paste to this file name into a textbox (even after giving the textbox an arabic font), the name inside the textbox appears like : Ô À Á Ç È Ê and not with arabic letters as i see in Windows explorer!
I did this, after failing using your Rename Function, so that i tried to copy/paste the file name to textbox and call your function Rename based on this textbox , but also i couldn't make it work.
Thanks a lot for your support, if you got more ideas/codes/suggestions, i will be thankfull.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
justgreat
...the file name passed to your rename function as source file is appearing as ????????????????????????.
I used THE API TO GET a file and not the common dialog box, like mentionned
here http://support.microsoft.com/?scid=k...61286&x=18&y=2
Is there any flag to put to get a file name in UNICODE with this api?
That MSDN link is not a unicode example. It does not use unicode APIs. Take a quick look at the unicode compatible open/save dialog class in my signature below.
-
Re: Rename , Open, read Unicode files
Thanks a lot LaVole for your replies.
I will try to find API, to read and write into a file that support UNICODE, as i understand from your replies is that such API are called W - VERSION?
to conclude, what i need now, is to check your class to get the path of Unicode files
-Try the code of Jonney to be able to rename unicode files
- Looking for API CreateFile API and such API to be able to read and write into the file
- To get file lengh GetFileSize, Get/SetFileAttributes etc...If the file is UNICODE using FSO won't work ?
-
2 Attachment(s)
Re: Rename , Open, read Unicode files
Quote:
First of ALL,Unicode in VB6 is complicated for new babies.
Since VB Controls and IDE is ANSI,thus Textbox shows Question Mark ???.
You go further to modify your locale to ARABIC on Control Panel. Start-->Control Panel -->Region and Language Options-->Advance Tab -->Then select language for non-unicode setting,suggest temporarily set to Arabic.
For your case,it isn't a permanent solution. You can use Lavolpe's Unicode Open/Save Dialog to choose that Arabic file,then use my above codes to change the file name via CODE,then you use unicode Messagebox or Print Text on Picturebox via DrawTextW API or an Unicode-aware control for displaying the result.
I have spend my time to make a demo for you.
Quote:
Actually,SHFileOperation(A)/W can do everything: Cut/Copy/Move/Rename file in UNICODE Version,you go ahead to modify
Learn Unicode,please go to http://www.cyberactivex.com/UnicodeTutorialVb.htm , it is a master page.
Cheers!
-
Re: Rename , Open, read Unicode files
I don't use FSO. I was under the impression that it does work for unicode files and if that is true, then honestly, it should be easier to use than the API way unless one is already really familiar with the API way.
FYI: W-version APIs look like this: CreateFileW where ANSI versions look like CreateFileA.
If you look at that MSDN link again, you'll see that it uses OpenFileNameA which is how I quickly determined it was not a unicode example.
W-version APIs generally need their parameters tweaked a bit when it comes to strings, changing them to ByVal Long vs. ByVal String. Rule of thumb: you cannot pass a string ByVal to a W-version API. StrPtr() should be used. This is because passing a string ByVal to an API, VB will convert it to ANSI and the W-versions doesn't want ANSI. Passing StrPtr() instead, passes the memory address of the unicode string. VB strings are stored as unicode behind the scenes.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
Jonney
Since VB Controls and IDE is ANSI,thus Textbox shows Question Mark ???.
You go further to modify your locale to ARABIC on Control Panel. Start-->Control Panel -->Region and Language Options-->Advance Tab -->Then select language for non-unicode setting,suggest temporarily set to Arabic.
For your case,it isn't a permanent solution. You can use Lavolpe's Unicode Open/Save Dialog to choose that Arabic file,then use my above codes to change the file name via CODE,then you use unicode Messagebox or Print Text on Picturebox via DrawTextW API or an Unicode-aware control for displaying the result.
I have spend my time to make a demo for you.
Learn Unicode,please go to
http://www.cyberactivex.com/UnicodeTutorialVb.htm , it is a master page.
Cheers!
Thanks a lot, i really appreciate a lot this demo, i am reading it and trying to understand it, will let you know if all is fine...
The only thing remaining is how can i get what the user typed in Textbox, and write it, into a file ! or loading it from file to fill the textbox
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
LaVolpe
I don't use FSO. I was under the impression that it does work for unicode files and if that is true, then honestly, it should be easier to use than the API way unless one is already really familiar with the API way.
FYI: W-version APIs look like this: CreateFileW where ANSI versions look like CreateFileA.
If you look at that MSDN link again, you'll see that it uses OpenFileNameA which is how I quickly determined it was not a unicode example.
W-version APIs generally need their parameters tweaked a bit when it comes to strings, changing them to ByVal Long vs. ByVal String. Rule of thumb: you cannot pass a string ByVal to a W-version API. StrPtr() should be used. This is because passing a string ByVal to an API, VB will convert it to ANSI and the W-versions doesn't want ANSI. Passing StrPtr() instead, passes the memory address of the unicode string. VB strings are stored as unicode behind the scenes.
If i change in the code of microsoft the OpenFileNameA to OpenFileNameW (dunno if it exists), and i change the parameters for Strings, using the StrPtr instead of the string itself, it could work ?
What is the advantage of your code compared to microsoft's one ?
-
Re: Rename , Open, read Unicode files
What I think is needed here is a copy of Kaplan's Internationalization with Visual Basic.
Sample Chapter 6 VB-Is It ANSI or Unicode? should go a long way toward explaining what is going on here.
So far we've seen nothing to suggest that Unicode is in play here, and plenty to demonstrate that the file name issue at least is a locale problem relating to ANSI character encoding.
While the data within the files might well be Unicode, and the FSO can help with that, I'm not so sure that's even true. More likely the data in the files is localized ANSI as well.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
justgreat
If i change in the code of microsoft the OpenFileNameA to OpenFileNameW (dunno if it exists), and i change the parameters for Strings, using the StrPtr instead of the string itself, it could work ?
What is the advantage of your code compared to microsoft's one ?
Not necessarily. It is more complicated than that in this case. Why? Because the API expects a UDT also, correct? If you look at that UDT, it has a lot of string members. If you pass the UDT ByRef, VB will convert those strings to ANSI for the API call and no joy. If you noticed the calls I made in that class, I am using VarPtr to pass the UDT. This has a similar effect of StrPtr for strings. By using VarPtr & changing the API declaration approrpiately, I'm passing a Long ByVal vs. a UDT ByRef and VB won't convert unicode > ansi. Take a look at the ShowSave & ShowOpen routines in that class and also the declaration for the W & A version of the API.
Your second question. What is the advantage? Simply one class that can be used on ANSI or Unicode systems, that supports both ANSI & unicode file and path names. Where the MSDN example can be used on both systems also, but not with unicode file and/or path names. There is not much difference, if any, of using that MSDN example and VB's common dialog; both do not support unicode.
-
Re: Rename , Open, read Unicode files
Thanks for the explication and i see the advantage of your class, but what i wanted maybe to say, is in my case and to avoid changing the whole code, can't i simply change the declaration and the way i pass string and add to it StrPtr , just to avoid changing lots of things in my existant code ?
In the example provided by Jonney and based on your class, we can get the path name, but if i do something like :
Code:
lstMyList.additem cDlg.Filename
, the file name appears again inside the listbox with ????? even if i choosed arabic font for the listbox !
Second thing i noticed, I made a textbox with arabic font, i typed things in it,to simulate a textbox in wich the user choose the destination File's name, in the textbox, arabic letters appeared, then in the code if i do something like :
Code:
msSaveAsFileName = text1.text
, the saved file name will be ÖÕËÞÝ and not with arabic letters ! So i can't let the user select his filename because he won't be happy to get ÖÕËÞÝ instead of the arabic letters he is seeing in the textbox
i am talking about this part of the code :
Code:
If RenameFile(msOriginalFileName, msSaveAsFileName, True) = True Then
ShellMsgBox "Original File is " & msOriginalFileName & vbCrLf & "Rename As " & msSaveAsFileName, "Rename File - Unicode Version", vbApplicationModal
-
Re: Rename , Open, read Unicode files
You are not going to be able to add unicode strings to non-unicode control properties. Doing so ends up converting them to ANSI. This is completely understandable. For example, adding a string to a listbox is done with API calls and a non-unicode window will be getting their ANSI messages from those API calls. With the listbox and adding list items, SendMessage(hWnd, LB_ADDSTRING, n, theString) is used & sends the listbox an ANSI string and if you tried to send it a unicode string, it will be expecting an ANSI string and give you unexpected results (??????). To display unicode strings and maintain their values, you will want to use unicode-capable controls. And for VB, this really means 3rd party controls.
Bottom line is that if you want to play with unicode, generally your controls should be unicode compatible too and have ways of accepting unicode strings, and they do.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
LaVolpe
You are not going to be able to add unicode strings to non-unicode control properties. Doing so ends up converting them to ANSI. This is completely understandable. For example, adding a string to a listbox is done with API calls and a non-unicode window will be getting their ANSI messages from those API calls. With the listbox and adding list items, SendMessage(hWnd, LB_ADDSTRING, n, theString) is used & sends the listbox an ANSI string and if you tried to send it a unicode string, it will be expecting an ANSI string and give you unexpected results (??????). To display unicode strings and maintain their values, you will want to use unicode-capable controls. And for VB, this really means 3rd party controls.
Bottom line is that if you want to play with unicode, generally your controls should be unicode compatible too and have ways of accepting unicode strings, and they do.
So your code and the one's of Jonney, will work in case i don't want to use controls (listbox, textbox etc...) to interact with user !
In this case, i must use special controls ?
Thus, i think that i won't make it anymore because i think that third party controls are expensive and it will it will take me lots of time to change all the code and controls that i used, except if someone knows free controls for that
Thanks again to all of you for your support
-
Re: Rename , Open, read Unicode files
Which uniCode aware controls do you need?
Button, label? checkbox? etc...
-
Re: Rename , Open, read Unicode files
The controls i am using are mainly:
Textbox (to allow user to type a file name) and Listbox (select and display files)
For the moment, i didn't translate the interface, so textbox and listbox, will be enough to a user to type the name of the destination file he wants and to select his file and see the result...So listboxes and textboxes may solve my problem.
In the futur, I will need more controls, if i want to translate my application's interface, into UNICODE LANGAGES, because i do use labels, options and checkboxes and command button and frames.
-
Re: Rename , Open, read Unicode files
Buy commercial Unicode-aware ActiveX controls either from http://www.cyberactivex.com/ or create / Modify from www.vbaccelerator.com or contract out to Lavolpe.
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
Jonney
Thanks for the suggestion, but I don't think that i will buy controls, for a free application !
-
Re: Rename , Open, read Unicode files
by the way Jonney what did you mean by "or contract out to Lavolpe." ?
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
justgreat
by the way Jonney what did you mean by "or contract out to Lavolpe." ?
Lavolpe is one of VB guru who are writing (have written) sets of excellent unicode controls and graphic foundational class. Please check his public profile : Occupation - Contractor
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
Jonney
Lavolpe is one of VB guru who are writing (have written) sets of excellent unicode controls and graphic foundational class. Please check his public profile : Occupation - Contractor
I am not understanding what do you mean by he is contractor, anyway i will wait his comment maybe he has some explications :)
Each time i want to work on something i find that Lavolpe already made it lol :)
-
Re: Rename , Open, read Unicode files
I haven't created any unicode controls that I am willing to distribute. However, I think Merri still has his in the Code Bank section, you may want to search there. Also, as suggested, look at PlanetSourceCode.com for others. There are controls for sale as mentioned, but many free ones too; just a matter of searching -- but "you get what you pay for"
-
Re: Rename , Open, read Unicode files
As you said LaVolpe , "you get what you pay for", it makes no sens to add to my application free controls that am not sure that they won't cause bugs ... And am not ready to buy controls for a tool that i will make it free ...
Thus, my application won't support UNICODE files, i don't know if VB.Net 's controls support UNICODE, but i faced many issues while making this application, that i noticed them easiar to be done in .net, so i think that after this application i will move to .net, even if vb6 for me is the langage i like most
Thanks again to all of you for trying to help
-
Re: Rename , Open, read Unicode files
justgreat:
Quote:
Can anyone tell me please, how to Manipulate Unidcode files ?
For example, how to Rename an Arabic or chinese files ? in my debug mode, the name of such files appears like "C:?????????.extension" so how can i use the functions like Name, Open, Copy, Read, kill, get, put etc....on such files ?
Yours appears to be a rather big subject. First of all, I must emphasize that I am far from an expert in Unicode. I became involved in Unicode because very occasionally a Chinese friend would send me a DVD containing photos under various folder names. To view those photos I cannot use my own paint program, nor a third party program such as PhotoShop (v 7.0.0), as folder names are in Chinese, which hinders the access of the files. So I had to make a simple program in order to Open the files and/or to browser them one by one through Next/Previous buttons. Since some photos were vertically taken, I had to provide "turn left" and "turn right" buttons as well. To gain an acceptable speed for the said rotation operations, I used DIB to contain the source image, before projecting it to a full-screen-sized PictureBox (keeping aspect ratio). It is through this kind of exercise that I've acquired some Unicode knowledge.
Having browsed the earlier "replies" in your thread, I wonder whether my actual experience would throw some light on your current situation. Hence I summarize the following points with the hope of providing you with some kind of reference.
(1) To achieve what I wanted, I had to (a) do a unicode folder BAS, (b) do a unicode textbox CTL, (c) do a unicode listbox CTL, (d) do a GetOpenFileNameW/GetSaveFileNameW CLS, and (e) deploy APIs such as FindFileFirstW, FindFileNextW, SendMessageW and CopyFileApiW, etc. (I will try to explan why I need these a bit later).
(2) You might not need all of the above; but judging by what you described, you need most of them.
(3) Don't feel a bit intimidated. Remember that at the very beginning, probably I knew not more than you already knew.
Now, some explanation is in order:
(A) Why I need a unicode folder BAS? To show a thumbnail of the first JPG file in the folder the user has clicked [not the OK (i.e. Select) button, but any of the displayed paths, the thumbnail gives me a lead of what kind of photos the folder is for]. The said clicked path is shown at the upper portion in the Folder Dialog, in my case the path might be in Chinese, and in your case it might be in Arabic. The Folder Dialog is invoked through SHBrowseForFolderW with BIF_STATUSTEXT (meaning to show the clicked path at the top portion of Folder Dialog). Owing to this aspect, there is a light subclassing in this BAS.
(B) Why not just set FF_BROWSEINCLUDEFILES and let Folder Dialog display individual files under the user-clicked path? Because a wanted file pattern cannot be set, e.g. to limit/filter files to "JPG/JPEG/JPE" only in my case (my code can open other file formats, e.g. PNG).
(C) I don't use any subclassing in my unicode textbox CTL and unicode listbox CTL, just on a single-form basis. Why? Partly because I don't need any Event in this particular case, e.g. to handle user control's Mouse_Up, and partly because by definition of "CreateWindowExW", the basic functions needed are already inherently present. (I believe this piece of advice would save you tons of time and trouble, if you take it).
(D) What is unicode textbox used for? For example, to serve as a unicode label (VB label cannot display unicode string) to display the loaded file name and the image size, and to provide a dialog for soliciting a file name for "rename" operation (VB textbox cannot display unicode string).
(E) What is unicode listbox used for? To list files of the designated file pattern under a selected folder, if user finally clicks OK (i.e.Select) button in Folder Dialog (VB listbox cannot display unicode filename). User can then click any of the listed files to load it.
(F) What are GetOpenFileNameW/GetSaveFileNameW for? To open a file, or to save to a file (at current full screen size, not at original size, for easier transmittal when wanted). Folder Dialog and subsequent listing of files is just a means of accessing the files, File Open dialog is another means (you can drag/drop as well, but forget about this for now).
(G) Why SendMessageW, rather than SendMessage? To avoid a unicode string getting lost into ANSI.
Perhaps you don't have to do all of the above BAS/CTL/CLS if you don't want to; you should be able to find some valid postings here and PSC. I am new in vbForum, but at least I can recommend one to you: the File Open/Save Dialog CLS by La Volpe (though I don't favour the approach to a particular point, but it is just a petty difference in opnion which should not affect your use of his code). If you go to PSC, search "Vesa Piittinen" and "AndRay" might also yield you valid codes. Perhaps you should pick the one(s) without deploying a TLB file (unless you want it with a certain reason), and if available, the one(s) without a subclassing (unless you intend to make a full-blown coverage. Chances are you already feel dizzy about unicode stuff at the moment, hence a simpler/cleaner code would make your life a bit easier).
To summarize: Internally VB stores strings as Unicode, as such is capable of manipulating strings in any character of any language. It is just that when displaying a string, VB textbox control and alike do an implicit conversion from Unicode to ANSI. Internal to VB, the runtime is always converting Unicode to the current Windows ANSI code page identifier, therefore there is no way to change this conversion short of changing the ANSI code page for the system. This is the root which causes all the ensuing troubles and the above are but to tackle the problems programmatically for practical reasons.
Final remarks: A little while ago, I posted my unicode textbox user control, but then I had to spend lots of time to "defend" my code, e.g. "it must use subclassing" (ignoring some users would prefer it that way -- why should I reinvent the wheel, as there are already valid postings with subclassing), "no IOleInPlaceActiveObject" (not knowing the heavy cost of it), and (elsewhere) one author recommended a posting on how to include Event in the code (implying I must be unaware of Event stuff). Of course there have been a few users telling me that the code is very useful to them (another site sometime ago). I would othersise be very glad if someone provided a specific feedback on how the code failed to accept a Chinese input in some way (as it is the core of the code, my code claimed it was capable of handwriting recognition), but feel rather tired when the discussion is centered only on whether a Tab key failed to function properly (actually a change in TabStop setting would be okay). So to save trouble I decided not to post my code any more, I am not used to that kind of forum discussion.
Hope the above would give you some kind of direction, thus of a little help in some small way.
-
Re: Rename , Open, read Unicode files
@jooney
That work on xp but not allways in Windows7...(I can't find why)
Code:
.pFrom = StrPtr(sSourceFile & Chr(0) & Chr(0))
.pTo = StrPtr(sDesFile & Chr(0))
change it to this
Code:
.pFrom = StrPtr(sSourceFile )
.pTo = StrPtr(sDesFile ))
i found that the second form work..
I also notice that his run both on Xp and Windows 7...
Code:
Dim s$
Dim s2$
s$ = String(1024, 0)
s2$ = String(1024, 0)
Mid$(s$, 1, Len(sSourceFile)) = sSourceFile
Mid$(s2$, 1, Len(sDesFile)) = sDesFile
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
georgekar
@jooney
That work on xp but not allways in Windows7...(I can't find why)
Code:
.pFrom = StrPtr(sSourceFile & Chr(0) & Chr(0))
.pTo = StrPtr(sDesFile & Chr(0))
change it to this
Code:
.pFrom = StrPtr(sSourceFile )
.pTo = StrPtr(sDesFile ))
i found that the second form work..
I also notice that his run both on Xp and Windows 7...
Code:
Dim s$
Dim s2$
s$ = String(1024, 0)
s2$ = String(1024, 0)
Mid$(s$, 1, Len(sSourceFile)) = sSourceFile
Mid$(s2$, 1, Len(sDesFile)) = sDesFile
Please test again http://www.vbforums.com/showthread.p...=1#post3815066. It does work on my PC (Win7).
-
Re: Rename , Open, read Unicode files
I checked so I decide to use
Code:
Dim s$
Dim s2$
s$ = String(1024, 0)
s2$ = String(1024, 0)
Mid$(s$, 1, Len(sSourceFile)) = sSourceFile
Mid$(s2$, 1, Len(sDesFile)) = sDesFile
I also notice thar I can't use \\?\, something that I use for Unicode Filenames..
And I found it works...until now always. Maybe the len of buffer maybe is significant. So with 1204 bytes...we are OK.
I have you name in M2000 code...Look the source (is in the link in bottom).
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
Jonney
Since VB Controls and IDE is ANSI,thus Textbox shows Question Mark ???.
You go further to modify your locale to ARABIC on Control Panel. Start-->Control Panel -->Region and Language Options-->Advance Tab -->Then select language for non-unicode setting,suggest temporarily set to Arabic.
For your case,it isn't a permanent solution. You can use Lavolpe's Unicode Open/Save Dialog to choose that Arabic file,then use my above codes to change the file name via CODE,then you use unicode Messagebox or Print Text on Picturebox via DrawTextW API or an Unicode-aware control for displaying the result.
I have spend my time to make a demo for you.
Learn Unicode,please go to
http://www.cyberactivex.com/UnicodeTutorialVb.htm , it is a master page.
Cheers!
Your example Jonney run pretty good, but I still caught the problem is:
Example: Folder unicode and unicode filename also
D:\TRường\ABC\tưỡng đi sô.doc
in the ABC Folder files:
tính điểm.xls
Lữ Vĩnh tường.jpg
Sóc đi ăn.doc
Trình chiếu mã.doc
The problem is how to scan floder ABC to display the full name of the file to TextBox1 (fm20.dll), each file on the first line?
Thank Jonney and siblings lot.
My English is very bad, please forgive people.
-
Re: Rename , Open, read Unicode files
I've found DirW, print textBox1 pretty good.
The problem is to delete the file path name (Unicode) are not met Kill command (It is reported that Run-time error '52' Bad file name or number)
Please ask any KillW command?
Thank you
-
Re: Rename , Open, read Unicode files
Quote:
Originally Posted by
tonglep
The problem is how to scan floder ABC to display the full name of the file to TextBox1 (fm20.dll), each file on the first line?
Quote:
Originally Posted by
tonglep
I've found DirW, print textBox1 pretty good.
Here's another (likely more efficient) solution that you may want to consider:
Code:
Private Const INVALID_HANDLE_VALUE As Long = (-1&)
Private Const MAX_PATH As Long = 260
Private Type WIN32_FIND_DATA
dwFileAttributes As VbFileAttribute
ftCreationTime As Currency
ftLastAccessTime As Currency
ftLastWriteTime As Currency
nFileSize As Currency
dwReserved As Currency
cFileName As String * MAX_PATH
cAlternateFileName As String * 14
End Type
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByVal lpFindFileData As Long) As Long
Private Declare Function FindNextFileW Lib "kernel32.dll" (ByVal hFindFile As Long, ByVal lpFindFileData As Long) As Long
'This subroutine replaces the specified MSForms.TextBox's contents with
'a list of all filenames found in the specified ParentFolder directory
Public Sub PrintFileNames(ByRef ParentFolder As String, ByRef TextBox As MSForms.TextBox)
Dim hFindFile As Long, WFD As WIN32_FIND_DATA
TextBox.Text = vbNullString
hFindFile = FindFirstFileW(StrPtr(ParentFolder & "\*"), VarPtr(WFD))
If hFindFile <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) <> vbDirectory Then
TextBox.SelText = WFD.cFileName
TextBox.SelText = vbNewLine
End If
Loop While FindNextFileW(hFindFile, VarPtr(WFD))
hFindFile = FindClose(hFindFile): Debug.Assert hFindFile
End If
End Sub
Quote:
Originally Posted by
tonglep
The problem is to delete the file path name (Unicode) are not met Kill command (It is reported that Run-time error '52' Bad file name or number)
Please ask any KillW command?
Unlike the intrinsic Kill statement, this very thin wrapper of the DeleteFile function doesn't support wildcards:
Code:
Private Declare Function DeleteFileW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
Public Function KillW(ByRef PathName As String) As Boolean
KillW = DeleteFileW(StrPtr(PathName)): Debug.Assert KillW
End Function
-
Re: Rename , Open, read Unicode files
Problem solved. KillW very good use, why not see Thank button? Thank [Bonnie West] very much.
-
Re: Rename , Open, read Unicode files
I use for a long time (see post #34)
But I found some time hang my program so I make a new one.
(I cut it to fit here)
Code:
Declare Function MoveFile Lib "kernel32" Alias "MoveFileW" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long) As Long
Public Function RenameMoveFile(ByVal sSourceFile As String, ByVal sDesFile As String) As Boolean
Dim f$, fd$, flag As Long
If Left$(sSourceFile, 2) <> "\\" Then
f$ = "\\?\" + sSourceFile
Else
f$ = sSourceFile
End If
If Left$(sDesFile, 2) <> "\\" Then
fd$ = "\\?\" + sDesFile
Else
fd$ = sDesFile
End If
flag = 1
RenameMoveFile= 0 <> MoveFile(StrPtr(f$), StrPtr(fd$))
End Function