【发布时间】:2013-12-04 21:36:02
【问题描述】:
我正在编写一些路径解析 C++ 代码,并且为此尝试了很多 Windows API。 PathGetArgs/PathRemoveArgs 和稍加按摩的CommandLineToArgvW 有区别吗?
换句话说,除了长度/清洁度之外,是这样的:
std::wstring StripFileArguments(std::wstring filePath)
{
WCHAR tempPath[MAX_PATH];
wcscpy(tempPath, filePath.c_str());
PathRemoveArgs(tempPath);
return tempPath;
}
与此不同:
std::wstring StripFileArguments(std::wstring filePath)
{
LPWSTR* argList;
int argCount;
std::wstring tempPath;
argList = CommandLineToArgvW(filePath.c_str(), &argCount);
if (argCount > 0)
{
tempPath = argList[0]; //ignore any elements after the first because those are args, not the base app
LocalFree(argList);
return tempPath;
}
return filePath;
}
这是这个
std::wstring GetFileArguments(std::wstring filePath)
{
WCHAR tempArgs[MAX_PATH];
wcscpy(tempArgs, filePath.c_str());
wcscpy(tempArgs, PathGetArgs(tempArgs));
return tempArgs;
}
不同于
std::wstring GetFileArguments(std::wstring filePath)
{
LPWSTR* argList;
int argCount;
std::wstring tempArgs;
argList = CommandLineToArgvW(filePath.c_str(), &argCount);
for (int counter = 1; counter < argCount; counter++) //ignore the first element (counter = 0) because that's the base app, not args
{
tempArgs = tempArgs + TEXT(" ") + argList[counter];
}
LocalFree(argList);
return tempArgs;
}
?在我看来,PathGetArgs/PathRemoveArgs 只是提供了CommandLineToArgvW 解析的更清洁、更简单的特殊情况实现,但我想知道是否存在 API 行为不同的极端情况。
【问题讨论】:
-
似乎 SHLW-API (
Path*) 除了找到第一个空格字符之外没有做任何特别的事情。这可能是文档的全部内容:“此函数不应用于通用命令路径模板。”heuristics used byCommandLineToArgvW似乎是a bit more involved。 -
旧新事物链接+1。