CreateProcess() 中的 lpCommandLine 出现问题

发布于 2024-12-10 12:07:36 字数 1222 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-12-17 12:07:36

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

注意:

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

Are you sure it's CreateProcess that's creating that file, and not forLb1SPZ.exe?

Note:

  1. You're using wide strings. If forLb1SPZ.exe uses ANSI strings, it might see a \0 after the D. If it should, say, open "D:\1.txt", it might see the file name as "D" (I'm not positive on this, but I imagine the OS doesn't convert the encoding. I might be wrong, and it does). Try using CreateProcessA and see if there's a difference.
  2. The second argument must be a LPTSTR and not an LPCTSTR. You're passing a string literal, and according to the docs that string might be changed by CreateProcess. If that happens, you'll have undefined behavior, and possibly a crash.
  3. You're passing the app name in both the first and second arguments. That's usually redundant. Is it intentional?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文