Microsoft small business knowledge base

Article ID: 155763 - Last Review: July 11, 2005 - Revision: 2.3

This article was previously published under Q155763

On This Page


As a developer, you may need to access the functionality provided by a 16- bit dynamic-link library (DLL) from your Win32 application. This is true particularly when you do not have the source code for the DLL so that you can port it to Win32. This article discusses the mechanism by which 32-bit DLLs can call 16-bit DLLs. The mechanism is called a thunk and the method implemented under Microsoft Windows 95, Windows 98, and Windows Millennium Edition is called a flat thunk.

The following describes the three major steps involved in creating a flat thunk:
  1. Create the thunk script.
  2. Build the 32-bit DLL.
  3. Build the 16-bit DLL.


A flat thunk consists of a 32-bit and a 16-bit DLL that work together. A Win32 application calls the 32-bit DLL, and the 32-bit DLL calls an exported function in the 16-bit DLL. When the function in the 16-bit DLL returns, it returns back to the 32-bit DLL, which in turn returns back to the Win32 application. The 32-bit and 16-bit DLLs work by calling the Windows 95 32-bit and 16-bit kernels to handle all of the low-level details necessary to make the transition from 32-bit to 16-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 then assembled twice; one time with each of two flags of -DIS_32 and -DIS_16. This allows you to create both the 32-bit and 16-bit object modules. These object modules are linked in the 32-bit and 16-bit DLLs, respectively. The following diagram summarizes the files involved in building the DLLs:
                         | 32to16.thk |
                         | 32to16.asm |
                           /         \ 
                  -DIS_32 /           \ -DIS_16
                        /              \ 
                  +-----------+  +-----------+
                  | 32THK.obj |  | 16THK.obj |
                  +-----------+  +-----------+
                        /                 \ 
        +-------+    +-------+             +-------+
        | APP32 | -> | DLL32 | -- THUNK -- | DLL16 |
        +-------+    +-------+             +-------+

Tools Needed to Build Flat Thunks

  • Microsoft Visual C++ version 1.5x (16-bit) compiler for the 16-bit side of creating the thunk. The 16-bit side of the thunk is a 16-bit DLL.
  • Microsoft Visual C++ version 2.x or higher (32-bit) compiler for creating the 32-bit side of the thunk. The 32-bit side of the thunk is a 32-bit DLL.
  • Thunk compiler (Thunk.exe) from the Microsoft Win32 SDK for compiling thunk scripts.
  • Microsoft Macro Assembler (MASM) version 6.1 or higher for assembling the assembly-language output of the thunk compiler.
  • 16-bit Rc.exe file from the BINW16 directory of the Microsoft Win32 SDK for marking the 16-bit thunk DLL as version 4.0.

Creating the Thunk Script

