【问题标题】:How to separate a executing path to file path and parameter?如何将执行路径与文件路径和参数分开?
【发布时间】:2012-09-12 08:33:57
【问题描述】:

有这样的行

C:\Program Files\Realtek\Audio\HDA\RAVCpl64.exe -s

在注册表项中

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

现在我想将绝对路径和参数从行中分离出来。

如果该行是

C:\Space Dir\Dot.Dir\Sample Name.Dot.exe param path."C:\Space Dir\Dot.Dir\Sample Name.Dot.exe"

我应该使用哪个分隔符来处理这条线?有没有 Windows API 函数可以解决这个问题?

【问题讨论】:

  • 您可以使用 -(破折号)来标记您的字符串吗?
  • @TonyTheLion 但某些应用程序的参数以其他字符开头
  • 您的意思是您正在尝试编写代码来从命令行解析程序和参数?这不是很简单,因为在包含空格的路径中存在歧义。查看CreateProcess 的文档,特别是关于lpApplicationName 参数的文档。据我所知,没有封装该逻辑的 Windows API,因此您必须自己编写。
  • 第一个路径没有用双引号括起来。
  • @Jay 程序即将解析上面 reg 键中的所有值。用引号括起来实际上使解析更容易,但我想知道 reg 键中是否有这种未封闭的行。

标签: c++ windows winapi registry


【解决方案1】:

你想要的函数在标准 C 库中,你可以在 Windows 中使用。

char theDrive[5],thePath[MAX_PATH],theFilename[MAX_PATH],theExtension[MAX_PATH];

_splitpath(theSCTDataFilename,theDrive,thePath,theFilename,theExtension);

您还可以使用像这样的更通用的标记函数,它接受任何字符串、char 和 CStringArray..

void tokenizeString(CString theString, TCHAR theToken, CStringArray *theParameters)
{
CString temp = "";
int i = 0;

for(i = 0; i < theString.GetLength(); i++ ) 
    {                                   
    if (theString.GetAt(i) != theToken) 
       {
       temp += theString.GetAt(i);  
       }
     else
       {
       theParameters->Add(temp);    
       temp = "";
       }

         if(i == theString.GetLength()-1)
      theParameters->Add(temp);
    }
}

CStringArray thePathParts;
tokenizeString("your\complex\path\of\strings\separated\by\slashes",'/',&thePathParts);

这将为您提供一个 CString 数组(CStringArray 对象),其中包含输入字符串的每个部分。只要您知道要拆分字符串的分隔符,您就可以使用此函数解析主要块然后解析次要块。

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多