gpt4 book ai didi

c++ - CreateProcess() 中的 lpCommandLine 出现问题

转载 作者:行者123 更新时间:2023-11-28 03:45:37 24 4
gpt4 key购买 nike

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;
}

最佳答案

您确定是 CreateProcess 创建了该文件,而不是 forLb1SPZ.exe 吗?

注意:

  1. 您使用的是宽字符串。如果 forLb1SPZ.exe 使用 ANSI 字符串,它可能会在 D 之后看到一个\0。如果它应该打开“D:\1.txt”,它可能会看到文件名为“D”(我不确定关于这一点,但我想操作系统不会转换编码。我可能错了,但确实如此)。尝试使用 CreateProcessA 并查看是否存在差异。
  2. 第二个参数必须是 LPTSTR 而不是 LPCTSTR。您正在传递一个字符串文字,根据文档,该字符串可能会被 CreateProcess 更改。如果发生这种情况,您将遇到未定义的行为,并且可能会发生崩溃。
  3. 您将在第一个和第二个参数中传递应用名称。这通常是多余的。是故意的吗?

关于c++ - CreateProcess() 中的 lpCommandLine 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7786614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com