1

我正在尝试在 C 中打开文件,但是当文件在 Windows 中包含拉丁字符时,我遇到了问题。

这段代码

hFile = CreateFileW(ws, // file to be opened
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // open existing file only
FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
// normal file archive and impersonate client
NULL); // no attr. template

if(hFile == INVALID_HANDLE_VALUE)
    printf("Could not open %ls file, error %d\n", ws, GetLastError());
else
    printf("File's HANDLE is OK!\n");

// when finished, close the file handle
CloseHandle(hFile);

当文件没有任何奇怪的字符但失败并出现错误 2 (ERROR_FILE_NOT_FOUND) 时,它可以完美运行。

--

例如,使用此文件:

C:\Documents and Settings\Administrador\Escritorio\hola.mp3

输出是

File's HANDLE is OK!

但是有了这个文件:

C:\Documents and Settings\Administrador\Escritorio\holá.mp3

输出是

Could not open C:\Documents and Settings\Administrador\Escritorio\holá.mp3 file, error 2

这两个文件都存在于该位置。

--

这是ws的初始化:

char* filename;
wchar_t  ws[256];

// I get the filename from the SDK I am using (Cycling'74 Max SDK)
filename = (atom_getsym(argv))->s_name;
// and convert it to UTF16
AnsiToUnicode16(filename, ws, 256);

AnsiToUnicode16用于MultiByteToWideChar进行转换。

--

当我使用 FindFirstFile() 遍历文件夹的文件时,我得到以下结果:

  • 下一个文件名为 hola.mp3。
  • 下一个文件名为 hol□.mp3。

我不知道如何让它知道hol□.mp3应该是holá.mp3

顺便说一句,如果文件夹是带有重音符号的文件夹,则 FindFirstFile() 失败。

4

1 回答 1

3

就像 Windows 告诉你的那样。具有该名称的文件不存在。虽然你认为你的名字是对的,但系统会告诉你你没有。系统是正确的。

大概是结果

filename = (atom_getsym(argv))->s_name;
AnsiToUnicode16(filename, ws, 256);

不会导致ws具有期望的值。

FWIW,FILE_ATTRIBUTE_ARCHIVE仅在创建文件时打开现有文件时没有影响。并且SECURITY_IMPERSONATION仅当您还包含SECURITY_SQOS_PRESENT.

于 2014-09-05T09:28:26.337 回答