Raz-Soft » Blog Archive » Unicode files path Buy Cheap Software
Re: Top 5 EVER »
« Imagini live din… metrou

Unicode files path

  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...

C++:
  1. bool __fastcall FileExistsW(WideString WFilename)
  2. {
  3.   WIN32_FIND_DATAW FindFileData;
  4.   HANDLE hFind;
  5.  
  6.   hFind = FindFirstFileW(WFilename.c_bstr(), &FindFileData);
  7.  
  8.   if (hFind != INVALID_HANDLE_VALUE) {
  9.     FindClose(hFind);
  10.     return true;
  11.   }
  12.   return false;
  13. }
  14.  
  15.  
  16. bool __fastcall DirectoryExistsW(WideString WFilename)
  17. {
  18.   WIN32_FIND_DATAW FindFileData;
  19.   HANDLE hFind;
  20.  
  21.   hFind = FindFirstFileW(WFilename.c_bstr(), &FindFileData);
  22.  
  23.   if (hFind != INVALID_HANDLE_VALUE) {
  24.     FindClose(hFind);
  25.     if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return true;
  26.   }
  27.   return false;
  28. }

  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:

C++:
  1. int argCount=0;
  2.           LPWSTR* args;
  3.           args=CommandLineToArgvW(GetCommandLineW(),&argCount);
  4.           if (argCount>1)
  5.            {
  6.             AppParams=args[1];
  7.             AppParams=WideExcludeTrailingBackslash(AppParams);
  8.  
  9.            }

related things:

Author: Raz | On January 27th, 2007 | C++ Builder, [ En ] | No Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions