PathGetArgs / PathRemoveArgs vs. CommandLineToArgvW - 有什么区别吗? [英] PathGetArgs/PathRemoveArgs vs. CommandLineToArgvW - is there a difference?

查看:288
本文介绍了PathGetArgs / PathRemoveArgs vs. CommandLineToArgvW - 有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些路径解析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]; //忽略第一个之后的任何元素,因为它们是args,而不是基本应用程序

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 ++)//忽略第一个元素(counter = 0),因为这是基本应用程序,而不是args
{
tempArgs = tempArgs + TEXT()+ argList [counter];
}

LocalFree(argList);

return tempArgs;
}

?看起来像 PathGetArgs / PathRemoveArgs 只是提供一个更干净,更简单的特殊情况下的实现 CommandLineToArgvW 解析,但我想知道是否有任何角落的情况下,API的行为会有所不同。

解决方案

函数是类似的,但不完全一样 - 主要与如何处理引号字符串有关。



PathGetArgs 返回一个指向输入字符串中第一个空格后面的第一个字符的指针。如果在第一个空格之前遇到引号字符,则在函数开始再次查找空格之前需要另一个引号。如果没有找到空格,函数返回指向字符串结尾的指针。



PathRemoveArgs 调用 PathGetArgs ,然后使用返回的指针终止字符串。



CommandLineToArgvW 接收提供的字符串并将其拆分为数组。它使用空格来描绘数组中的每个项目。数组中的第一项可以引用以允许空格。第二个和后续项也可以引用,但它们支持稍微更复杂的处理 - 参数也可以包括嵌入的引号,前面加上反斜杠。例如:

 c:\program files\my app\my app.exearg1argument 2 arg \number\3

这将产生一个包含四个条目的数组: / p>


  • argv [0] - c:\program files\ my app\my app.exe

  • argv [1] - arg1

  • argv [2] - 参数2

  • argv [3] - argnumber3



请参阅 CommandLineToArgVW 文档以获取解析规则的完整描述,包括如何在参数中嵌入反斜杠以及引号。


I'm working on some path-parsing C++ code and I've been experimenting with a lot of the Windows APIs for this. Is there a difference between PathGetArgs/PathRemoveArgs and a slightly-massaged CommandLineToArgvW?

In other words, aside from length/cleanness, is this:

std::wstring StripFileArguments(std::wstring filePath)
{
  WCHAR tempPath[MAX_PATH];

  wcscpy(tempPath, filePath.c_str());
  PathRemoveArgs(tempPath);

  return tempPath;
}

different from this:

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;
}

and is this

std::wstring GetFileArguments(std::wstring filePath)
{
  WCHAR tempArgs[MAX_PATH];

  wcscpy(tempArgs, filePath.c_str());
  wcscpy(tempArgs, PathGetArgs(tempArgs));

  return tempArgs;
}

different from

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;
}

? It looks to me like PathGetArgs/PathRemoveArgs just provide a cleaner, simpler special-case implementation of the CommandLineToArgvW parsing, but I'd like to know if there are any corner cases in which the APIs will behave differently.

解决方案

The functions are similar but not exactly the same - mostly relating to how quoted strings are handled.

PathGetArgs returns a pointer to the first character following the first space in the input string. If a quote character is encountered before the first space, another quote is required before the function will start looking for spaces again. If no space is found the function returns a pointer to the end of the string.

PathRemoveArgs calls PathGetArgs and then uses the returned pointer to terminate the string. It will also strip a trailing space if the first space encountered happened to be at the end of the line.

CommandLineToArgvW takes the supplied string and splits it into an array. It uses spaces to delineate each item in the array. The first item in the array can be quoted to allow spaces. The second and subsequent items can also be quoted, but they support slightly more complex processing - arguments can also include embedded quotes by prepending them with a backslash. For example:

 "c:\program files\my app\my app.exe" arg1 "argument 2" "arg \"number\" 3"

This would produce an array with four entries:

  • argv[0] - c:\program files\my app\my app.exe
  • argv[1] - arg1
  • argv[2] - argument 2
  • argv[3] - arg "number" 3

See the CommandLineToArgVW docs for a full description of the parsing rules, including how you can have embedded backslashes as well as quotes in the arguments.

这篇关于PathGetArgs / PathRemoveArgs vs. CommandLineToArgvW - 有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