2012-10-24 57 views
0

ello我希望有人可以幫助我解決這個問題我正在使用下面的例子從msdn for createprocess函數。格式在CreateProcess中的字符串C++

#include <windows.h> 
#include <stdio.h> 
#include <tchar.h> 

void _tmain(int argc, TCHAR *argv[]) 
{ 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 

    ZeroMemory(&si, sizeof(si)); 
    si.cb = sizeof(si); 
    ZeroMemory(&pi, sizeof(pi)); 

    if(argc != 2) 
    { 
     printf("Usage: %s [cmdline]\n", argv[0]); 
     return; 
    } 

    // Start the child process. 
    if(!CreateProcess(NULL, // No module name (use command line) 
     argv[1],  // Command line 
     NULL,   // Process handle not inheritable 
     NULL,   // Thread handle not inheritable 
     FALSE,   // Set handle inheritance to FALSE 
     0,    // No creation flags 
     NULL,   // Use parent's environment block 
     NULL,   // Use parent's starting directory 
     &si,   // Pointer to STARTUPINFO structure 
     &pi)   // Pointer to PROCESS_INFORMATION structure 
    ) 
    { 
     printf("CreateProcess failed (%d).\n", GetLastError()); 
     return; 
    } 

    // Wait until child process exits. 
    WaitForSingleObject(pi.hProcess, INFINITE); 

    // Close process and thread handles. 
    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 
} 

這是通過dos訪問和完美使用命令提示符下使用此命令。當我的Result.txt

鍵入此

this.exe「my.exe測試」>的my.exe是另一個控制檯應用程序,需要輸入測試和>的Result.txt是我的輸出日誌。我試圖刪除命令行的需要,所以我試圖將路徑提供給createprocess函數調用。這是我深陷其中是什麼,我想

if(!CreateProcess(NULL, // No module name (use command line) 
     "\"my.exe test \" > result.txt",  // this.exe "my.exe test" > result.txt 
     NULL,   // Process handle not inheritable 
     NULL,   // Thread handle not inheritable 

我認爲\」會給我我需要的結果仍然不工作,但它似乎沒有工作,將解析my.exe和測試部分,但不是>的Result.txt輸出。然而,它在命令提示細工作,如果我通過它的argv [1]。

任何幫助非常讚賞。

因此,在總結

在控制檯I可以解析

this.exe "my.exe test" > result.txt // Works fine via cmd.exe 

到App我試圖

my.exe test > result.txt // Not work but missing "" 

\"my.exe test \" > result.txt // work for first section 

回答

3

CreateProcess不僅會被打破的話成單獨的參數基本的命令行解析 - 它不理解文件重定向或任何。如果你通過"> result.txt",它會試圖解析這兩個參數,字面意思是">""result.txt"

如果你想重定向命令的輸出,你有兩個選擇:

  1. 自己做重定向。爲此,首先打開文件CreateFile(傳遞使句柄可繼承的安全屬性),然後將結果句柄分配給傳入的結構的hStdOut成員。然後,請記住關閉文件CreateProcess返回後,否則你會泄漏一個句柄。
  2. 使用其他程序進行重定向。當你在命令行輸入命令時,它是cmd.exe,它分析你的命令行並執行文件重定向。因此,而不是創建一個my.exe過程中,您可以改爲創建一個cmd.exe過程中使用命令行是這樣的:

    cmd.exe "my.exe test > result.txt" 
    
3

CreateProcess只期望一個可執行文件的名字和一些參數。重定向實際上不是程序參數。這由shell解釋(cmd.exe)。

當你調用你自己的程序時發生了什麼如下...

cmd> this.exe "my.exe test" > result.txt 

argv[0] = "this.exe" 
argv[1] = "my.exe test" 
Output is sent to result.txt by the shell 

現在,你的一個不工作:

cmd> this.exe my.exe test > result.txt 

argv[0] = "this.exe" 
argv[1] = "my.exe" 
argv[2] = "test" 
Output is sent to result.txt by the shell 

你會看到,因爲你只發送給argv[1]CreateProcess,該行爲並不如你預期。

現在,正如我所提到的,CreateProcess並不實際重定向輸出。要做到這一點,你應該使用system調用,它調用cmd.exe或任何命令解釋器系統採用:

system("my.exe test > result.txt"); 

參見:http://msdn.microsoft.com/en-us/library/277bwbdz(v=vs.80).aspx

+0

亞當水稻的回信傢伙非常感謝我現在可以看到我哪裏錯了我使用這個函數的原因是因爲當我使用系統函數調用它執行的.exe文件時,我不想讓黑色dos框彈出。我的應用程序是WINAPI有更好的解決方案嗎? –

+0

那麼,正如Adam建議您可以從'CreateProcess'運行'cmd.exe'。另外,你可以嘗試在'STARTUPINFO'結構中設置窗口可見性標誌。有兩個地方你需要做到這一點(一個提供標誌,另一個告訴它你已經提供了標誌)。請參閱http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx – paddy

+0

謝謝我試試欣賞你的時間。 –