The information in this article applies to:
SUMMARYThe following is the text of the article "Modules, Instances, and Tasks". This article is available in Windows Help file format in the Microsoft Software Library in addition to the text presented below. MORE INFORMATIONThe following files are available for download from the Microsoft
Download Center. Click the file names below to download the files: http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. This article discusses modules, instances, and tasks. It covers the following topics:
OVERVIEWThe Microsoft Windows graphical environment defines two types of executable files: applications and dynamic-link libraries (DLLs). The code in both types is normally sharable. Multiple instances of an application or multiple users of a DLL can share code loaded by the first instance. Most Windows-based applications and DLLs also contain resources that are sharable in the same manner. When Windows loads an application or a DLL into memory for the first time, it assigns a unique identifier, called a module handle, or an hModule, to the shared code and resources.When Windows loads an application, it creates a unique execution context for the application to distinguish it from other running copies of the same application. This execution context is given an identifier called an instance handle, or an hInstance. Part of the execution context for an application is its automatic data segment, where the application's static data, stack, and local heap reside. Two running copies of an application share code, but each has its own data segment. DLLs differ from applications in that all instances of a DLL share the same execution context, and therefore share a common data segment. Part of the execution context of an application is defined by a set of CPU registers and a stack. This portion of the execution context (a task context) is given a special identifier called a task handle, or an hTask. Every loaded application has its own task handle, but task handles are never associated with DLLs. Therefore, the automatic data segment of a DLL never contains a stack. Windows can easily obtain a module handle from an instance handle because an execution context is always associated with only one module. Because a module can have more than one running instance, however, obtaining an instance handle given only the module handle is impossible. DEALING WITH HMODULE AND HINSTANCEModule and instance handles are used inconsistently in the Microsoft Windows Software Development Kit (SDK) documentation. For example, LoadLibrary is documented to return an hInstance, but FreeLibrary takes an hModule. How can you free a library if Windows provides no obvious way to convert an hInstance to an hModule?A number of the Windows-based application programming interfaces (APIs) that are documented as accepting only a module handle are defined incorrectly: Most can take an instance handle or a module handle. FreeLibrary, for example, can accept either a module handle or an instance handle. The following APIs are documented incorrectly; the following short descriptions of what the function does are correct:
At this point you may be asking, "If an instance handle can be used in
place of a module handle, why have module handles at all? Why not just
have instance handles and be done with it?" Good question. The answer
is that it really doesn't matter. You can just write applications
using instance handles rather than module handles for the functions
above. This is good news because instance handles are much easier to
obtain than module handles. For example, calling GetWindowWord with
GWW_HINSTANCE obtains the instance handle of the application or the
DLL that created a window. This instance handle can then be used to
determine the module filename of the application that created the
window. No equivalent way exists to do this using module handles.
Actually, module handles have a real use. Internally, Windows uses module handles to tag certain resources, such as window classes and hooks, that are associated with a module and not with a particular instance of a module. These resources are not freed until the last instance of the module is freed. Although undocumented in Windows version 3.0, obtaining a module handle from an instance handle is possible. Calling the GetModuleHandle function with the low word of the lpModuleName parameter set to an hInstance and the high word of the same parameter set to NULL returns the module handle of the specified hInstance. The version 3.1 WINDOWS.H file contains a macro, GetInstanceModule, that does this for you. INSTANCE HANDLE API FUNCTIONSThe following Windows functions deal with instance handles:
EXPORTED FUNCTIONSExecution usually enters a module's code through exported entry points. If an application has multiple instances, the exported entry points must be able to set the proper data segment upon entry. This applies to all functions that are the target of a call from another module, such as functions in an application that Windows calls (dialog procedures, callback functions, and so on). But exporting alone is not enough to set the proper data segment. For each instance of the application, these functions need a unique entry point, which can set the proper data segment and then jump to the "real" function. The MakeProcInstance function is used for this purpose. It takes a pointer to an exported function and an instance handle and creates a call thunk. A call thunk is merely a piece of code that moves the specific instance's data segment value into the AX register and then jumps to the real function. Exported functions expect the appropriate data segment value to be passed in the AX register, and move the value to the DS register in the function prolog. The application uses the pointer to the call thunk returned by MakeProcInstance in place of the pointer to the real function in calls such as DialogBox and EnumWindows.Window procedures do not need call thunks. When an application creates a window, it supplies an hInstance value on the CreateWindow call. This value is saved internally, and Windows uses it to set the proper AX value before dispatching messages to a window procedure. Dialog procedures, on the other hand, are not real window procedures and do need call thunks. Exported functions that reside in a DLL also don't require call thunks. Because all users of a DLL share the same instance, creating call thunks for them is not necessary. TASKSA task can be thought of as a "logical CPU" with its own CS:IP, stack, and registers. A task can execute code from any number of modules. As execution enters a module (usually through an exported entry point), the current task handle remains unchanged, but the function's prolog code sets the proper execution context, that is, the proper data segment.Task switching occurs when an application calls a Windows function that gives up control of the system, a procedure known as yielding. Because Windows is a nonpreemptive multitasking system, applications must periodically give up control of the system to allow other applications to run. The following functions can cause the calling task to yield:
Because DLLs are not instanced under Windows (that is, all instances share the same DS), creating a server-type DLL that can provide services to multiple simultaneous clients is not straightforward. The problem is knowing which client is requesting service at any point. Some DLLs call the GetCurrentTask function to retrieve the current task's hTask and then tag a data structure or some other data item with this task handle. Then, each time a request for service is made, the DLL checks its list of known client task handles with the current task. This solution has only one flaw. Task handles are actually global handles to an internal data structure that describes the task. Global handles can be reused. If one task terminates and another starts, the new task can have the same task handle as the task just terminated. This is not a problem to a server DLL as long as the DLL knows that the original task terminated. Even if a client application is written to disconnect from a server DLL before terminating, an abnormal termination of the application can still cause the problem just described. In this case, the server DLL doesn't know the task terminated. Rather than hacking around this problem, DLLs in this situation can easily use the Toolhelp notification services to detect when a particular task terminates. Copyright 1992 by Microsoft Corporation. All rights reserved. Additional query words: 0 softlib MTI.EXE
Keywords : kbfile kbsample kb16bitonly kbWinOS310 kbWinOS300 KrTsksIns |
Last Reviewed: December 8, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |