Skip to content

Unicode filename handling in new windows environment #755

@laanwj

Description

@laanwj

The new windows environment uses the *A functions instead of the *W ones:

  size_t error_text_size = ::FormatMessageA(
    ScopedHandle handle = ::CreateFileA(
        ::CreateFileA(filename.c_str(), desired_access, share_mode,
        ::CreateFileMappingA(handle.get(),
    ScopedHandle handle = ::CreateFileA(
    ScopedHandle handle = ::CreateFileA(
    return GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES;
    HANDLE dir_handle = ::FindFirstFileA(find_pattern.c_str(), &find_data);
    } while (::FindNextFileA(dir_handle, &find_data));
    if (!::DeleteFileA(filename.c_str())) {
    if (!::CreateDirectoryA(dirname.c_str(), nullptr)) {
    if (!::RemoveDirectoryA(dirname.c_str())) {
    if (!::GetFileAttributesExA(filename.c_str(), GetFileExInfoStandard,
    if (::MoveFileA(from.c_str(), to.c_str())) {
    if (::ReplaceFileA(to.c_str(), from.c_str(), /*lpBackupFileName=*/nullptr,
    ScopedHandle handle = ::CreateFileA(
    if (!GetTempPathA(ARRAYSIZE(tmp_path), tmp_path)) {

I think these use what the process code page happens to be, which might not be UTF-8. I don't claim to understand Windows unicode handling, so please tell me I'm wrong, but in our previous Windows environment we used

void ToWidePath(const std::string& value, std::wstring& target) {
	wchar_t buffer[MAX_PATH];
	MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, buffer, MAX_PATH);
	target = buffer;
}

void ToNarrowPath(const std::wstring& value, std::string& target) {
	char buffer[MAX_PATH];
	WideCharToMultiByte(CP_UTF8, 0, value.c_str(), -1, buffer, MAX_PATH, NULL, NULL);
	target = buffer;
}

along with the *W wide variants of Windows API functions (but #755 (comment) is a better way using std::codecvt).

I'm seeing some downstream test failures regarding file names, and I suspect this might be the cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions