2012-09-12 36 views
1

有這樣的詩句爲如何將執行路徑分隔爲文件路徑和參數?

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函數來解決這個問題?

+0

你可以使用* - *(短劃線)來標記你的字符串嗎? –

+0

@TonyTheLion但某些應用程序的參數以其他字符開頭 –

+3

您的意思是您正在嘗試編寫代碼來解析命令行中的程序和參數?這不是非常簡單,因爲在包含空格的路徑中存在歧義。查看[CreateProcess](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx)的文檔,特別是關於'lpApplicationName'參數的文檔。據我所知,沒有Windows API封裝該邏輯,所以你必須自己編寫它。 – Luke

回答

0

您需要的功能位於Windows中可以使用的標準C庫中。

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的對象)的數組包含輸入字符串的每個部分。只要知道要分割字符串的分隔符字符串,就可以使用此函數解析主要區塊和次要區塊。