Top Banner
Win32 Programming Lesson 8: Processes
24

Win32 Programming

Feb 26, 2016

Download

Documents

maitland

Win32 Programming. Lesson 8: Processes. Where are we?. We’re starting to have some foundational understanding of Windows But really, we don’t know how to get any work done (doesn’t that sound like your Sophomore year?). Executing Code. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Win32 Programming

Win32 ProgrammingLesson 8: Processes

Page 2: Win32 Programming

Where are we? We’re starting to have some foundational

understanding of Windows But really, we don’t know how to get any

work done (doesn’t that sound like your Sophomore year?)

Page 3: Win32 Programming

Executing Code The Operating System organizes executing

code into a logical hierarchy Processes Threads Fibers

Page 4: Win32 Programming

Definition 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

Page 5: Win32 Programming

Threads 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

Page 6: Win32 Programming

Scheduling 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

Page 7: Win32 Programming

Windows Applications Two flavors

GUI (all that Windowsy stuff) CUI (command line – the One True Way™)

Of course, life isn’t so simple – it’s not a binary decision

Every program needs an entry point – WinMain, wWinMain etc.

The Operating System doesn’t call this – it calls a special startup function

Page 8: Win32 Programming

Startup 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 RTL’s global variables Initialize the heap for the RTL Call constructors for global and static objects

Page 9: Win32 Programming

A 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);

Page 10: Win32 Programming

The 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

);

Page 11: Win32 Programming

Free alloced memory int nNumArgs;

PWSTR *ppArgv = CommandLineToArgvW(GetCommandLineW(), &nNumArgs);

// Use the arguments… if (*ppArgv[1] == L'x') {     }

// Free the memory block HeapFree(GetProcessHeap(), 0, ppArgv);

Page 12: Win32 Programming

Environment Variables Associated on a per-process basis Stored in the environment block Stored in the form:

VarName1=Value1\0VarName2=Value2\0…VarNameX=ValueX\0\0

Page 13: Win32 Programming

Associated Functions GetEnvironmentVariable(

PCTSTR pszName,PTSTR pszValue,DWORD dwSize);

Often in the form “%USERPROFILE%\My Documents”

Windows supplies a helper function for this case

Page 14: Win32 Programming

ExpandEnvironmentString ExpandEnvironmentStrings(

PCTSTR pszSrc,PTSTR pszDst,DWORD nSize);

Where nSize is the maximum space available for expansion

Page 15: Win32 Programming

ErrorModes A process can choose to trap certain errors

itself instead of having the Operating System trap those errors

UINT SetErrorMode(UINT fuErrorMode)

Page 16: Win32 Programming

CurrentDirectory and Directory Example: CreateFile DWORD GetCurrentDirectory (

DWORD cchCurDir,PTSTR pszCurDir);

BOOL SetCurrentDirectory(PCTSTR pszCurDir);

Current Directories are also stored in the environment variables

Page 17: Win32 Programming

Finally… GetVersion and GetVersionEx Read the history, it’s somewhat amusing

Page 18: Win32 Programming

Creating 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 let’s just look it up…

Page 19: Win32 Programming

Terminating a Process Four ways

The primary thread’s 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)

Page 20: Win32 Programming

Entry 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

Page 21: Win32 Programming

ExitProcess Fine from an OS perspective Horrible from the RTL perspective as

Destructors aren’t called – you can prove this to yourself if you want to

Page 22: Win32 Programming

ChildProcessesPROCESS_INFORMATION pi; DWORD dwExitCode; // Spawn the child process. BOOL fSuccess = CreateProcess(..., &pi); if (fSuccess) {   

// Close the thread handle as // soon as it is no longer needed!    CloseHandle(pi.hThread);   // Suspend our execution until // the child has terminated.    WaitForSingleObject(pi.hProcess, INFINITE);    // The child process terminated; //get its exit code.    GetExitCodeProcess(pi.hProcess, &dwExitCode);    // Close the process handle as soon as it// is no longer needed.    CloseHandle(pi.hProcess);

}

Page 23: Win32 Programming

Detaching a Process In the previous example, the processes are

linked – the child process won’t be destroyed until the Parent reads the exit code

However, you can detach a process by closing the associated handles in the parent process

Page 24: Win32 Programming

Enumerating 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 that’s a little more tricky.

Your call – CLI is okay, but a nice GUI gets you extra marks…