Unicode files path

Posted on the January 27th, 2007 under Builder, [ En ] by Raz

  Checking if a file or folder exists with FileExists and DirectoryExists (VCL) will not work on Unicode files path, so here are the Unicode versions…

[cpp]
bool __fastcall FileExistsW(WideString WFilename)
{
WIN32_FIND_DATAW FindFileData;
HANDLE hFind;

hFind = FindFirstFileW(WFilename.c_bstr(), &FindFileData);

if (hFind != INVALID_HANDLE_VALUE) {
FindClose(hFind);
return true;
}
return false;
}

bool __fastcall DirectoryExistsW(WideString WFilename)
{
WIN32_FIND_DATAW FindFileData;
HANDLE hFind;

hFind = FindFirstFileW(WFilename.c_bstr(), &FindFileData);

if (hFind != INVALID_HANDLE_VALUE) {
FindClose(hFind);
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return true;
}
return false;
}
[/cpp]
  How about Unicode files passed at command line? WinMain function will return char*, but you need WideString (wchar_t *) to do your job with the file/path… Well there is fast way to handle that, just don’t use the argument parameter passed at WinMain because obviously you will not get your path correctly, instead use the Unicode version of the GetCommandLine function: GetCommandLineW
Here is a sample:
[cpp]
int argCount=0;
LPWSTR* args;
args=CommandLineToArgvW(GetCommandLineW(),&argCount);
if (argCount>1)
{
AppParams=args[1];
AppParams=WideExcludeTrailingBackslash(AppParams);

}
[/cpp]

Leave a Reply

Skip, ignore me





XHTML::
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>