Question: Assignment: (1) Study Win32 API functions related to the process; (2) Write a program to create and terminate a new process on Windows OS using

Assignment:

(1) Study Win32 API functions related to the process;

(2) Write a program to create and terminate a new process on Windows OS using C++.

Background

1. CreatProcess( )

In Windows, one process can create another process by using the Win32 API function CreateProcess( ). Whenever a process is created, the OS performs a large amount of work such as allocating resources to the process, and creating a base thread for the process, etc.

The CreateProcess( ) function has many parameters, and some of them can be quite complex. Unlike the fork( ) call, which has no parameters and childs behavior is completely defined by parents profile and default behavior.

After the Windows OS has created the new process, it will return a handle for the child process and a handle for the base thread in the process.

Below is a copy of the function prototype for the CreateProcess( ) (check the Win32 API reference manual for details about each parameter). The function prototype does not use any standard C types. Instead, it uses a set of types defined in the windows.h file, many of which are just aliases for standard C types.

Bool CreatProcess(

LPCTSTR lpApplicationName,

// pointer to name of the executable module

LPTSTR lpCommanLine,

// pointer to command line string

LPSECURITY_ATTRIBUTES lpProcessAttributes,

// pointer to process security attributes

LPSECURITY_ATTRIBUTES lpThreadAttributes,

// pointer to thread security attributes

BOOL BInheritHandles, //handle inheritance flag

DWORD dwCreationFlags, // creation flags

LPVOID lpEnvironment, // pointer to new environment block

LPCTSTR lpCurrentDirectory, // pointer to current directory name

LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO

LPPROCESS_INFOMATON lpProcessInformation

// pointer to PROCESS_INFORMATION

);

The ten parameters in CreatProcess( ) provide great flexibility to the programmer, but for simple case, a default value can be used for many of them. For example, the following code skeleton shows how to create a child process with a single thread

#include

#include

#include

STARTUPINFO startInfo;

PROCESS_INFORMATION processInfo;

ZeroMemory(&startInfo, sizeof(startInfo));

startInfo.cb=sizeof(startInfo);

if(!CreateProcess(NULL, lpCommandLine, NULL, NULL, FALSE,

0, NULL, NULL, &startInfo, &processInfo))

{fprintf(stderr, CreatProcess failed on err %d , GetLastError( ) );

Note: lpCommandLine can be replaced by Notepad.exe, this will create a process of Notepad. This argument may need to be typecast as a wchar_t* for some systems. Google LPWSTR for more info.

2. TerminateProcess ()

This function terminates the specified process and all of its threads.

BOOL TerminateProcess (

HANDLE hProcess, // [in] Handle to the process to terminate

DWORD uExitCode); // [in] Specifies the exit code for the process and for all

threads terminated as a result of this call. such as 0.

)

Example:

HANDLE hProc1 = processInfo.hProcess; // Get Handle for this created process

TerminateProcess(hProc1,0); // Terminate this process

Useful URLs:

Create process: http://msdn.microsoft.com/en-us/library/ms682512

Terminate process: http://msdn.microsoft.com/en-us/library/aa450927.aspx

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!