Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 25 25. Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management.

Similar presentations


Presentation on theme: "Lecture 25 25. Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management."— Presentation transcript:

1 Lecture 25 25

2 Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management

3 Lecture 25 The Import Libraries.LIB Concept of an import library.LIB Concept of an import library.LIB Import Library is statically linked Import Library is statically linked How to create an import library? How to create an import library? Important System DLLs: Kernel32.dll, User32.dll, Gdi32.dll Important System DLLs: Kernel32.dll, User32.dll, Gdi32.dll Import Libraries of System DLLs: Kernel32.lib, User32.lib, Gdi32.lib Import Libraries of System DLLs: Kernel32.lib, User32.lib, Gdi32.lib

4 Lecture 25 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. 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.

5 Lecture 25 Variable scope in DLLs Sharing variables across multiple processes #pragma data_seg(“OUR_DATA”) and its usage #pragma data_seg(“OUR_DATA”) and its usage Specifying a section in the executable in Visual C++ 6.0 Using linker option: Using linker option: /section: OUR_DATA,rws Using pre-processor directive in source file: Using pre-processor directive in source file: #pragma comment(linker, “/SECTION: OUR_DATA, RWS”) Tip: “Quick View” utility can be used to verify shared section(s)

6 Lecture 25 Calling Conventions and DLLs Significance of Calling Conventions of the caller and the called function in a DLL 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 C++ uses same calling convention / parameter passing as C, but performs name decoration extern “C” { … … function declarations … } prevents C++ name-decoration extern “C” { … … function declarations … } prevents C++ name-decoration

7 Lecture 25 Windows Hooks Win32 Hooks are a way to trap messages before they reach a single or any application Win32 Hooks are a way to trap messages before they reach a single or any application Application level hook Application level hook Hook that affects a single application, i.e. the one that installs it. The hook procedure can be in the same executable as the original application. Hook that affects a single application, i.e. the one that installs it. The hook procedure can be in the same executable as the original application. System level hook System level hook Hook that effects all applications in the system. This must be implemented separately in a DLL. This DLL is then loaded in the memory space of every running process, by the system. Hook that effects all applications in the system. This must be implemented separately in a DLL. This DLL is then loaded in the memory space of every running process, by the system.

8 Lecture 25 Resource-only DLLs Including resources in a DLL Including resources in a DLL LoadBitmap(), LoadString(), LoadMenu(), LoadIcon() LoadBitmap(), LoadString(), LoadMenu(), LoadIcon() HRSRC FindResource( HMODULE hModule, // module handle HMODULE hModule, // module handle LPCTSTR lpName, // resource name LPCTSTR lpName, // resource name LPCTSTR lpType // resource type LPCTSTR lpType // resource type); lpType can be RT_ICON, RT_MENU, RT_STRING etc. Use of resource-only DLLs for internationalisation Use of resource-only DLLs for internationalisation

9 Lecture 25 DLL versions Why versioning? The comctl32.dll example Why versioning? The comctl32.dll example The VERSIONINFO resource statement The VERSIONINFO resource statement

10 Lecture 25 VERSIONINFO resource statement VS_VERSION_INFO VERSIONINFO FILEVERSION 1,0,0,1 FILEVERSION 1,0,0,1 PRODUCTVERSION 1,0,0,1 PRODUCTVERSION 1,0,0,1 FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL FILEFLAGS 0x1L FILEFLAGS 0x1L FILEOS 0x40004L FILEOS 0x40004L FILETYPE 0x1L FILETYPE 0x1L FILESUBTYPE 0x0L FILESUBTYPE 0x0LBEGIN BLOCK "StringFileInfo" BLOCK "StringFileInfo" BEGIN BEGIN BLOCK "040904b0" BLOCK "040904b0" BEGIN BEGIN VALUE "CompanyName", "Virtual University\0" VALUE "CompanyName", "Virtual University\0" VALUE "LegalCopyright", "Copyright © 2002 Virtual University\0" END END........................ END ENDEND

11 Lecture 25 DLL versions BOOL GetFileVersionInfo( LPTSTR lptstrFilename, // file name LPTSTR lptstrFilename, // file name DWORD dwHandle, // ignored DWORD dwHandle, // ignored DWORD dwLen, // size of buffer DWORD dwLen, // size of buffer LPVOID lpData // version information buffer LPVOID lpData // version information buffer); HRESULT CALLBACK DllGetVersion( DLLVERSIONINFO *pdvi); HRESULT CALLBACK DllGetVersion( DLLVERSIONINFO *pdvi);

12 Lecture 25 Thread basics WinMain() is the primary thread. WinMain() is the primary thread. All other threads created in the process are secondary threads All other threads created in the process are secondary threads Usage of threads Usage of threads

13 Lecture 25 Thread basics User Interface (UI) and Worker threads User Interface (UI) and Worker threads User interface threads own one or more Windows and have their own message queue User interface threads own one or more Windows and have their own message queue Worker threads do not own any windows, and may not have a message queue Worker threads do not own any windows, and may not have a message queue

14 Lecture 25 Thread basics Relationship of a Process and threads; a Thread and windows in Windows Relationship of a Process and threads; a Thread and windows in Windows A process may consist of one or more threads A process may consist of one or more threads A thread may own zero or more windows A thread may own zero or more windows

15 Lecture 25 Creating Secondary Threads How to create secondary threads? How to create secondary threads? _beginthread() C runtime _beginthread() C runtime The CreateThread() Win32 API The CreateThread() Win32 API

16 Lecture 25 Creating Secondary Threads unsigned long _beginthread( void( __cdecl *start_address )( void * ), pointer to thread function unsigned stack_size, stack-size for new thread void *arglist ); argument list to be passed to thread

17 Lecture 25 Threads and message queuing The message routing: System message queue > Thread message queue > Window procedure (if a UI thread) The message routing: System message queue > Thread message queue > Window procedure (if a UI thread) Threads with and without a message queue Threads with and without a message queue UI threads always have a message queue UI threads always have a message queue When is the message queue created? When is the message queue created? The system creates a thread-specific message queue only when the thread makes its first call to one of the Win32 User or GDI functions The system creates a thread-specific message queue only when the thread makes its first call to one of the Win32 User or GDI functions

18 Lecture 25 The Thread Procedure Thread procedure can be any function with the following signatures (return value and parameter list): DWORD WINAPI ThreadProc( LPVOID lpParameter LPVOID lpParameter Thread data passed to the thread function );

19 Lecture 25 Creating Secondary Threads HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,// Security Descriptor LPSECURITY_ATTRIBUTES lpThreadAttributes,// Security Descriptor DWORD dwStackSize, // initial stack size DWORD dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier LPDWORD lpThreadId // thread identifier);

20 Lecture 25 Secondary Thread Procedure The thread functions The thread functions A thread terminates when its thread procedure returns A thread terminates when its thread procedure returns Terminating a thread within a thread function: _endthread() and ExitThread() Terminating a thread within a thread function: _endthread() and ExitThread() Difference between return values of C runtime and Win32 thread creation functions Difference between return values of C runtime and Win32 thread creation functions The Thread Handles The Thread Handles

21 Lecture 25 More about Threads Thread handles and thread IDs Thread handles and thread IDs CREATE_SUSPENDED flag CREATE_SUSPENDED flag SuspendThread(), ResumeThread() SuspendThread(), ResumeThread() Sleep() call Sleep() call Advantages of Threads Advantages of Threads


Download ppt "Lecture 25 25. Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management."

Similar presentations


Ads by Google