From: Jean on
Hello Friedel
Here is the code:

int how_many_fichiers_unicode(char *rep) {
HANDLE hh;
wchar_t wrep[_MAX_PATH];
char full_name[_MAX_PATH]
int nbFic=0;
WIN32_FIND_DATAW wfind;
int ret, sz;
LPSTR ansi;

#define UNICODE
#define _UNICODE

ret = mbstowcs(wrep, (const char*)rep, _MAX_PATH);

hh = (HANDLE)FindFirstFileW(wrep, &wfind);
if(hh == (HANDLE)0xff) return -1;

do {
if(wfind.dwFileAttributes & _A_SUBDIR) {
continue;
}
else
if(wfind.dwFileAttributes & _A_HIDDEN) {
continue;
}

memset(full_name, 0, _MAX_PATH);
sz = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)NULL, 0,
NULL, NULL);
ansi = HeapAlloc(GetProcessHeap(), 0, sz+1);
ret = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)ansi,
sz+1, NULL, NULL);

MessageBox(NULL,ansi,"",MB_OK);

HeapFree(GetProcessHeap(), 0, ansi);
nbFic+=1;
} while(FindNextFileW(hh, &wfind));
FindClose(hh);

#undef _UNICODE
#undef UNICODE
return nbFic;
}

Jean

"Friedel Jantzen" <nospam_plz(a)freenet.de> a �crit dans le message de news:
1vs2f7kouo4qo$.zugdln4km8z9$.dlg(a)40tude.net...
> Hello Jean,
>
> please post the code.
>
> Am Wed, 7 Apr 2010 08:15:46 +0200 schrieb Jean:
>
>> Hello
>>
>> (pure C and SDK, XP/7)
>>
>> I try to read a directory with cyrillic and western mixed file names.
>> I use FindFirstFileW, WideCharToMultiByte(CP_UFT8...
>>
>> when i write the filename to a text file all is correct (with fwrite)
>
> If you converted the strings to UTF8, you cannot add them to a Windows
> control. If your project is Unicode, strings must be coded UCS-2 LE
> (WCHAR,
> WideChar) to use them for the APIs.
>
>> when i add the filename to a listbox i get garbage, idem if i use
>> MessageBoxA to display the filename
>
> I do not understand what you mean by "idem" (English ist not my mother
> tongue)?
> All unicode APIs expect WideChar, not UTF8.
>
> Regards,
> Friedel


From: Ulrich Eckhardt on
Jean wrote:
> #define UNICODE
> #define _UNICODE

Stop, this doesn't work. These are evaluated in a few headers. Once these
headers are included, and they are at this point, these macros don't have
any further effect. Define them on the commandline or in a central header
included first by every file of your project. Otherwise, the chance of
having those inconsistent for different parts of your code is just too big.
If you're using MSVC, set the charset to "Unicode" in the "general" tab of
the project's settings. In any case, these only affect what TCHAR is, but
you are (almost) not using any TCHAR functions but the explicit *W or *A
versions.

> hh = (HANDLE)FindFirstFileW(wrep, &wfind);
> if(hh == (HANDLE)0xff) return -1;

Two big nonos here:
1. Don't cast the returnvalue of FFF, use the correct type on the left side
instead.
2. The value it returns in case of errors is documented, use that macro
instead. I think it's INVALID_HANDLE_VALUE, but it could also be NULL.

Read the documentation at http://msdn.microsoft.com and/or install a plugin
for Firofox that looks up such functions there.

> if(wfind.dwFileAttributes & _A_SUBDIR) {

You generally never need anything starting like _THIS, i.e. underscore
followed by an uppercase letter. Again, read the documentation for the file
attributes, the constants are documented there.

> sz = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)NULL,
> 0,
> NULL, NULL);
> ansi = HeapAlloc(GetProcessHeap(), 0, sz+1);
> ret = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)ansi,
> sz+1, NULL, NULL);
>
> MessageBox(NULL,ansi,"",MB_OK);

