We help IT Professionals succeed at work.

FindFirstFile and unicode

myson
myson asked
on
Hello!
I have a problem with non-unicode version of function FindFirstFile(), which on NT fails when trying to enumerate a directory, whose filename has some unicode chars. This gives error message "invalid path". I would use a unicode version, but I want my program to work on Win9x too. Is there any solution (like calling the right function dynamically -- I don't accept any comments like compile one version with _UNICODE and one without this).
Thanks in advance,
MyS
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
A filename cannot contain 'some' UNICODE characters - it's either UNICODE or it isn't. Where do you get the path from before calling 'FindFirstFile()'?

Author

Commented:
I'm using recursive calling of FindFirstFile-FindNextFile. I call it for the first time for eg. C:, and recursively process directories. And this is the mystery. It normally lists the directory, but when I call FindFirstFile for this dirname, I receive the error. But the error only happens when calling for special-char (it's some eastern-europe) filename. Other directories on the disk work fine.
you can use the functions directly eg FindFirstFileW and FindFirstFileA - you can decide on which platform you are on and use the appropriate funtion call - the UNICODE define (or lack of) switches FindFirstFile between the above 2 functions

Author

Commented:
Shaun,
I have tried this, but compiler writes to the IAT of created exe file both of the functions, and so win9x reports error finding imported function FindFirstFileW. I I know that this is ideal solution for VC++6.0 - delay import loading, but not for VC++5.0.
> win9x reports error finding imported function FindFirstFileW

use LoadLibrary and GetProcAddress then

eg something like this

HANDLE hHandle=::LoadLibraryA("Kernel32.dll");

typedef HANDLE (WINAPI *LPFFINDFIRSTFILEW)(LPCWSTR,LPWIN32_FIND_DATAW) ;

LPFFINDFIRSTFILEW lpfFindFirstFileW=NULL;
     
lpfFindFirstFileW=(LPFFINDFIRSTFILEW)::GetProcAddressA(hHandle,"FindFirstFileW");

_WIN32_FIND_DATAW dataW;
lpfFindFirstFileW(L"autoexec.bat",&dataW);
> GetProcAddressA
oops there isn't one of them use GetProcAddress

Author

Commented:
Hmm, not very elegant, but works.
Thanks, ShaunWilde, and the points are yours.
> Hmm, not very elegant, but works

don't blame me blame MS :)