Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Process Control Using Win32 API

Process Control Using Win32 API

Ratings: (0)|Views: 2,638|Likes:
Published by zainvi.sf6018
This brief tutorial describes how to create, control, and kill a process using Win32 API. Its a gist from various sources and must for someone trying windows systems programming.
This brief tutorial describes how to create, control, and kill a process using Win32 API. Its a gist from various sources and must for someone trying windows systems programming.

More info:

Published by: zainvi.sf6018 on Jun 01, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Prepared by: Syed Feroz Zainvi Available at: http://www.computer-science-notes.blogspot.com E-mail: zainvi.sf@gmail.com
Creating A Process
A process can be created using CreateProcess function of Win32 API. The prototype of this function is:
BOOL CreateProcess(LPCTSTR lpszImageName, // path of executable fileLPTSTR lpszCommandLine, // command lineLPSECURITY_ATTRIBUTES lpsaProcess, // process security attributesLPSECURITY_ATTRIBUTES lpsaThread, // thread security attributesBOOL fInheritHandles, // does new process inherit handlesDWORD fdwCreate, // process creation flagsLPVOID lpvEnvironment, // environment block for new processLPCTSTR lpszCurDir, // current folder for new processLPSTARTUPINFO lpsiStartInfo, // specifies window featuresLPPROCESS_INFORMATION lppiProcInfo); // new process information
To create a process, you can simply pass the name of the process leaving the other parameters to takethe default value NULL except the ninth and tenth parameter. Ninth parameter is the address of thestructure STARTUPINFO that you need to fill before calling CreateProcess and contains informationrequired to run the process. Tenth parameter is the address of the uninitialized structurePROCESS_INFORMATION which receives the information about the newly created process.
STARTUPINFO structure must be initialized to 0 to avoid process crash due to corrupted data. Thisinitialization can be done using either of the following function calls:
 memset(&StartupInfo, 0, sizeof(StartupInfo));::ZeroMemory(&StartupInfo, sizeof(StartupInfo));
Of the several fields in STARTUPINFO, field ‘cb’ must be set to the size of the STARTUPINFO structure.
Following call will launch the notepad with readme.txt file opened in it.
STARTUPINFO StartupInfo;PROCESS_INFORMATION ProcInfo; memset (&StartupInfo, 0, sizeof (STARTUPINFO));StartupInfo.cb = sizeof (STARTUPINFO);::CreateProcess ("notepad.exe", _T("readme.txt"),NULL,NULL,FALSE,0,NULL,NULL,&StartupInfo, &ProcInfo);
If application path is not in your PATH environment variable then you need to specify to append the complete path to the application name, e.g. “C:\\Windows\\System32\\notepad.exe”. Note the use of double back-slash in the path string.
Running A Console Application Silently
To run a console application without starting the associated command prompt, you need to set followingtwo fields of STARTUPINFO before calling the CreateProcess function.
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;StartupInfo.wShowWindow = SW_HIDE;
Both these fields are of DWORD type. The first field dwFlags informs the application that we are settingwindow display information while second field wShowWindow sets the window to hide.
Killing A Process
The Win32 API function used to kill a process is:

Activity (4)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Ronen Peker liked this
avitech007 liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->