You need to create a script that can 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 32->16 thunks begins with the following statement:
enablemapdirect3216 = true;
The Thunk compiler expects that the 32-bit side of the thunk is declared as __stdcall, and that the 16-bit side is __far __pascal. (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 example shows a thunk script for a function that has no parameters:
   enablemapdirect3216 = true;

   void MyThunk16()
The equivalent declaration would be:
   C   language:  void WINAPI MyThunk16(void);
   C++ language:  extern "C" void WINAPI MyThunk16();
The following example script describes a function that takes two parameters and returns a value. The second parameter is an output parameter that contains a pointer that is passed back to the 32-bit DLL.
   enablemapdirect3216 = true;

   typedef int   BOOL;
   typedef char *LPSTR;

   BOOL MyThunk16(LPSTR lpstrInput, LPSTR lpstrOutput)
      lpstrInput  = input;    // optional; input is default
      lpstrOutput = output;
The statement "lpstrOutput = output" tells the Thunk compiler that the 16-bit function returns an address that needs to be converted from a selector:offset pointer into a 32-bit linear address.

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;

   typedef struct _CIRCLE {
      POINT center;
      UINT  radius;

   void MyThunk32( LPCIRCLE lpCircleInOut)
      lpCircleInOut = inout;
The statement "lpCircleInOut = inout" tells the script compiler that this pointer is going to be used for input and output. This causes the Thunk compiler to convert lpCircleInOut from a 32-bit linear address to a selector:offset pointer when the function is called and then back to a 32-bit linear address when the function returns. The conversion is handled by the thunk created by the Thunk compiler.

Using the Thunk Compiler

The Thunk compiler usage is as follows:
thunk.exe /options <inputfile> -o <outputfile>
The following command line shows how to compile a 32->16 thunk script. This line takes a thunk script named 32to16.thk and produces an assembly-language file named 32to16.asm.
thunk -t thk 32to16.thk -o 32to16.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 32->16 and 16->32 thunks. Each thunk script should have a unique prefix.

Building the 32-bit DLL

  1. In the DllMain function of your 32-bit DLL, you must make a call to a function created by the Thunk compiler named thk_ThunkConnect32 for every reason (dwReason) DllMain is called, as shown here ("thk" is the prefix from the Thunk compiler -t switch):
          // prototype for function in .obj file from the thunk script
          BOOL WINAPI thk_ThunkConnect32(LPSTR     lpDll16,
                                         LPSTR     lpDll32,
                                         HINSTANCE hDllInst,
                                         DWORD     dwReason);
                              DWORD     dwReason,
                              LPVOID    lpvReserved)
             if (!thk_ThunkConnect32("DLL16.DLL", "DLL32.DLL",
                                     hDLLInst, dwReason))
                return FALSE;
             switch (dwReason)
                case DLL_PROCESS_ATTACH:
                case DLL_PROCESS_DETACH:
                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
             return TRUE;
  2. Include the following lines in the EXPORTS section of the module definition (.def ) file for the 32-bit DLL. For example:
  3. Export the functions that the Win32 application calls. You can either use the 32-bit DLL's module definition (.def) file or the __declspec(dllexport) keyword. Be sure the functions are declared and defined as __stdcall (or WINAPI). If the 32-bit DLL is written in C++, be sure to declare the functions as extern "C" as well.
  4. Compile the thunk script as follows (if not already compiled):
          thunk -t thk 32to16.thk -o 32to16.asm
  5. Assemble the assembly-language file produced by the Thunk compiler as a 32-bit object module. For example:
          ml /DIS_32 /c /W3 /nologo /coff /Fo thk32.obj 32to16.asm
  6. Link this object module as part of the 32-bit DLL.
  7. Link the Thunk32.lib library as part of the 32-bit DLL. This is the 32-bit import library provided in the Win32 SDK that contains references to the 32-bit thunking APIs that the code created by the Thunk compiler uses.

Building the 16-bit DLL

  1. The 16-bit DLL must export a function named "DllEntryPoint." This function must make a call to a function created by the Thunk compiler named thk__ThunkConnect16 ("thk" is the prefix from the Thunk compiler -t switch) every time DllEntryPoint is called:
          // prototype for function in .obj file from the thunk script
          BOOL WINAPI __export thk_ThunkConnect16(LPSTR lpDll16,
                                                  LPSTR lpDll32,
                                                  WORD  hInst,
                                                  DWORD dwReason);
          BOOL WINAPI __export DllEntryPoint(DWORD dwReason,
                                             WORD  hInst,
                                             WORD  wDS,
                                             WORD  wHeapSize,
                                             DWORD dwReserved1,
                                             WORD  wReserved 2)
             if (!thk_ThunkConnect16("DLL16.DLL",
                return FALSE;
             return TRUE;
  2. Include the following lines in the IMPORTS section of the module definition (.def) file for the 16-bit DLL. For example:
          C16ThkSL01      = KERNEL.631
          ThunkConnect16  = KERNEL.651
  3. Include the following lines in the EXPORTS section of the module definition (.def) file for the 16-bit DLL. THK_THUNKDATA16 is defined in the object file that is assembled from the output of the Thunk compiler. Both of these symbols must have the RESIDENTNAME keyword, but can have any ordinal number.
          DllEntryPoint   @2  RESIDENTNAME
  4. Add the thunk functions to the EXPORTS statement of the 16-bit DLL's module definition (.def) file. Be sure they are declared and defined as __far __pascal __export (or WINAPI __export). If the DLL is written in C++, be sure to declare them as extern "C" as well. The 32-bit side of the thunk calls these functions.
  5. Compile the thunk script as follows (if not already compiled):
          thunk -t thk 32to16.thk -o 32to16.asm
  6. Assemble the assembly-language file produced by the Thunk compiler as a 16-bit object module. For example:
          ml /DIS_16 /c /W3 /nologo /Fo thk16.obj 32to16.asm
  7. Link this object module as part of the 16-bit DLL.
  8. Mark the 16-bit DLL as version 4.0. To do this, use the resource compiler (Rc.exe). The following line shows the syntax:
    rc -40 <DLL file>
    This -40 option is available in the Resource Compiler that is provided with the Win32 SDK.

    NOTE: Be sure to use the Rc.exe file in the BINW16 directory so that the DLL is marked with version 4.0. The Rc.exe file that ships with 16-bit versions of Microsoft Visual C++ does not mark the DLL as version 4.0.


For information about how to debug flat thunks, please refer to the following article in the Microsoft Knowledge Base:
133722  ( ) How To Debug Flat Thunks

  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows 95
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows Millennium Edition
    • Microsoft Platform Software Development Kit-January 2000 Edition
kbhowto kbtshoot kbapi kbnetwork kbprogramming kbkernbase kbthunks KB155763
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support