0% found this document useful (0 votes)
19 views

C Ms Windows Programming

The document discusses the MS Windows API for processes and threads. It describes functions like CreateProcess and CreateThread for creating processes and threads. It also covers related functions for waiting, exiting, and getting information on processes and threads. Examples of using the API in C code and Visual Studio are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Ms Windows Programming

The document discusses the MS Windows API for processes and threads. It describes functions like CreateProcess and CreateThread for creating processes and threads. It also covers related functions for waiting, exiting, and getting information on processes and threads. Examples of using the API in C code and Visual Studio are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MS Windows API for Processes/Threads

In MS Windows, the system call interface is not documented. Instead the


MS Windows API is documented, which helps with being able to run
programs portably across muliple versions of the MS Windows operating
systems.
Creating a process/thread gives you a handle that is used to refer to the
actual object that represents a process/thread.

I CreateProcess(...). Fork-and-exec a new process.


I CloseHandle(...).
I ExitProcess(...), TerminateProcess(...), GetExitCodeProcess(...),
GetCurrentProcessId(), GetCurrentProcess().
I CreateThread(...). Create a new thread and start running the start
function specified in the new thread.
I ExitThread(...), GetExitCodeThread(...), TerminateThread(...),
GetCurrentThreadId(), GetCurrentThread().
I WaitForSingleObject(...), WaitForMultipleObjects(...). These can be
used to wait for either a process or a thread.

Get detailed information from http://msdn.microsoft.com/library/


CreateProcess Call in MS Windows API

BOOL WINAPI C r e a t e P r o c e s s (
LPCTSTR l p A p p l i c a t i o n N a m e ,
LPTSTR lpCommandLine ,
LPSECURITY_ATTRIBUTES l p P r o c e s s A t t r i b u t e s ,
LPSECURITY_ATTRIBUTES l p T h r e a d A t t r i b u t e s ,
BOOL b I n h e r i t H a n d l e s ,
DWORD d w C r e a t i o n F l a g s ,
LPVOID l p E n v i r o n m e n t ,
LPCTSTR l p C u r r e n t D i r e c t o r y ,
LPSTARTUPINFO l p S t a r t u p I n f o ,
LPPROCESS_INFORMATION l p P r o c e s s I n f o r m a t i o n
);
Processes Related Calls in MS Windows API
W a i t F o r S i n g l e O b j e c t ( h P r o c e s s , INFINITE ) ;

CloseHandle ( pi . hProcess ) ;

DWORD WINAPI G e t C u r r e n t P r o c e s s I d ( v o i d ) ;
HANDLE WINAPI G e t C u r r e n t P r o c e s s ( v o i d ) ;

VOID WINAPI E x i t P r o c e s s (
UINT u E x i t C o d e
);
BOOL WINAPI T e r m i n a t e P r o c e s s (
HANDLE h P r o c e s s ,
UINT u E x i t C o d e
);
BOOL WINAPI G e t E x i t C o d e P r o c e s s (
HANDLE h P r o c e s s ,
LPDWORD l p E x i t C o d e
);
MS Windows API for Processes

t y p e d e f s t r u c t _PROCESS_INFORMATION {
HANDLE h P r o c e s s ;
HANDLE hThread ;
DWORD d w P r o c e s s I d ;
DWORD dwThreadId ;
} PROCESS_INFORMATION ,
∗LPPROCESS_INFORMATION ;

t y p e d e f s t r u c t _SECURITY_ATTRIBUTES {
DWORD n L e n g t h ;
LPVOID l p S e c u r i t y D e s c r i p t o r ;
BOOL b I n h e r i t H a n d l e ;
} SECURITY_ATTRIBUTES ,
∗LPSECURITY_ATTRIBUTES ;
MS Windows API for Threads
HANDLE WINAPI C r e a t e T h r e a d (
LPSECURITY_ATTRIBUTES l p T h r e a d A t t r i b u t e s ,
SIZE_T d w S t a c k S i z e ,
LPTHREAD_START_ROUTINE l p S t a r t A d d r e s s ,
LPVOID l p P a r a m e t e r ,
DWORD d w C r e a t i o n F l a g s ,
LPDWORD l p T h r e a d I d
);
// p r o t o t y p e f o r a t h r e a d s t a r t method
DWORD WINAPI T hr ea dPro c (
LPVOID l p P a r a m e t e r
);

