C Ms Windows Programming
C Ms Windows Programming
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.