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
亞當水稻的回信傢伙非常感謝我現在可以看到我哪裏錯了我使用這個函數的原因是因爲當我使用系統函數調用它執行的.exe文件時,我不想讓黑色dos框彈出。我的應用程序是WINAPI有更好的解決方案嗎? –
那麼,正如Adam建議您可以從'CreateProcess'運行'cmd.exe'。另外,你可以嘗試在'STARTUPINFO'結構中設置窗口可見性標誌。有兩個地方你需要做到這一點(一個提供標誌,另一個告訴它你已經提供了標誌)。請參閱http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx – paddy
謝謝我試試欣賞你的時間。 –