Article ID: 154093 - Last Review: November 21, 2006 - Revision: 3.3 How To Call 32-bit Code from 16-bit Code Under Windows 95, Windows 98, or Windows Millennium EditionThis article was previously published under Q154093 On This PageSUMMARY
It 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 Microsoft Windows 95, Windows 98, or Windows Millennium Edition (Me) is
called a flat thunk.
The three major steps in writing the thunk code are:
MORE INFORMATION
A 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, Windows 98, or Windows Me 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: C language: void WINAPI MyThunk32(void); C++ Language: extern "C" void WINAPI MyThunk32(); The following thunk script uses more complex parameter types such as structures. This example also shows how to specify input and output parameters: 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.asm
The "-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
REFERENCES
For information about how to debug flat thunks, please refer to the
following article in the Microsoft Knowledge Base:
133722
(http://support.microsoft.com/kb/133722/EN-US/
)
How to Debug Flat Thunks
APPLIES TO
| Article Translations
|

Back to the top
