سؤال

Here's my code. I always get error 3, what can I do? I tried to replace CreateProcces with CreateProcessA,replace first two param, try to process other program, but it still doesn't work.Thanks.

  #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) {}        // подождать
 }

Yes,you`re right. i've corrected it, but it still returns error code 3. First,notepad.exe isn't executed, second, getlasteeror returns code error 3, what i did wrong?

I place:

      char* path="C:\\Windows\\notepad.exe";
      CreateProcessA(path,"sfvfd",NULL,NULL,NULL,NULL,NULL,NULL,&cif,&pi);

instead of(and it's worked!):

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

What's the difference?

هل كانت مفيدة؟

المحلول 2

try this code from MSDN example

#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 );

نصائح أخرى

It is well documented on MSDN, if you read carefully:

First argument lpApplicationName:

The name of the module to be executed. [...]

The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. [...]

You don't want to put module name to execute into the first argument for whatever reason. This is OK if you then pass NULL as the argument.

You however pass a non-NULL pointer to an empty string. So API won't pick your notepad path, and instead attempts to run an empty string.

Nence, 3 = ERROR_PATH_NOT_FOUND "The system cannot find the path specified."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top