CreateProcess() 中的 lpCommandLine 出现问题
arg我有一些代码:
CreateProcess(L"D:\\prog\\forLb1SPZ.exe",L"D:\\prog\\forLb1SPZ.exe D:\\1.txt",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)
此代码正在工作,但是...文件正在程序附近创建(编写此代码的位置)并且名称为“D” - argv[1] 的第一个符号。怎么了?
forLb1SPZ.exe 中的代码
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include <math.h>
using namespace std;
int _tmain(int argc, char* argv[])
{
int value;
FILE *Ptr;
Ptr=fopen("argv[1]","w");
for(int i=0;i<20000;i++){
value=rand();
fprintf(Ptr,"%d i=%d \n",value,i);
}
fclose(Ptr);
return 0;
}
lab2SPZ.exe 中的代码(主程序)
#include "stdafx.h"
#include "iostream"
#include <windows.h>
#include <stdio.h>
using namespace std;
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcess(L"D:\\forLb1SPZ.exe","D:\\forLb1SPZ.exe D:\\1.txt",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
{printf( "creating fail\n");system("pause");return 0;}
printf("handle: %X\n", pi.hProcess);
WaitForSingleObject( pi.hProcess, INFINITE );
system("pause");
return 0;
}
argI have some code:
CreateProcess(L"D:\\prog\\forLb1SPZ.exe",L"D:\\prog\\forLb1SPZ.exe D:\\1.txt",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)
THis code is working, BUT... file is creating near the program (where this code is wrote) and has name "D" - the first symbol of argv[1]. What`s wrong?
Code in forLb1SPZ.exe
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include <math.h>
using namespace std;
int _tmain(int argc, char* argv[])
{
int value;
FILE *Ptr;
Ptr=fopen("argv[1]","w");
for(int i=0;i<20000;i++){
value=rand();
fprintf(Ptr,"%d i=%d \n",value,i);
}
fclose(Ptr);
return 0;
}
Code in lab2SPZ.exe (main program)
#include "stdafx.h"
#include "iostream"
#include <windows.h>
#include <stdio.h>
using namespace std;
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcess(L"D:\\forLb1SPZ.exe","D:\\forLb1SPZ.exe D:\\1.txt",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
{printf( "creating fail\n");system("pause");return 0;}
printf("handle: %X\n", pi.hProcess);
WaitForSingleObject( pi.hProcess, INFINITE );
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定是
CreateProcess
创建该文件,而不是 Lb1SPZ.exe 吗?注意:
Are you sure it's
CreateProcess
that's creating that file, and not forLb1SPZ.exe?Note:
CreateProcessA
and see if there's a difference.CreateProcess
. If that happens, you'll have undefined behavior, and possibly a crash.