Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GetShortPathNameW返回文件扩展名。我希望它忽略.exe 我已经在C++中用MVC 2008免费版获得了SwitthNAMEW。我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名。有没有可能有人能帮我?我拥有的代码如下所示: WCHAR buffer[MAX_PATH]; LPCWSTR dataFilePath = wArgv[datafile_argv]; // Hack for Windows in case there are unicode chars in the path. // The normal argv[] array has ????? instead of the unicode chars // and fails, so instead we manually get the short file name, which // is always using ANSI chars. if (wcschr(dataFilePath, '\\') == NULL) { GetCurrentDirectoryW(MAX_PATH, buffer); wcscat(buffer, L"\\"); wcscat(buffer, dataFilePath); dataFilePath = &buffer[0]; } if (GetShortPathNameW(dataFilePath, directoryPathBuffer, MAX_PATH) == 0) { platform->DisplayAlert("Unable to determine startup path: GetShortPathNameW failed."); game_file_name = NULL; return; }_C++ - Fatal编程技术网

GetShortPathNameW返回文件扩展名。我希望它忽略.exe 我已经在C++中用MVC 2008免费版获得了SwitthNAMEW。我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名。有没有可能有人能帮我?我拥有的代码如下所示: WCHAR buffer[MAX_PATH]; LPCWSTR dataFilePath = wArgv[datafile_argv]; // Hack for Windows in case there are unicode chars in the path. // The normal argv[] array has ????? instead of the unicode chars // and fails, so instead we manually get the short file name, which // is always using ANSI chars. if (wcschr(dataFilePath, '\\') == NULL) { GetCurrentDirectoryW(MAX_PATH, buffer); wcscat(buffer, L"\\"); wcscat(buffer, dataFilePath); dataFilePath = &buffer[0]; } if (GetShortPathNameW(dataFilePath, directoryPathBuffer, MAX_PATH) == 0) { platform->DisplayAlert("Unable to determine startup path: GetShortPathNameW failed."); game_file_name = NULL; return; }

GetShortPathNameW返回文件扩展名。我希望它忽略.exe 我已经在C++中用MVC 2008免费版获得了SwitthNAMEW。我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名。有没有可能有人能帮我?我拥有的代码如下所示: WCHAR buffer[MAX_PATH]; LPCWSTR dataFilePath = wArgv[datafile_argv]; // Hack for Windows in case there are unicode chars in the path. // The normal argv[] array has ????? instead of the unicode chars // and fails, so instead we manually get the short file name, which // is always using ANSI chars. if (wcschr(dataFilePath, '\\') == NULL) { GetCurrentDirectoryW(MAX_PATH, buffer); wcscat(buffer, L"\\"); wcscat(buffer, dataFilePath); dataFilePath = &buffer[0]; } if (GetShortPathNameW(dataFilePath, directoryPathBuffer, MAX_PATH) == 0) { platform->DisplayAlert("Unable to determine startup path: GetShortPathNameW failed."); game_file_name = NULL; return; },c++,C++,我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名 当然,只要使用这个函数。您只需向它传递一个包含字符串的长度为MAX\u PATH的缓冲区,它就会在适当的位置剥离扩展名(如果有) std::wstring path(MAX_PATH, L'\0'); if (!GetCurrentDirectoryW(MAX_PATH, &path[0])) { throw std::runtime_error("The GetCurrentDirectory function f

我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名

当然,只要使用这个函数。您只需向它传递一个包含字符串的长度为
MAX\u PATH
的缓冲区,它就会在适当的位置剥离扩展名(如果有)

std::wstring path(MAX_PATH, L'\0');
if (!GetCurrentDirectoryW(MAX_PATH, &path[0]))
{
    throw std::runtime_error("The GetCurrentDirectory function failed");
}
PathRemoveExtension(&path[0]);
我建议使用或来连接路径元素,而不是使用C风格的字符串操作函数。这些函数设计用于处理路径,并自动为您添加反斜杠(或任何其他路径分隔符字符)


但我真的不理解你在问题中发布的代码,从评论开始:

// Hack for Windows in case there are unicode chars in the path.
// The normal argv[] array has ????? instead of the unicode chars
// and fails, so instead we manually get the short file name, which
// is always using ANSI chars.
也许更简单的解释方法是,作为参数传递给C程序入口点的标准
argv[]
数组必须是
char
类型的数组。在Windows上,这意味着它不支持Unicode字符。对Unicode的支持要求您使用
wchar\u t
(或等效宏之一)

