CreateProcess()中lpCommandLine的问题
Trouble with lpCommandLine in CreateProcess()
argI有一些代码:
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 failn");system("pause");return 0;}
printf("handle: %Xn", pi.hProcess);
WaitForSingleObject( pi.hProcess, INFINITE );
system("pause");
return 0;
}
您确定是CreateProcess
正在创建该文件,而不是lb1spz .exe吗?
注意:
- 你正在使用宽字符串。如果forLb1SPZ.exe使用ANSI字符串,它可能会在D之后看到一个 。如果它应该,比如说,打开"D:1.txt",它可能会看到文件名为"D"(我对此并不肯定,但我想操作系统不会转换编码。我可能是错的,但确实是错的)。尝试使用
CreateProcessA
,看看是否有区别。 - 第二个参数必须是LPTSTR而不是LPCTSTR。你正在传递一个字符串字面量,根据文档,字符串可能会被
CreateProcess
改变。如果发生这种情况,你将有未定义的行为,并可能崩溃。 - 你在第一个和第二个参数中都传递了应用程序名称。这通常是多余的。这是故意的吗?
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- CreateProcess()中lpCommandLine的问题