CreateProcess,winapi,错误代码3

CreateProcess,winapi,错误代码3,winapi,createprocess,Winapi,Createprocess,这是我的密码。我总是遇到错误3,我能做什么?我试着用CreateProcessA替换CreateProcces,替换前两个参数,试着处理其他程序,但仍然不起作用。谢谢 #include "windows.h" #include <iostream> void main() { STARTUPINFOA cif; ZeroMemory(&cif,sizeof(cif)); PROCESS_INFORMATION pi; C

这是我的密码。我总是遇到错误3,我能做什么?我试着用CreateProcessA替换CreateProcces,替换前两个参数,试着处理其他程序,但仍然不起作用。谢谢

  #include "windows.h"
  #include <iostream>

  void main() {

     STARTUPINFOA cif;
     ZeroMemory(&cif,sizeof(cif));
     PROCESS_INFORMATION pi;
     CreateProcessA("","C:\\Windows\\notepad.exe",NULL,NULL, NULL,NULL,NULL,NULL,&cif,&pi);

     DWORD error=GetLastError();
     std::cout << "error " << error << "\n";
     while(1) {}        // подождать
 }
而不是(而且成功了!):


有什么区别吗?

试试MSDN示例中的代码

#include <windows.h>
#include <stdio.h>


void main()
{  
  STARTUPINFOA si;
  PROCESS_INFORMATION pi;

  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );

  // Start the child process. 
  if(!CreateProcessA( NULL,     // No module name (use command line)
    "C:\\Windows\\notepad.exe", // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
  {
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
  }

  // Wait until child process exits.
  WaitForSingleObject( pi.hProcess, INFINITE );

  // Close process and thread handles. 
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );
#包括
#包括
void main()
{  
新创资讯科技有限公司;
处理信息;
零内存(&si,sizeof(si));
si.cb=sizeof(si);
零内存(&pi,sizeof(pi));
//启动子进程。
如果(!CreateProcessA(NULL,//没有模块名(使用命令行)
“C:\\Windows\\notepad.exe”,//命令行
NULL,//进程句柄不可继承
NULL,//线程句柄不可继承
FALSE,//将句柄继承设置为FALSE
0,//没有创建标志
NULL,//使用父级的环境块
NULL,//使用父级的起始目录
&si,//指向STARTUPINFO结构的指针
&pi)//指向进程信息结构的指针
) 
{
printf(“CreateProcess失败(%d)。\n”,GetLastError());
返回;
}
//等待子进程退出。
WaitForSingleObject(pi.hProcess,无限);
//关闭进程和线程句柄。
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

尝试MSDN示例中的此代码

#include <windows.h>
#include <stdio.h>


void main()
{  
  STARTUPINFOA si;
  PROCESS_INFORMATION pi;

  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );

  // Start the child process. 
  if(!CreateProcessA( NULL,     // No module name (use command line)
    "C:\\Windows\\notepad.exe", // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
  {
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
  }

  // Wait until child process exits.
  WaitForSingleObject( pi.hProcess, INFINITE );

  // Close process and thread handles. 
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );
#包括
#包括
void main()
{  
新创资讯科技有限公司;
处理信息;
零内存(&si,sizeof(si));
si.cb=sizeof(si);
零内存(&pi,sizeof(pi));
//启动子进程。
如果(!CreateProcessA(NULL,//没有模块名(使用命令行)
“C:\\Windows\\notepad.exe”,//命令行
NULL,//进程句柄不可继承
NULL,//线程句柄不可继承
FALSE,//将句柄继承设置为FALSE
0,//没有创建标志
NULL,//使用父级的环境块
NULL,//使用父级的起始目录
&si,//指向STARTUPINFO结构的指针
&pi)//指向进程信息结构的指针
) 
{
printf(“CreateProcess失败(%d)。\n”,GetLastError());
返回;
}
//等待子进程退出。
WaitForSingleObject(pi.hProcess,无限);
//关闭进程和线程句柄。
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
如果您仔细阅读:

第一个参数
lpApplicationName

要执行的模块的名称。[…]

lpApplicationName参数可以为NULL。在这种情况下,模块名称必须是lpCommandLine字符串中第一个空格分隔的标记。[…]

无论出于何种原因,您都不希望将要执行的模块名放入第一个参数中。如果随后将
NULL
作为参数传递,则这是可以的

但是,您会传递一个指向空字符串的非空指针。因此API不会选择您的记事本路径,而是尝试运行空字符串

Nence,
3
=
错误路径未找到
“系统无法找到指定的路径。”

如果您仔细阅读:

第一个参数
lpApplicationName

要执行的模块的名称。[…]

lpApplicationName参数可以为NULL。在这种情况下,模块名称必须是lpCommandLine字符串中第一个空格分隔的标记。[…]

无论出于何种原因,您都不希望将要执行的模块名放入第一个参数中。如果随后将
NULL
作为参数传递,则这是可以的

但是,您会传递一个指向空字符串的非空指针。因此API不会选择您的记事本路径,而是尝试运行空字符串


Nence,
3
=
错误路径未找到
“系统找不到指定的路径。”

您的if测试有点奇怪。
CreateProcess
如果成功,将返回一个非零值,因此您最好取消
==TRUE
。您怎么知道它失败了?您没有测试返回值。@paulm首先,notepad.exe没有执行,其次,getlasteer返回代码错误3,我做错了什么?如果没有失败,那么GetLastError的e值没有任何意义如果测试有点奇怪。
CreateProcess
如果成功将返回一个非零值,因此最好不要使用
==TRUE
。你怎么知道它失败了?你没有测试返回值。@paulm首先,notepad.exe没有执行,其次,GetLasteError返回代码错误3,这是什么id错误?如果没有失败,则GetLastError的值没有意义
#include <windows.h>
#include <stdio.h>


void main()
{  
  STARTUPINFOA si;
  PROCESS_INFORMATION pi;

  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );

  // Start the child process. 
  if(!CreateProcessA( NULL,     // No module name (use command line)
    "C:\\Windows\\notepad.exe", // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
  {
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
  }

  // Wait until child process exits.
  WaitForSingleObject( pi.hProcess, INFINITE );

  // Close process and thread handles. 
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );