The information in this article applies to:
SUMMARY
Neither Windows NT nor Windows 95 allow direct mixing of 16-bit code and 32-
bit code in the same process. Both platforms support IPC mechanisms, such
as DDE, RPC, OLE, named pipes, and WM_COPYDATA, which 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 a Win32-based DLL
(including functions in the system DLLs) from a 16-bit application under
WOW.
Q125710 PRB: Types of Thunking Available in Win32 PlatformsThe Generic Thunking interface consists of functions that allow a 16-bit application to load the Win32 DLL, get the address of an exported DLL routine, call the routine (passing it up to thirty-two 32-bit arguments), convert 16:16 (WOW) addresses to 0:32 addresses (useful if you need to build up a 32-bit structure that contains pointers and pass a pointer to it), call back into the 16-bit application from the Win32 DLL, and free the Win32 DLL. Generic Thunks are documented in the Win32 SDK documentation, under "Programming and Tools Guides/Programming Techniques." NOTE: It is a good idea to test the Win32 DLL by calling it from a Win32- based application before attempting to call it from a 16-bit Windows-based application, because the debugging support is superior in the 32-bit environment. MORE INFORMATION
The basic steps for calling a function through generic thunking are:
The problem with CallProc32W() is that you cannot create a prototype for it unless you restrict each file so that it only uses calls to functions that contain the same number of parameters. This is a limitation of the Pascal calling convention. Windows NT 3.5 and later supports CallProcEx32W(), which uses the C calling convention to support variable arguments. However, under Windows 95 there are certain caveats in using the CallProc32W() and CallProcEx32W() functions. For more information, see the documentation for these functions. Sample CodeThe following code fragments can be used as a basis for Generic Thunks.Assume that the 16-bit Windows-based application is named app16, the Win32 DLL is named dll32, and the following are declared:
The DLL routine is defined in dll32.c as follows:
Attempt to load the library in the app16 WinMain():
Attempt to get the address of MyPrint():
Although some of the Generic Thunking functions are called in 16-bit code,
they need to be provided with 32-bit handles, and they return 32-bit
handles. Therefore, before calling CallProcEx32W() and passing it a handle,
you must convert the window handle, hWnd, to a 32-bit window handle,
hWnd32:
Call MyPrint() and pass it TestString and hWnd32 as arguments:
Alternatively, you can use CallProc32W() as follows:
A mask of 2 (0x10) is given because we want to pass TestString by reference
(WOW translates the pointer) and we want to pass the handle by value.
Free the library right before exiting WinMain():
NOTE: When linking the Windows-based application, you need to put the
following statements in the .DEF file, indicating that the functions will
be imported from the WOW kernel:
The complete sample is available for download from the Microsoft
Download Center:
GThunks.exeFor more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address http://www.microsoft.com/downloads/search.aspand then click How to use the Microsoft Download Center. FaultsOn MIPS systems, an alignment fault will occur when a Win32-based application de-references a pointer to unaligned data that was passed by a 16- bit Windows application. As a workaround, declare the parameter with the UNALIGNED keyword. For example,
becomes
An application can use SetErrorMode() to specify SEM_NOALIGMENTFAULTEXCEPT
flag. If this is done, the system will automatically fix up alignment
faults and make them invisible to the application.
The default value of this error mode is OFF for MIPS, and ON for ALPHA. So on MIPS platforms, an application MUST call SetErrorMode() and specify SEM_NOALIGMENTFAULTEXCEPT if it wants the system to automatically fix alignment faults. This call does not have to be made on ALPHA platforms. This flag has no effect on x86 systems. Note that the fix above is preferable. Additional query words: kbKernBase kbThunks kbSDKWin32 kbDSupport
Keywords : kbcode kbfile kbsample kbKernBase kbWinOS2000 kbThunks kbDSupport kbGrpKernBase |
Last Reviewed: December 30, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |