我用VC2010 express建立一个solution,里面两个工程。
一个是MyConsole,启动一个进程。#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <WinBase.h>
int _tmain(int argc, _TCHAR* argv[]){
STARTUPINFO StartupInfo;
ZeroMemory(&StartupInfo,sizeof(STARTUPINFO));
StartupInfo.cb=sizeof(STARTUPINFO);
PROCESS_INFORMATION ProcessInformation;
ZeroMemory(&ProcessInformation,sizeof(PROCESS_INFORMATION));
ProcessInformation.hProcess=GetCurrentProcess();
BOOL ret=CreateProcess(_T("client"),0,0,0,TRUE,CREATE_DEFAULT_ERROR_MODE,0,0,&StartupInfo,&ProcessInformation);
if(FALSE==ret){
printf("%d\n",GetLastError());
}
printf("ok\n");
return 0;
}另一个就是client,就一个printf语句:int _tmain(int argc, _TCHAR* argv[])
{
printf("subprocess entry\n");
return 0;
}但是我程序运行的结果是:
2
okMSDN对此的解释是:
ERROR_FILE_NOT_FOUND
2 (0x2) The system cannot find the file specified.
 
奇怪了,也就是说client程序没有被找到。但是在工程的Debug目录里面,我看MyConsole和client都在那里啊。
这到底是为什么。

解决方案 »

  1.   

    TCHAR szPaht[MAX_PATH] = {_T("...\\Debug\\client.exe")};
    if(CreateProcess(NULL, szPath, ....))
    {
     ...
    }
      

  2.   

    BOOL WINAPI CreateProcess(
      __in_opt     LPCTSTR lpApplicationName,
      __inout_opt  LPTSTR lpCommandLine,
      __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,
      __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,
      __in         BOOL bInheritHandles,
      __in         DWORD dwCreationFlags,
      __in_opt     LPVOID lpEnvironment,
      __in_opt     LPCTSTR lpCurrentDirectory,
      __in         LPSTARTUPINFO lpStartupInfo,
      __out        LPPROCESS_INFORMATION lpProcessInformation
    );Parameters
    lpApplicationName 
    The name of the module to be executed. This module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer. The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:
    c:\program.exe files\sub dir\program name 
    c:\program files\sub.exe dir\program name 
    c:\program files\sub dir\program.exe name 
    c:\program files\sub dir\program name.exe 
    If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module as well as its arguments.To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.
      

  3.   

    The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. Bingo! "client.exe"