2013-11-24 50 views
4

這是「sleeper.exe」的源代碼,我有:參數傳遞到可執行文件無法工作在C++

int main(int argc, char** argv) { 
    cout<<argv[1]; 
    return 0; 
} 

當我從命令行調用是這樣的:

C:\sleeper 5 

我見

5 
在命令行

所以這個工作得很好..

現在,我想打電話給一些其他的EXE此exe是這樣的:

std::cout << "ret is:" << ret; 
std::cout << "\n"; 

CreateProcess("sleeper.exe", // No module name (use command line) 
       ret, // 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 
      ) 

這裏RET是5,以及,我相信,因爲我看到它在命令行罰款:

ret is: 5 

有一個名爲config.mpap在同一個目錄下的文件和我讀值從這裏開始是這樣的:

std::ifstream myReadFile; 
myReadFile.open("config.mpap"); 

char output[400]; 
if (myReadFile.is_open()) { 
    while (!myReadFile.eof()) { 
     myReadFile >> output; 
    } 
} 
myReadFile.close(); 

char y = output[37]; 
int numberOfSleeps = y - '0'; // So now numberOfSleeps is 5 

然後我轉換numberOfSleepsRET這樣的:

char* ret = NULL; 
int numChars = 0; 
bool isNegative = false; 
// Count how much space we will need for the string 
int temp = sleepTime; 
do { 
    numChars++; 
    temp /= 10; 
} while (temp); 
ret = new char[ numChars + 1 ]; 
ret[numChars] = 0; 
if (isNegative) ret[0] = '-'; 
int i = numChars - 1; 
do { 
    ret[i--] = sleepTime % 10 + '0'; 
    sleepTime /= 10; 
} while (sleepTime); 

可以請別人幫我,爲什麼RET不通過從createprocess.exe到sleeper.exe?

編輯:

它的工作原理是這樣的:

if (!CreateProcess(NULL, // No module name (use command line) 
        "sleeper 5", // Command line 

然而,這並不甚至編譯:

std::string sleeper("sleeper "); 
sleeper += ret; 
if (!CreateProcess(NULL, // No module name (use command line) 
        sleeper, // Command line 
+0

@NicholasM [CreateProcess](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v = vs.85).aspx)是一個WINAPI函數。因此操作系統將成爲Windows。他不能複製它,因爲它是原生的。 – Kiruse

+0

@ Derija93,謝謝,我會刪除我的評論。 – NicholasM

+0

出於好奇,你在控制檯中看到了什麼? – Kiruse

回答

3

命令行(CreateProcess的的第二個參數)開出全命令行,包括可執行文件的名稱。如果第一個參數不是NULL,它將用作要運行的可執行文件,但命令行仍然必須包含可執行文件的名稱。在過去,甚至預先給一個空間(給出一個空的可執行文件名)爲我工作。

+0

這同意CreateProcess()[這裏](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v = vs.85).aspx)的描述。 – Simon

+0

對不起,我的英文不好,你的意思是通過「臥鋪」作爲第一個參數應該工作?如果你這樣說,那不適合我。 –

+0

@KorayTugay編輯中的第二個代碼原則上是正確的,但是您需要將指針傳遞給C函數。例如。 'std :: vector buffer {sleeper.cbegin(),sleeper.end()}; (0)buffer.push_back; CreateProcess(...,sleeper.data(),...);' – Philipp

相關問題