BBC BASIC for Windows
Programming >> Database and Files >> How to OPEN(IN) a file with unicode filename?
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1475955083

How to OPEN(IN) a file with unicode filename?
Post by hellomike on Oct 8th, 2016, 7:31pm

Hi,

In belows code, OPENIN() returns 0 when trying to open a file with an unicode filename.

Code:
...
VDU 23,22,800;800;8,16,8,8
PRINT filename$
H%=OPENIN(filename$) 

The string from filename$ prints out OK, i.e. as unicode.

What should I change to make it work?

The current workaround I can think of is first copying the file with a neutral name to @tmp$ using the API call "SHFileOperationW" and then opening that copy instead.

I hope avoiding that though.

Thanks

Mike
Re: How to OPEN(IN) a file with unicode filename?
Post by michael on Oct 10th, 2016, 02:27am

You may need to email Richard or leave a message on his forum concerning this subject. Its a bit beyond my knowledge and seems moderators are absent atm.

http://bbcbasic.conforums.com/


Re: How to OPEN(IN) a file with unicode filename?
Post by DDRM on Oct 10th, 2016, 07:58am

I agree this would be a good one to run past Richard,as it's something he might want to add to his list of "things to change if I do another version"! Also, he knows what he's talking about...

Looking at the windows documentation, I see that the OpenFile function can't handle unicode. I don't know if this is what BB4W uses "under the hood" in the OPENIN function. There is an alternative, called "CreateFile" which has a W version (presumably "CreateFileW") which does handle unicode. It returns a handle to the file. I don't have time to try playing with it now, but you may then be able to use this just as you would any other file handle returned by OPENIN. You may need to close the file yourself, probably using CloseHandle.

I'm sure you realise, but others may not, that these are Windows API calls, so you'll need to use SYS, and you'll need to look up the right parameters, etc.

If you make it work, please post a simple example for the benefit of the rest of us!

smiley

D
Re: How to OPEN(IN) a file with unicode filename?
Post by DDRM on Oct 10th, 2016, 12:58pm

I tried this, which seems to work:
Code:
      FILE_ATTRIBUTE_NORMAL = &80
      GENERIC_READ = &80000000
      OPEN_EXISTING = 3
      SYS "CreateFile","Text1.txt",GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0 TO f%
      WHILE NOT EOF#f%
        INPUT#f%,q$
        PRINT q$
      ENDWHILE
      CLOSE #f%
      REM Test whether it's closed properly
      f%=OPENIN"Text1.txt"
      WHILE NOT EOF#f%
        INPUT#f%,q$
        PRINT q$
      ENDWHILE
      CLOSE #f%
 

I'd expect you to need to use CreateFileW to handle a unicode name string, but simply changing the name of the function failed. I suspect it may be necessary to convert the filename to UFT16 using MultibyteToWideChar: there's an example of how to do that in the section called "Displaying GIF and JPEG images", but I don't have time to play with it now...

D
Re: How to OPEN(IN) a file with unicode filename?
Post by hellomike on Oct 11th, 2016, 10:12am

Thanks for your help guys.

As you might have noticed, Richard answered in this thread:
http://bbcbasic.conforums.com/index.cgi?board=language&action=display&num=1476136244