How about a straightforward "MessageBoxW(NULL, wfind.cFileName, ...)"
instead? This will work.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: Jean on
Ulrich, thank you for the answer.

I defined UNICODE because it did not want to modify all my program (several
thousands of lines).
I thought that i could use it in a small module just to read file names,
then add these file names to a syslistview.
But it seems that i will probably not be able to do that.
Must i declare all my project as UNICODE and modify declarations such as
char -> w_char, etc... ?

(_A_SUBDIR is described in microsoft.com)

Jean

"Ulrich Eckhardt" <eckhardt(a)satorlaser.com> a �crit dans le message de news:
9ib097-c3n.ln1(a)satorlaser.homedns.org...
> Jean wrote:
>> #define UNICODE
>> #define _UNICODE
>
> Stop, this doesn't work. These are evaluated in a few headers. Once these
> headers are included, and they are at this point, these macros don't have
> any further effect. Define them on the commandline or in a central header
> included first by every file of your project. Otherwise, the chance of
> having those inconsistent for different parts of your code is just too
> big.
> If you're using MSVC, set the charset to "Unicode" in the "general" tab of
> the project's settings. In any case, these only affect what TCHAR is, but
> you are (almost) not using any TCHAR functions but the explicit *W or *A
> versions.
>
>> hh = (HANDLE)FindFirstFileW(wrep, &wfind);
>> if(hh == (HANDLE)0xff) return -1;
>
> Two big nonos here:
> 1. Don't cast the returnvalue of FFF, use the correct type on the left
> side
> instead.
> 2. The value it returns in case of errors is documented, use that macro
> instead. I think it's INVALID_HANDLE_VALUE, but it could also be NULL.
>
> Read the documentation at http://msdn.microsoft.com and/or install a
> plugin
> for Firofox that looks up such functions there.
>
>> if(wfind.dwFileAttributes & _A_SUBDIR) {
>
> You generally never need anything starting like _THIS, i.e. underscore
> followed by an uppercase letter. Again, read the documentation for the
> file
> attributes, the constants are documented there.
>
>> sz = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)NULL,
>> 0,
>> NULL, NULL);
>> ansi = HeapAlloc(GetProcessHeap(), 0, sz+1);
>> ret = WideCharToMultiByte(CP_UTF8, 0, wfind.cFileName, -1, (LPSTR)ansi,
>> sz+1, NULL, NULL);
>>
>> MessageBox(NULL,ansi,"",MB_OK);
>
> How about a straightforward "MessageBoxW(NULL, wfind.cFileName, ...)"
> instead? This will work.
>
> Uli
>
> --
> Sator Laser GmbH
> Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932
>


From: Timo Kunze on
Am 08.04.2010 10:11, schrieb Jean:
> (_A_SUBDIR is described in microsoft.com)

Yes, it is, but in a different context. If you want to use the _A_*
constants, then you should also use _find/_wfind instead of FindFirstFile.

Timo
--
www.TimoSoft-Software.de - Unicode controls for VB6
"Those who sacrifice freedom for safety deserve neither."
"Demokratie ist per Definition unsicher. Ihr Schutz entsteht aus der
Überzeugung, dass die demokratischen Kräfte überwiegen und sich – auf
demokratischem Wege – durchsetzen."
From: Jean on
OK, got it, thank you

Jean
"Timo Kunze" <TKunze71216(a)gmx.de> a �crit dans le message de news:
hpk3ki$sf5$1(a)speranza.aioe.org...
> Am 08.04.2010 10:11, schrieb Jean:
>> (_A_SUBDIR is described in microsoft.com)
>
> Yes, it is, but in a different context. If you want to use the _A_*
> constants, then you should also use _find/_wfind instead of FindFirstFile.
>
> Timo
> --
> www.TimoSoft-Software.de - Unicode controls for VB6
> "Those who sacrifice freedom for safety deserve neither."
> "Demokratie ist per Definition unsicher. Ihr Schutz entsteht aus der
> �berzeugung, dass die demokratischen Kr�fte �berwiegen und sich - auf
> demokratischem Wege - durchsetzen."