Docsity

Threads and DLLs - Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

Partial preview of the text

Download Threads and DLLs - Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

  • Chapter
  • 25.1 IMPORT LIBRARIES ( LIB)
  • 25.2 CALLING CONVENTIONS
  • 25.3 VARIABLE S COPE IN DLL
  • 25.4 RESOURCE ONLY DLL
  • 25.5 DLL VERSIONS
  • 25.6 GET F ILE VERSION INFO
  • 25.7 THREADS
  • 25.7.1 THREADS AND M ESSAGE QUEUING
  • 25.7.2 CREATING S ECONDARY THREAD
  • 25.7.3 THREAD ADVANTAGES
  • 25.7.4 THREAD DISADVANTAGES
  • S UMMARY
  • EXERCISES

25.1 Import Libraries ( .lib)

Import Library is statically linked to Executable module.

Example of Import libraries in windows are:

  • Kernel32.lib
  • User32.lib
  • Gdi32.lib

Important System DLLs are

  • Kernel32.dll
  • User32.dll
  • Gdi32.dll

25.2 Calling Conventions

Functions used in DLL’s are normally use __stdcall calling convention. __stdcall calling convention is a standard calling convention used by the APIs in Windows. This calling convention cleans the stack after returning the called procedure automatically. No extra code is needed to clean out stack. __stdcall calling convention pushes the arguments in stack from right to left order.

25.3 Variable Scope in DLL

Variables defined in DLL have scope in memory until the DLL is loaded. After unloading, the variable scope is vanished. Locally defined variables are accessed within the DLL only. The variables that are set to export variables can be accessed outside the DLL if the DLL is statically linked.

Variables can be shared across multiple processes by making the separate data section as following.

#pragma data_seg( [ [ { push | pop } , ] [ identifier , ] ] [ "segment- name " [, " segment-class " ] )

Specifies the data segment where initialized variables are stored in the .obj file. OBJ files can be viewed with the dumpbin application. The default segment in the .obj file for initialized variables is .data. Variables initialized to zero are considered uninitialized and are stored in .bss.

Do not use the following names, as they will conflict with standard names. For example, .sdata is used on RISC platforms:

  • .arch
  • .bss
  • .data
  • .edata
  • .idata
  • .pdata
  • .rdata
  • .reloc
  • .rsrc
  • .sbss
  • .sdata
  • .srdata
  • .text
  • .xdata

Specify one or more attributes for the section. The attribute characters, listed below, are not case sensitive. You must specify all attributes that you want the section to have; an omitted attribute character causes that attribute bit to be turned off. If you do not specify R, W, or E, the existing read, write, or executable status remains unchanged.

The meanings of the attribute characters are shown below.

Character Attribute Meaning E Execute The section is executable R Read Allows read operations on data W Write Allows write operations on data

S Shared Shares the section among all processes that load the image D Discardable Marks the section as discardable K Cacheable Marks the section as not cacheable

L Preload VxD only; marks the section as preload P Pageable Marks the section as not pageable X Memory-resident VxD only; marks the section as memory-resident

K and P are peculiar in that the section flags that correspond to them are in the negative sense. If you specify one of them on the .text section (/SECTION:.text,K), there will be no difference in the section flags when you run DUMPBIN with the /HEADERS option; it was already implicitly cached. To remove the default, specify /SECTION:.text,!K and DUMPBIN will reveal section characteristics, including "Not Cached."

A section in the PE file that does not have E, R, or W set is probably invalid.

To set this linker option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box.
  2. Click the Linker folder.
  3. Click the Command Line property page.
  4. Type the option into the Additional Options box.

25.4 Resource Only DLL

Resource Only DLL contains only resource of different language and local types. Resource only DLLs do not contain Entry Point or any DllMain Function.

Use of resource-only DLL is for internationalization.

25.5 DLL Versions

Version information makes it easier for applications to install files properly and enables setup programs to analyze files currently installed. The version-information resource contains the file's version number, intended operating system, and original file name.

You can use the version information functions to determine where a file should be installed and identify conflicts with currently installed files. These functions enable you to avoid the following problems:

  • installing older versions of components over newer versions
  • changing the language in a mixed-language system without notification
  • installing multiple copies of a library in different directories
  • copying files to network directories shared by multiple users

The version information functions enable applications to query a version resource for file information and present the information in a clear format. This information includes the file's purpose, author, version number, and so on.

You can add version information to any files that can have Microsoft® Windows® resources, such as dynamic-link libraries (DLLs), executable files, or font files. To add the information, create a VERSIONINFO Resource and use the resource compiler to compile the resource.

25.6 Get File Version Info

The GetFileVersionInfo function retrieves version information for the specified file.

BOOL GetFileVersionInfo(

Worker threads are initially without message queue.

User Interface threads are those threads which are attached any GUI component such as window.

When a process start at least one thread is running that first thread is called primary thread other threads can made, these threads will, then, be called secondary threads.

25.7.2 Creating Secondary Thread

For creating thread we can use following functions:

_beginthread() and _endthread() This function is a ‘C’ runtime concept from UNIX system These functions no longer have place in Win32 systems.

The CreateThread API

In windows systems CreateThread API is used to create a thread in a process. Every thread has its own thread procedure.

  • Threads can be stopped and exited using ExitThread API call.
  • Thread enters into running state after creating it. For thread not to be run automatically gives the CREATE_SUSPENDED flag in CreateThread API.
  • Threads can be suspended or resumes after their creations by:
  • SuspendThread and ResumeThread.

25.7.3 Thread Advantages

Using threads has the following advantages:

  1. Threads can be used to start another activity parallel. E.g. saving file on disk, automatically while you are typing.
  2. Perform different calculations parallel.

25.7.4 Thread Disadvantages

Threads major disadvantage is that they make the system slow because thread uses the time sharing concept that is another name multitasking. A multitasking operating system divides the available processor time among the processes or threads that need it. The system is designed for preemptive multitasking; it allocates a processor time slice to each thread it executes. The currently executing thread is suspended when its time slice

elapses, allowing another thread to run. When the system switches from one thread to another, it saves the context of the preempted thread and restores the saved context of the next thread in the queue.

The length of the time slice depends on the operating system and the processor. Because each time slice is small (approximately 20 milliseconds), multiple threads appear to be executing at the same time. This is actually the case on multiprocessor systems, where the executable threads are distributed among the available processors.

Note: You must use caution when using multiple threads in an application, because system performance can decrease if there are too many threads.

Summary

Multitasking Operating systems are useful to run applications simultaneously. Threads and processes are the key features of Operating systems. In this lecture we studied about variable sharing in DLLs, variable scope in DLLs, DLL Versioning, Resource only DLLs, Threads and their advantages and disadvantages. Many Threads can work better than using single thread sometime.

Exercises

  1. Create a dynamic link library and make a function which displays only message box. Export the functions using __dllexport.
  2. Call the function from above library in your executable module. The linking must be static linking and use __dllimport.