2013-05-12 298 views
0

這是我的代碼。我總是得到錯誤3,我能做什麼?我嘗試用CreateProcessA替換CreateProcces,替換前兩個參數,嘗試處理其他程序,但它仍然不起作用。謝謝。CreateProcess,winapi,錯誤代碼3

#include "windows.h" 
    #include <iostream> 

    void main() { 

    STARTUPINFOA cif; 
    ZeroMemory(&cif,sizeof(cif)); 
    PROCESS_INFORMATION pi; 
    CreateProcessA("","C:\\Windows\\notepad.exe",NULL,NULL, NULL,NULL,NULL,NULL,&cif,&pi); 

    DWORD error=GetLastError(); 
    std::cout << "error " << error << "\n"; 
    while(1) {}  // подождать 
} 

是的,你說得對。我已經糾正它,但它仍然返回錯誤代碼3. 首先,notepad.exe不執行,第二,getlasteeror返回代碼錯誤3,我做錯了什麼?

我把:

 char* path="C:\\Windows\\notepad.exe"; 
     CreateProcessA(path,"sfvfd",NULL,NULL,NULL,NULL,NULL,NULL,&cif,&pi); 

,而不是(和它的工作!):

 CreateProcessA("","C:\\Windows\\notepad.exe",NULL,NULL,    
     NULL,NULL,NULL,NULL,&cif,&pi); 

有什麼區別?

+0

你的測試有點奇怪。如果成功,'CreateProcess'返回一個非零值,所以你最好放鬆'== TRUE'。 – ChrisF 2013-05-12 11:38:00

+0

你怎麼知道它失敗?你沒有測試返回值。 – paulm 2013-05-12 11:47:34

+0

@paulm首先,notepad.exe沒有執行,第二,getlasteeror返回代碼錯誤3,我做錯了什麼? – Robert 2013-05-12 11:53:49

回答

1

嘗試從MSDN例如

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


void main() 
{ 
    STARTUPINFOA si; 
    PROCESS_INFORMATION pi; 

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

    // Start the child process. 
    if(!CreateProcessA(NULL,  // No module name (use command line) 
    "C:\\Windows\\notepad.exe", // 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); 
+0

謝謝,它的作品也是 – Robert 2013-05-12 12:04:25

2

這是well documented on MSDN,如果你仔細閱讀這個代碼:

第一個參數lpApplicationName

要執行的模塊的名稱。 [...]

lpApplicationName參數可以爲NULL。在這種情況下,模塊名稱必須是lpCommandLine字符串中第一個以空格分隔的標記。 [...]

您不希望將模塊名稱放入第一個參數中,無論出於何種原因。如果你通過NULL作爲參數,這是可以的。

但是,您將非NULL指針傳遞給空字符串。所以API不會選擇你的記事本路徑,而是嘗試運行一個空字符串。

Nence,3 = ERROR_PATH_NOT_FOUND「系統找不到指定的路徑。」

+0

是的,你是對的,謝謝你 – Robert 2013-05-12 13:28:25