DWORD WINAPI G e t C u r r e n t T h r e a d I d ( v o i d ) ;
HANDLE WINAPI G e t C u r r e n t T h r e a d ( v o i d ) ;

VOID WINAPI E x i t T h r e a d (
DWORD dwExitCode
);
BOOL WINAPI T e r m i n a t e T h r e a d (
HANDLE hThread ,
DWORD dwExitCode
);
Checking Errors in System Calls
I DWORD GetLastErrorCode(void). Retrieves the calling
thread’s last-error code value. The last-error code is
maintained on a per-thread basis. Multiple threads do not
overwrite each other’s last-error code. This function should be
called right after a system call returns an error (usually we
know that from a negative return value from the system call).
I To obtain an error string for system error codes, use the
FormatMessage function.
DWORD FormatMessage (
DWORD dwFlags ,
LPCVOID l p S o u r c e ,
DWORD dwMessageId ,
DWORD dwLanguageId ,
LPTSTR l p B u f f e r ,
DWORD n S i z e ,
v a _ l i s t ∗ Arguments
);
Sample Error Code
void ErrSys(char *szMsg)
{
LPVOID lpMsgBuf;

// Try to format the error message from the last failed call
// (returns # of TCHARS in message -- 0 if failed)
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // source and processing options
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, // message source
GetLastError(), // message identifier
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // language (Default)
(LPTSTR) &lpMsgBuf, // message buffer
0, // maximum size of message buffer
// (ignored with FORMAT_MESSAGE_ALLOCATE_BUFFER set)
NULL // array of message inserts
))
{
// Display the formatted string with the user supplied string at front.
fprintf(stderr, "%s: %s\n", szMsg, (LPSTR)lpMsgBuf);
LocalFree(lpMsgBuf); // Free the buffer.
} else {
fprintf(stderr, "%s: Could not get the error message!\n", szMsg);
}
fflush(NULL); /* flush all output streams */
ExitProcess(1); /* exit abnormally */
}
Using MS Visual Studio
I Visual Studio is available via MSDN program from the CS
department.
I Start up Visual Studio. Choose New Project → Visual C++
→ Win32 → Win32 Console Project.
I In the Wizard window, choose Application Settings → Empty
Project → Finish.
I Right click on the project in the right pane and then choose
Add → Add Existing Item.... Note that this doesn’t copy the
file into the Visual Studio project folder.
I Also note that, Visual Studio uses Unicode by default. For
now, we will simply turn this off. Press ALT+F7 to open the
project properties, and navigate to Configuration Properties →
General. Switch Character Set to “Multi-Byte Character
Setting".
I Tip. If you want to know definition of MS Windows API
typedefs, right-click on the type (e.g. LPVOID) and select “go
to definition” from the drop down menu.
MS Windows API Examples

I lab/ms-windows/ch2/fork-and-exec.c
I lab/ms-windows/ch2/fork-and-wait.c
I lab/ms-windows/ch2/fork-hello-world.c
I lab/ms-windows/ch2/fork-test.c
I lab/ms-windows/ch2/file-copy.c
I lab/ms-windows/ch2/thread-hello-world.c
I lab/ms-windows/ch2/thread-scheduling.c
I lab/ms-windows/ch2/thread-test.c
I and others in the ms-windows/ch2 examples folder....
Microsoft PowerShell
Powershell is a shell with a command-line and scripting language available
on Microsoft platforms.

I Aliases are built-in for common commands used in bash with


Unix/Linux/Mac OSX systems. For example, TAB is used for
command completion and aliases exist for ls, cp, man, date etc.
I Pipes are also supported but they pass objects instead of unstructured
text streams.
I Includes a dynamically typed scripting language with .NET
integration. Here is a simple example of a loop:
while ($true) {.\fork-hello-world; echo ""}
I The following shows how to time a command or script in powershell:
Measure-Command {sleep 2}
I Powershell script files are text files with a .ps1 extension. By default,
you cannot run scripts unless they are signed. To enable it, use the
following command:
Set-ExecutionPolicy RemoteSigned
Use the command Get-Help About_Signing to learn more about
signing.

You might also like