通过使用
wmain
作为入口点,而不是
main
,您可以在Windows中专门解决此问题。对于此函数,
argv[]
是一个宽字符数组(
wchar\u t
)。我假设您正在为多个操作系统交叉编译代码,在这种情况下,您需要使用一些预处理器魔法来确保在针对Windows时获得正确的入口点

如果您绝对不能这样做,那么只需调用和函数来检索命令行参数,然后将它们转换为包含宽字符的
argv[]
样式数组

作为一种替代方法,您可以按照Alf的建议,调用函数,将
NULL
作为第一个参数传递,以检索可执行文件的路径

无论采用哪种方法,都必须认识到转换短路径是一种非常难看的黑客行为,而且如果底层文件系统禁用了短名称,这种行为实际上会被破坏。由于您的代码到处都在使用Unicode API,因此只要您首先获得有效的路径字符串,处理路径中的Unicode字符应该不会有问题

我只想使用它,这样它就可以搜索或返回不带.exe扩展名的文件名

当然,只要使用这个函数。您只需向它传递一个包含字符串的长度为
MAX\u PATH
的缓冲区,它就会在适当的位置剥离扩展名(如果有)

std::wstring path(MAX_PATH, L'\0');
if (!GetCurrentDirectoryW(MAX_PATH, &path[0]))
{
    throw std::runtime_error("The GetCurrentDirectory function failed");
}
PathRemoveExtension(&path[0]);
我建议使用或来连接路径元素,而不是使用C风格的字符串操作函数。这些函数设计用于处理路径,并自动为您添加反斜杠(或任何其他路径分隔符字符)


但我真的不理解你在问题中发布的代码,从评论开始:

// Hack for Windows in case there are unicode chars in the path.
// The normal argv[] array has ????? instead of the unicode chars
// and fails, so instead we manually get the short file name, which
// is always using ANSI chars.
也许更简单的解释方法是,作为参数传递给C程序入口点的标准
argv[]
数组必须是
char
类型的数组。在Windows上,这意味着它不支持Unicode字符。对Unicode的支持要求您使用
wchar\u t
(或等效宏之一)

通过使用
wmain
作为入口点,而不是
main
,您可以在Windows中专门解决此问题。对于此函数,
argv[]
是一个宽字符数组(
wchar\u t
)。我假设您正在为多个操作系统交叉编译代码,在这种情况下,您需要使用一些预处理器魔法来确保在针对Windows时获得正确的入口点

如果您绝对不能这样做,那么只需调用和函数来检索命令行参数,然后将它们转换为包含宽字符的
argv[]
样式数组

作为一种替代方法,您可以按照Alf的建议,调用函数,将
NULL
作为第一个参数传递,以检索可执行文件的路径


无论采用哪种方法,都必须认识到转换短路径是一种非常难看的黑客行为,而且如果底层文件系统禁用了短名称,这种行为实际上会被破坏。由于您的代码在任何地方都使用Unicode API,因此只要您首先获得有效的路径字符串,处理路径中的Unicode字符应该不会有问题。

您就有缓冲区溢出的风险。为什么不使用C++的东西,比如<代码> STD::WScRe>哦,要获取启动路径(与当前目录相反),请使用
GetModuleFilename
(如果我正确地调用了名称,请选中它)。最好抛出一个异常,让调用方处理失败,而不是强加一个模态对话框。问题真的只是“如何去掉字符串的最后4个字符?”是的@David。这就是我想要的。嗯,不是字符串,而是GetShortPathNameW所拥有的。std::wstring由于某种原因在本例中不起作用。您可能会面临缓冲区溢出的风险。为什么不使用C++的东西,比如<代码> STD::WScRe>哦,要获取启动路径(与当前目录相反),请使用
GetModuleFilename
(如果我正确地调用了名称,请选中它)。最好抛出一个异常,让调用方处理失败,而不是强加一个模态对话框。问题真的只是“如何去掉字符串的最后4个字符?”是的@David。这就是我想要的。嗯,不是字符串,而是GetShortPathNameW所拥有的。std::wstring由于某些原因无法工作