Lab Session #1 Process Creation
Lab Session # 1
Process Creation
#include
#include
using namespace std;
int main() {
HANDLE hProcess = NULL;
HANDLE hThread = NULL;
STARTUPINFO Si; // STARTUP is a keyword
PROCESS_INFORMATION Pi;
DWORD dwProcessId = 0;
DWORD dwThreadId = 0;
ZeroMemory (&Si, sizeof(Si)); //For Memory Allocation
ZeroMemory (&Pi, sizeof(Pi)); //For Memory Allocation
BOOL bCreateProcess = NULL;
bCreateProcess = CreateProcess("C:/Program Files/Windows Media
Player/wmplayer.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);
if (bCreateProcess == FALSE){
cout << " Create Process Failed & Error no " << GetLastError() << endl;
3
}
else {
cout << " Create Process Success " << endl;
cout << "Process ID" << Pi.dwProcessId << endl;
cout << "Thread ID" << Pi.dwThreadId << endl;
// cout << "GetPId" << GetProcessId(Pi.hProcess) << endl;
// cout << "GetId" << GetThreadId(Pi.hThread) << endl;
WaitForSingleObject(Pi.hProcess, INFINITE);
CloseHandle(Pi.hThread);
CloseHandle(Pi.hProcess);
return 0;
}
}
The Procedure CreateProcess has the following form (you don’t have to implement
CreateProcess).
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL fInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new envirnment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
4
LPPROCESS_INFORMATION lpProcessInformation // process information
Exercise
Part – I
Implement the code segment given at the start of the first page in Visual C++. Create a
win32 console application (empty project) names “ProcessCreate”. Then go to the file
view and add a file process.c, implement the code in this file.
Part – II
Create a new win32 console application “Hello”. The application should simply display
Hello World on the screen. Compile and build an executable file “Hello.exe” for this
program. Place the executable in the same folder as the “ProcessCreate” source files.
Then modify the “ProcessCreate” source code to execute the Hello world executable.