You are here: Home » Builder » [ En ] » Unicode files path

Unicode files path

27 January |

  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]

You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Filed under

Builder, [ En ]

| Tags:

Comments are closed.