Title: Win32 Programming
1Win32 Programming
2Where are we?
- Were starting to have some foundational
understanding of Windows - But really, we dont know how to get any work
done (doesnt that sound like your Sophomore
year?)
3Executing Code
- The Operating System organizes executing code
into a logical hierarchy - Processes
- Threads
- Fibers
4Definition
- A Process is defined as
- An instance of a running program, which consists
of two components - A kernel object used by the operating system to
manage the process - An address space which contains all the DLLs,
executable code and data used by the process - Processes are inert to accomplish anything, a
process must have at least one thread that runs
in its context
5Threads
- Each thread executes simultaneously (in some
sense of the word why, why not?) - Each thread has its own stack frame, and set of
CPU registers
6Scheduling
- The OS schedules some time for each thread
- Each thread gets a time slice or quantum from the
CPU which provides the illusion of concurrency - We can think of it as a round robin scheduler
but it is significantly much more complicated - When a process is created, it is generally
created with one thread, called the primary
thread this thread can create other threads
7Windows Applications
- Two flavors
- GUI (all that Windowsy stuff)
- CUI (command line the One True Way)
- Of course, life isnt so simple its not a
binary decision - Every program needs an entry point WinMain,
wWinMain etc. - The Operating System doesnt call this it calls
a special startup function
8Startup Code
- The Runtime Libraries do a number of things
- Retrieve a pointer to the command line parameters
of the process - Retrieve a pointer to the new environment
variables of the process - Initialize the RTLs global variables
- Initialize the heap for the RTL
- Call constructors for global and static objects
9A Process Instance Handle
- Every DLL or executable gets its own Instance
Handle, which is unique - Used when we load resources (like icons)
- Can get the handle of a DLL with something like
- HMODULE GetModuleHandle(PCTSTR pszModule)
10The Command Line
- Can use the global variables __argv
- Can call down to PTSTR GetCommandLine()
- Can go from here to an argv-like structure using
- PWSTRÂ CommandLineToArgvW( Â Â Â PWSTRÂ pszCmdLine,Â
   int pNumArgs)
11Free alloced memory
- int nNumArgs PWSTR ppArgv  CommandLineToArgvW(
GetCommandLineW(), nNumArgs)
// Use the arguments if (ppArgv1  L'x')Â
   - // Free the memory block HeapFree(GetProcessHeap(
), 0, ppArgv)
12Environment Variables
- Associated on a per-process basis
- Stored in the environment block
- Stored in the form
- VarName1Value1\0VarName2Value2\0VarNameXVal
ueX\0\0
13Associated Functions
- GetEnvironmentVariable( PCTSTR pszName, PTSTR
pszValue, DWORD dwSize) - Often in the form USERPROFILE\My Documents
- Windows supplies a helper function for this case
14ExpandEnvironmentString
- ExpandEnvironmentStrings( PCTSTR pszSrc, PTSTR
pszDst, DWORD nSize) - Where nSize is the maximum space available for
expansion
15ErrorModes
- A process can choose to trap certain errors
itself instead of having the Operating System
trap those errors - UINT SetErrorMode(UINT fuErrorMode)
16CurrentDirectory and Directory
- Example CreateFile
- DWORD GetCurrentDirectory ( DWORD
cchCurDir, PTSTR pszCurDir) - BOOL SetCurrentDirectory(PCTSTR pszCurDir)
- Current Directories are also stored in the
environment variables
17Finally
- GetVersion and GetVersionEx
- Read the history, its somewhat amusing
18Creating a Process
- Simple use CreateProcess
- BOOL CreateProcess( PCTSTR pszApplicationName,
PTSTR pszCommandLine, PSECURITY_ATTRIBUTES
psaProcess, PSECURITY_ATTRIBUTES
psaThread, BOOL bInheritHandles, DWORD
fdwCreate, PVOID pvEnvironment, PCTSTR
pszCurDir, PSTARTUPINFO psaStartInfo, PPROCESS
_INFORMATION ppiProcInfo) - I could write out the usage, but lets just look
it up
19Terminating a Process
- Four ways
- The primary threads entry-point function returns
(YES) - One thread in the process calls ExitProcess (NO)
- A thread in another process calls
TerminateProcess (NO) - All the threads just happen to die on their own
(Never happens)
20Entry point returns
- When the primary thread dies
- Any C objects are destroyed using destructors
- Memory from the stack is correctly freed
- The process exit code is set
- The process kernel object is decremented
21ExitProcess
- Fine from an OS perspective
- Horrible from the RTL perspective as Destructors
arent called you can prove this to yourself if
you want to
22ChildProcesses
PROCESS_INFORMATIONÂ pi DWORDÂ dwExitCode
// Spawn the child process. BOOL fSuccess  Creat
eProcess(..., pi) if (fSuccess)Â
  // Close the thread handle as //
soon as it is no longer needed!
   CloseHandle(pi.hThread)   // Suspend our e
xecution until // the child has terminated.
   WaitForSingleObject(pi.hProcess, INFINITE)
   // The child process terminated //get itsÂ
exit code.    GetExitCodeProcess(pi.hProcess, d
wExitCode)    // Close the process handle as so
on as it // is no longer needed.
   CloseHandle(pi.hProcess)
23Detaching a Process
- In the previous example, the processes are linked
the child process wont be destroyed until the
Parent reads the exit code - However, you can detach a process by closing the
associated handles in the parent process
24Enumerating Processes
- Assignment time!
- Easily create a simple application which
enumerates all the processes on a machine. You
may use a command-line application if you wish,
or a GUI. Print out as much information about
each process as you can. - This looks really nice in .NET but thats a
little more tricky. - Your call CLI is okay.
- Use the Submit server!