問題描述
您好,我有以下代碼,但沒有按預期工作,無法弄清楚問題出在哪里.
Hello I have the following code but it isn't working as expected, can't figure out what the problem is.
基本上,我正在執行一個進程(一個 .NET 進程)并傳遞命令行參數,它由 CreateProcess() 成功執行,但 CreateProcess() 沒有傳遞命令行參數
Basically, I'm executing a process (a .NET process) and passing it command line arguments, it is executed successfully by CreateProcess() but CreateProcess() isn't passing the command line arguments
我在這里做錯了什么??
What am I doing wrong here??
int main(int argc, char* argv[])
{
PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
LPTSTR cmdArgs = "name@example.com";
if(CreateProcess("D:\email\smtp.exe", cmdArgs,
NULL,NULL,FALSE,0,NULL,
NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
printf("Yohoo!");
}
else
{
printf("The process could not be started...");
}
return 0;
}
嘿還有一件事,如果我像這樣傳遞我的 cmdArgs
:
Hey one more thing, if I pass my cmdArgs
like this:
// a space as the first character
LPTSTR cmdArgs = " name@example.com";
然后我得到錯誤,然后 CreateProcess 返回 TRUE
但我的目標進程沒有執行.
Then I get the error, then CreateProcess returns TRUE
but my target process isn't executed.
Object reference not set to an instance of an object
推薦答案
你應該在參數中指定還模塊名稱:LPTSTR cmdArgs = "App name@example.com";代碼>它應該是整個命令行(包括 argv[0]).
You should specify also the module name in parameters: LPTSTR cmdArgs = "App name@example.com";
It should be the whole command line (including argv[0]).
這篇關于CreateProcess 不傳遞命令行參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!