Creating a Process with CreateProcess

The function CreateProcess is the standard way of creating processes. The code in Listing 5.1 prompts the user for the filename of an application (for example, "pword.exe" for Pocket Word) and then calls CreateProcess to run the application.

Listing 5.1. Creates a process with CreateProcess
void Listing5_1()
{
  TCHAR szApplication[MAX_PATH];
  PROCESS_INFORMATION pi;
  if(!GetTextResponse(_T("Enter Application to Run:"),
      szApplication, MAX_PATH))
    return;
  if(CreateProcess(szApplication,
        NULL, NULL, NULL, FALSE, 0,
        NULL, NULL, NULL, &pi) == 0)
    cout ≪ _T("Cannot create process") endl;
  else
  {
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
  }
}

The CreateProcess function (Table 5.1) takes ten parameters, of which only two are essential in Windows CE for creating a process.

Table 5.1. CreateProcess—Creates a new process
CreateProcess 
LPCWSTR pszImageName,Name of application to run.
LPCWSTR pszCmdLineCommand line arguments for application.
LPSECURITY_ATTRIBUTES psaProcessNot supported, pass as NULL.
LPSECURITY_ATTRIBUTES psaThreadNot supported, pass as NULL.
BOOL fInheritHandlesNot supported, pass as FALSE.
DWORD fdwCreateFlags specifying how to launch the application. Only the following are commonly used in Windows CE:
 CREATE_NEW_CONSOLE—Create a new console (only supported on platforms supporting cmd.exe)
 CREATE_SUSPENDED—Create process, but do not start executing thread.
PVOID pvEnvironmentNot supported, pass as NULL.
LPWSTR pszCurDirNot supported, pass as NULL.
LPSTARTUPINFO psiStartInfoNot supported, pass as NULL.
LPPROCESS_INFORMATIONpProcInfoPointer to a PROCESS_INFORMATION structure.
HANDLE Return ValueKernel object handle, or zero on error.

It is possible to pass a NULL for pszImageName, in which case pszCmdLine should point at a string containing the application filename followed by the command line arguments. If the application file name is not fully qualified, Windows CE will search in the "Windows" folder followed by the root "" folder. Platform builders can add an additional OEM-dependent folder and a "ceshell" directory to the search.

The last argument to CreateProcess is a PROCESS_INFORMATION structure in which four pieces of information about the new process are returned:

  • hProcess—A kernel object handle for the new process

  • hThread—A kernel object handle for the primary thread for the new process

  • dwProcessId—The process's system-wide unique identifier

  • dwThreadId—The thread's system-wide unique identifier

Kernel object handles and identifiers are described in the next section. For now, note that the handles returned in the PROCESS_INFORMATION structure must be closed by calling CloseHandle.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.217.167