Article ID: 125710 - View products that this article applies to.
This article was previously published under Q125710
Windows NT, Windows 95, Windows 98, and Windows 2000 do not allow direct mixing of 16-bit code and 32-bit code in the same process. All these platforms support some sort of an IPC mechanism, such as DDE, RPC, OLE, named pipes, and WM_COPYDATA, that you can use for communication between 16-bit code and 32-bit code. However, there are occasions when it is necessary to call a function in 32-bit code from 16-bit code or vice-versa.
Thunks allow code on one side of the 16-32 process boundary to call code on the other side of the boundary. Each Win32 platform employs one or more thunking mechanisms. The following table summarizes the thunking mechanisms provided by the different Win32 platforms:
Generic thunks allow a 16-bit Windows-based application to load and call a Win32-based DLL.
Windows 95 and Windows 98 also support a second method called flat thunks, which allows a Win32 application to load and call a 16-bit DLL, or a 16-bit application to load and call a Win32 DLL.
This article describes the types of thunking mechanisms available on each Win32 platform.
Windows NT and Windows 2000Windows NT and Windows 2000 support generic thunks, which allow 16-bit code to call 32-bit code. Generic thunks must be initiated from a 16-bit Windows-based application. Once the thunk is established, the 32-bit code can make a callback to the 16-bit code using WOWCallback16(). The generic thunk is implemented by using a set of API functions that are exported by the WOWKERNEL and Wow32.dll.
In Windows NT, 16-bit Windows-based applications are executed in a VDM (virtual DOS machine) running the WOW (Windows on Win32) subsystem. The WOW subsystem is a Win32 process. Each 16-bit application runs as a thread in a VDM's WOW process.
Using generic thunks is like explicitly loading a DLL. The five major APIs used in generic thunking are: LoadLibraryEx32W(), FreeLibrary32W(), GetProcAddress32W(), CallProc32W(), or CallProcEx32W(). Their functionality is very similar to LoadLibraryEx(), FreeLibrary(), GetProcAddress(), and calling the function through a function pointer. The Win32 DLL called by the thunk is loaded into the VDM address space. The following is an example of thunking a call to GetVersionEx() via a generic thunk:
Windows 95 and Windows 98Windows 95 and Windows 98 allow 16-bit code to call 32-bit code and vice- versa using a mechanism called flat thunks. Flat thunks use a thunk compiler and updated 16-bit resource compiler that are included with the Platform SDK. Because the output of the thunk compiler is an assembly- language file, a copy of Microsoft Macro Assembler (MASM) 6.1 or later is also needed.
To use the thunk compiler, you need to create a thunk script, which is the function prototype with additional information about input and output variables. The thunk compiler produces a single assembly language file. This assembly language file must be assembled twice, each time using one of two different flags, -DIS_32 and -DIS_16, to produce 16-bit and 32-bit object files. These object modules must be liked to their respective 16-bit and 32-bit DLLs. There are no special APIs used, all you have to do is call the thunked function on one side of the thunk, and implement it on the other side.
NOTE: These thunks are not portable to other platforms.
In addition to flat thunks, Windows 95 and Windows 98 support the generic thunk mechanism. Generic thunks are recommended for portability between Windows 95/Windows 98 and Windows NT/Windows 2000.
For more information on Generic Thunks, see the "Generic Thunks" chapter in the Platform SDK online documentation.
For more information about flat thunks and the thunk compiler for Windows 95 and Windows 98, refer to the "Thunk Compiler" chapter in the Platform SDK online documentation.
In addition, you can query the Microsoft Knowledge Base with the appropriate keywords on each thunk mechanism to find further information.
Article ID: 125710 - Last Review: March 1, 2005 - Revision: 3.1