How To Call 32-bit Code from 16-bit Code in Windows 95Last reviewed: January 24, 1997Article ID: Q154093 |
The information in this article applies to:
SUMMARYIt is often desirable to port Win16 applications and DLLs to Win32 a little at a time rather than all at once. For example, you may want to port 16-bit DLLs to Win32 but still be able to call them from 16-bit code. This article discusses the mechanism by which 16-bit DLLs can call 32-bit DLLs. The mechanism is called a thunk and the method implemented under Windows 95 is called a flat thunk. The three major steps in writing the thunk code are:
MORE INFORMATIONA flat thunk consists of a 16-bit and a 32-bit DLL that work together. A Win16 application calls the 16-bit DLL and the 16-bit DLL calls an exported function in the 32-bit DLL. When the function in the 32-bit DLL returns, it returns back to the 16-bit DLL, which in turn returns back to the Win16 application. The 16-bit and 32-bit DLLs work by calling the Windows 95 16- bit and 32-bit kernels to handle all of the low-level details necessary to make the transition from 16-bit to 32-bit code and back. Designing a new flat thunk involves creating a thunk script (.THK file). This script is compiled with the thunk compiler into an assembly-language file which is assembled twice; one time with each of two flags: -DIS_16 and -DIS_32. The result is a 16-bit and a 32-bit object module. These object modules are linked into the 16-bit and 32-bit DLLs respectively. The following diagram summarizes the files involved in building the DLLs:
+------------+ | 16to32.THK | +------------+ | +------------+ | 16to32.ASM | +------------+ / \ -DIS_16 / \ -DIS_32 / \ +-----------+ +-----------+ | 16THK.OBJ | | 32THK.OBJ | +-----------+ +-----------+ / \ +-------+ +-------+ +-------+ | APP16 | -> | DLL16 | -- THUNK -- | DLL32 | +-------+ +-------+ +-------+ Tools Needed to Build Flat Thunks
Creating the Thunk ScriptYou need to create a script that will be used by the thunk compiler to create a thunk. A thunk script is a text file that contains type definitions, the function prototypes of the functions you wish to call via thunks and a specification of the direction of the parameters for each function. For example, some functions require both input and output parameters while others may only require input parameters. Thunk scripts use special syntax to describe whether parameters are input, output, or both input and output. A thunk script for 16->32 thunks begins with the following statement:
enablemapdirect1632 = true;By default, the 32-bit DLL is loaded only when a thunk to it is executed for the first time. Because this late binding is used, 16-bit code must not depend on any action taken by the initialization of the 32-bit DLL. Because the 32-bit DLL will load only when the first thunk to it executes, problems in loading the 32-bit DLL will not be detected when the 16-bit DLL first loads. To disable late binding of the 32-bit DLL, add the following line in your thunk script:
preload32=true;The thunk compiler expects that the 16-bit side of the thunk is declared as __far __pascal, and that the 32-bit side is __stdcall. (The WINAPI declaration takes care of this on both sides.) The __cdecl and __fastcall calling conventions are not supported by the thunk compiler. Note, however, that the thunk compiler does not actually accept the __far, __pascal, or __stdcall keywords; they are assumed. The following thunk script describes a function with no parameters:
enablemapdirect1632 = true; void MyThunk32() { }The equivalent declaration would be:
C language: void WINAPI MyThunk32(void); C++ Language: extern "C" void WINAPI MyThunk32();The function in the following example script takes two parameters and returns a value. The second parameter is an output parameter and contains a pointer that is passed back to the 16-bit DLL:
enablemapdirect1632 = true; typedef int BOOL; typedef char *LPSTR; BOOL MyThunk32(LPSTR lpstrInput, LPSTR lpstrOutput) { lpstrInput = input; // optional; input is default lpstrOutput = output; }The statement "lpstrOutput = output" tells the thunk compiler that the 32- bit function will return an address that needs to be converted from a 32- bit linear address to a selector:offset pointer. The following thunk script uses more complex parameter types such as structures. This example also shows how to specify input and output parameters:
enablemapdirect1632 = true; typedef unsigned int UINT; typedef char *LPSTR; typedef struct _POINT { UINT x; UINT y; }POINT, *LPPOINT; typedef struct _CIRCLE { POINT center; UINT radius; }CIRCLE, *LPCIRCLE; void MyThunk32( LPCIRCLE lpCircleInOut) { lpCircleInOut = inout; }The statement "lpCircleInOut = inout" tells the thunk compiler that this pointer is going to be used for input and output. This causes the thunk compiler to convert lpCircleInOut from a 16-bit far (selector:offset) pointer to a 32-bit linear address when the function is called and then back to a 16-bit far pointer when the function returns. The conversion is handled by the thunk created by the thunk compiler.
Using the Thunk CompilerThe thunk compiler usage is as follows:
thunk.exe /options <inputfile> -o <outputfile>The following line shows how to compile a 16->32 thunk script. This line takes a script named 16to32.thk and produces an assembly-language file named 16to32.asm:
thunk -t thk 16to32.thk -o 16to32.asmThe "-t thk" option tells the thunk compiler to prefix the thunk functions in the assembly-language file with "thk_". This prefix is used when linking multiple thunk scripts into a pair of DLLs and is useful for creating a pair of DLLs that contain both 16->32 and 32->16 thunks.
Building the 16-bit DLL
Building the 32-bit DLL
REFERENCESFor information about how to debug flat thunks, please refer to the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q133722 TITLE : How to Debug Flat Thunks |
KBCategory: kbprg kbhowto kbtshoot
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |