CS410 lecture24

Document Sample
CS410 lecture24 Powered By Docstoc
					W   indows
    PROGRAMMING
                                      Lecture 24



                  L e c t u r e   #




                        24
W   indows
    PROGRAMMING
                                                Lecture 24

                  Review of Last Lecture
       Windows Common Controls
       Common controls library commctl32.dll
       InitCommonControlsEx()
       Image Lists
       List View control
W   indows
    PROGRAMMING
                                                     Lecture 24

                  Memory management basics
    •   Separate address space for a process
    •   Physical and Virtual Memory
    •   Page table
    •   Address Space: Linear and Physical Address
    •   Committed and reserved pages
    •   Same address in different processes may map to
        different things or nothing
W   indows
    PROGRAMMING
                                                      Lecture 24

           Basic concepts and today’s topics
    •   What is a Process?
        A running application that consists of a private
        virtual address space, code, data, and other
        operating-system resources, such as files, pipes,
        and synchronization objects that are visible to
        the process. A process also contains one or
        more threads that run in the context of the
        process
W   indows
    PROGRAMMING
                                                     Lecture 24


                  What is a Thread ?
    •   What is a Thread?
        A thread is basically a path of execution through
        a program. It is also the smallest unit of
        execution that Win32 schedules. A thread
        consists of a stack, the state of the CPU
        registers, and an entry in the execution list
        maintained by Windows. Each thread shares all
        of the process’s resources.
                                                  contd…
W   indows
    PROGRAMMING
                                                    Lecture 24


            What is a Thread ? contd…
    •   A Thread and a Process
        A process consists of one or more threads and
        the code, data, and other resources of a program
        in memory. Each thread in a process operates
        independently. Unless you make them visible to
        each other, the threads execute individually and
        are unaware of the other threads in a process.
W   indows
    PROGRAMMING
                                            Lecture 24

            Linking and the Compiled Code
    •   What is compiled .OBJ code?
    •   The purpose of the linker
    •   Static Linking
    •   Dynamic Linking
    •   Dynamic Link Libraries (DLLs)
W   indows
    PROGRAMMING
                                                              Lecture 24

                  Dynamic Link Libraries
    •    Why a DLLs in not an EXE?
    •    Basic structure of a DLL
    •    The DLL entry point function
    •    DllMain() function

    BOOL WINAPI DllMain(
        HINSTANCE hinstDLL, // handle to DLL module
        DWORD fdwReason,            // reason for calling function
        LPVOID lpvReserved          // reserved
    );
W   indows
    PROGRAMMING
                                                             Lecture 24

                  Dynamic Link Libraries
    DLL_PROCESS_ATTACH

    •   Passed to the DLL entry point function when the DLL
        is being loaded into the virtual address space of the
        current process as a result of the process starting up or
        as a result of a call to LoadLibrary()
    DLL_THREAD_ATTACH

    •   The entry-point function of all DLLs currently attached
        to the process are called with this value when the
        current process creates a new thread. Existing threads
        do not call DLL entry point function with this value
    Select proper option in Visual C++ or your compiler to
      generate a DLL instead of an EXE
W   indows
    PROGRAMMING
                                                  Lecture 24

                  DLL exports and imports
    •   The export table
    •   How to export and import code (functions) in a
        DLLs
    •   Import data
    •   __declspec( dllimport ) int i;

    •   Export code
    •   __declspec( dllexport ) void function(void);
W   indows
    PROGRAMMING
                                                     Lecture 24

             Calling Conventions and DLLs
    •   Significance of Calling Conventions of the caller
        and the called function in a DLL
    •   C++ uses same calling convention / parameter
        passing as C, but performs name decoration
    •   extern “C” { … … function declarations … }
         prevents C++ name-decoration
W   indows
    PROGRAMMING
                                                     Lecture 24

                  Variable scope in DLLs
    •   Static variables have scope limited to the block
        in which they are declared. As a result, each
        process has its own instance of the DLL global
        and static variables by default.
W   indows
    PROGRAMMING
                                                            Lecture 24

    Load-time vs. Runtime Dynamic Linking
    •   Load-time Dynamic Linking: .LIB file contains all
        exported function addresses
    •   Runtime Dynamic Linking:      LoadLibrary()
    •   .DEF module definition files can be used instead of
        dllexport/dllimport.

    •   Using .LIB is safer sometimes in the sense that the
        programme stub refuses to load the main programme if
        some DLL can not be loaded.
    •   .DEF files are less common now, but are more
        powerful.
W   indows
    PROGRAMMING
                                                Lecture 24

         Loading a DLL and calling functions in it

    HMODULE LoadLibrary(
       LPCTSTR lpFileName // file name of module
    );


    FreeLibrary(hModule);


    FARPROC GetProcAddress(
       HMODULE hModule,     // handle to DLL module
       LPCSTR lpProcName    // function name
    );
W   indows
    PROGRAMMING
                                                                    Lecture 24

                                Example
    Create a DLL myDll.dll and its import library myDll.lib
    •   declspec( dllexport ) int sum(int, int);


    •   int sum(int a, int b)
        {
            return a+b;
        }


    Using myDll.DLL in your programme
    •   Specify myDll.lib at link-time in the list of import libraries
    •   Provide a prototype of int sum(int, int) in your
        programme
    •   Use the function as normal
W   indows
    PROGRAMMING
                                                     Lecture 24

                          Example

    Using myDll.DLL in your programme
    •   Call LoadLibrary() at runtime to load the
        DLL
    •   Call GetProcAddress() to get a pointer to the
        function sum() in your programme
    •   Use indirection to this pointer to function as
        normal to call the function.
W   indows
    PROGRAMMING
                                                   Lecture 24

                  The Import Libraries .LIB

    •   Concept of an import library .LIB
    •   Import Library is statically linked
    •   How to create an import library?
    •   Important System DLLs: Kernel32.dll,
        User32.dll, Gdi32.dll
    •   Import Libraries of System DLLs: Kernel32.lib,
        User32.lib, Gdi32.lib
W   indows
    PROGRAMMING
                                                              Lecture 24

                        Resource-only DLLs
    •    Including resources in a DLL
    •    LoadBitmap(), LoadString(), LoadMenu(), LoadIcon()

    HRSRC FindResource(
        HMODULE hModule, // module handle
        LPCTSTR lpName,       // resource name
        LPCTSTR lpType        // resource type
    );
         lpType can be RT_ICON, RT_MENU, RT_STRING etc.
    •    Use of resource-only DLLs for internationalisation
W   indows
    PROGRAMMING
                   DLL versions
                                    Lecture 24



    •   Why versioning?
         The comctl32.dll example
    •   The VERSIONINFO resource
        statement
W   indows
    PROGRAMMING
                  VERSIONINFO resource statement
                                                                            Lecture 24

    VS_VERSION_INFO VERSIONINFO
     FILEVERSION 1,0,0,1
     PRODUCTVERSION 1,0,0,1
     FILEFLAGSMASK 0x3fL
     FILEFLAGS 0x1L
     FILEOS 0x40004L
     FILETYPE 0x1L
     FILESUBTYPE 0x0L
    BEGIN
          BLOCK "StringFileInfo"
          BEGIN
                BLOCK "040904b0"
                BEGIN
                         VALUE "CompanyName", "Virtual University\0"
                VALUE "LegalCopyright", "Copyright © 2002 Virtual University\0"
            END
            ... ... ... ...
          END
    END
W   indows
    PROGRAMMING
                                                      Lecture 24

                         DLL versions
    BOOL GetFileVersionInfo(
        LPTSTR lptstrFilename,       // file name
        DWORD dwHandle,              // ignored
        DWORD dwLen,                 // size of buffer
        LPVOID lpData                // version
        information buffer
    );

    •   HRESULT CALLBACK DllGetVersion(
                                    DLLVERSIONINFO *pdvi);

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:1/16/2011
language:English
pages:21
Description: Virtual University of pakistan complete handouts of CS410.