ARGI有一些代碼:麻煩與lpCommandLine中的CreateProcess()
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;
}
這不能編譯。發佈真實的代碼。不要將const char *作爲lpCommandLine傳遞。 –