c++ - GetShortPathName 因网络驱动器上有中文文件夹而失败

标签 c++ c winapi window

我想用 GetShortPathName 函数在网络驱动器 F:\上获取一个带有中文文件夹名称的短路径名。当我从该文件夹运行 EXE 文件时,我看不到它能够获取短路径名。当我从 C:\驱动器执行此操作时,一切正常。

这是我的代码:

#include"stdafx.h"
#include<windows.h>
#include<tchar.h>
#include<stdio.h>
#include <iostream>

using namespace std;
#define BUFSIZE 4096


bool GetIsCaseCorrect(const WCHAR* fileName)
{
    bool result = false;
    // Correct case by converting to short path and back to long
    WCHAR shortFileName[_MAX_PATH];
    if (GetShortPathName(fileName, shortFileName, _MAX_PATH) != 0)
    {
        wchar_t correctFileName[_MAX_PATH];
        wcout << "ShortFile " << shortFileName;
        GetLongPathName(shortFileName, correctFileName, _MAX_PATH);
        result = wcscmp(fileName, correctFileName) != 0;
    }
    return result;
}

int main() {
    bool ret;
    HMODULE hModule = GetModuleHandleW(NULL);
    WCHAR path[MAX_PATH];
    GetModuleFileNameW(hModule, path, MAX_PATH);

    ret = GetIsCaseCorrect(path);
    getchar();
}

如果我运行此程序,短路径不会显示在文件夹为中文的非系统驱动器上。

我的 Windows 操作系统是 Windows 7。

最佳答案

据我所知,Windows 7机器默认使用SMB 2.0。 SMB 3.0 更好地支持 GetShortPathNameW 方法。

您可以按照下面的文档在 Win7 上启用 SMB 3.0。

https://support.microsoft.com/en-us/help/2696547/how-to-detect-enable-and-disable-smbv1-smbv2-and-smbv3-in-windows-and?wa=wsignin1.0%3Fwa%3Dwsignin1.0

或者您可以按照下面的文档检查Disable8dot3的卷状态。

https://blogs.msdn.microsoft.com/winsdk/2013/10/09/getshortpathname-doesnt-return-short-path-name/

最诚挚的问候,

毕男爵

关于c++ - GetShortPathName 因网络驱动器上有中文文件夹而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128464/

相关文章:

python - 与 Arduino Edison/Galileo 的语言兼容性

c - 在c中实现快速排序

c - ffmpeg av_seek_frame

c++ - 如何在 Windows 上清理命名管道?

windows - 在多 DPI 环境中将 HIMETRIC 转换为像素

c++ - Visual C++ 调试器可视化工具?

C++:从文件中读取并显示单个项目

c++ - 将 pfx 证书转换为 PEM 格式

c - 从 C 中的字符串中删除重复的字符

c - "scrolling range"是什么意思?