Solved

Help me,please!(A question about DLL)

Posted on 2000-03-03
10
198 Views
Last Modified: 2013-11-20
How to call the functions of the 16-bit
DLL in the windown95/98?
Thanks a lot!
0
Comment
Question by:gejianye
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:gejianye
ID: 2579734
I have only a DLL file(16-bit),but I must use VC6.0 to load the function in this file.How can I do this?
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2579917
Hi gejianye,

This is from MSDN:

------------------------------------------------------------------------------------------------------
You can't call directly from a 32-bit program to a 16-bit program (nor the other way around). However, with some restrictions, you can construct a "thunk," or a special chunk of code that translates the call for you.
Check out the section on the "Thunk Compiler" in the "Programmer's Guide to Windows 95" in the Win32 SDK, available in the MSDN Library or in the Visual C++ documentation. Note that you can't call 16-bit DLLs from 32-bit applications under Windows NT—only under Windows 95.
------------------------------------------------------------------------------------------------------

In this 'Thunk compiler' section you can find any desciption you need to do this.

I hope that helps,

ZOPPO
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2580607
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2580625
In simple ,you would like to call a 16 bit function from 32 bit code and I use thunking mechanism .Following   those steps: create 16 bit dll, create thunk script, compile with thunk.exe and I obtain the obj.

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2580654
a DDE server can also do this..
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:gejianye
ID: 2582567
How to do this with DDE?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2582891
Sorry.I'm not very familiar with DDE and it's old tech.
You can use thunking mecanism.
Maybe another expert can help on the DDE,i'm sure.
But You can use WM_COPYDATA to pass the params
Regards
W.Yinan
0
 
LVL 2

Accepted Solution

by:
danelroisman earned 200 total points
ID: 2584404
HOWTO: Call 16-bit Code from 32-bit Code Under Windows 95

SUMMARY
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 Windows 95 is called a flat thunk.

The following describes the three major steps involved in creating a flat thunk:

Create the thunk script.


Build the 32-bit DLL.


Build the 16-bit DLL.





MORE INFORMATION
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;
   }POINT, *LPPOINT;

   typedef struct _CIRCLE {
      POINT center;
      UINT  radius;
   }CIRCLE, *LPCIRCLE;

   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
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);

      BOOL WINAPI DllMain(HINSTANCE hDLLInst,
                          DWORD     dwReason,
                          LPVOID    lpvReserved)
      {
         if (!thk_ThunkConnect32("DLL16.DLL", "DLL32.DLL",
                                 hDLLInst, dwReason))
         {
            return FALSE;
         }
         switch (dwReason)
         {
            case DLL_PROCESS_ATTACH:
               break;

            case DLL_PROCESS_DETACH:
               break;

            case DLL_THREAD_ATTACH:
               break;

            case DLL_THREAD_DETACH:
               break;
         }
         return TRUE;
      }



Include the following lines in the EXPORTS section of the module definition (.def ) file for the 32-bit DLL. For example:
     
   thk_ThunkData32



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.


Compile the thunk script as follows (if not already compiled):

      thunk -t thk 32to16.thk -o 32to16.asm



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



Link this object module as part of the 32-bit DLL.


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
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",
                                 "DLL32.DLL",
                                 hInst,
                                 dwReason))
         {
            return FALSE;
         }
         return TRUE;
      }



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



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.

      THK_THUNKDATA16 @1  RESIDENTNAME
      DllEntryPoint   @2  RESIDENTNAME



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.


Compile the thunk script as follows (if not already compiled):

      thunk -t thk 32to16.thk -o 32to16.asm



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



Link this object module as part of the 16-bit DLL.


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.



0
 
LVL 5

Expert Comment

by:Wyn
ID: 2586270
danelroisman ,you should not answer for it's same to comments.You should answer him how do that using DDE or leave it unlocked to make more experts give information here.Otherwise,I have already answered.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 2772352
According to Wyn's last comment I don't think danelroisman deserves the points (since his comment doesn't provide any new information).
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now