HOWTO: Create an OLE Server to Implement "Thunking"

ID: Q141939


The information in this article applies to:
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0


SUMMARY

An out-of-process OLE server (that is, an EXE) may be called by either a 16- bit or 32-bit program. Hence, it can be used as an interface between a 16- bit program that requires the services of a 32-bit DLL, or a 32-bit program that calls functions inside a 16-bit application/DLL. This permits the OLE system DLLs to handle any necessary "thunking" of the 16-bit to 32-bit or 32-bit to 16-bit translations.

This article demonstrates how the functions contained in a 16-bit DLL can be called from a 32-bit program using an OLE server as an intermediary.


MORE INFORMATION

A key goal of OLE is to establish a standardized way for objects to be created and to communicate with one another. This communication can take place between applications even if they are written in different languages. Because OLE defines the interface, it handles the communication between the two objects. Please see the REFERENCES section below for more details.

In particular, one useful corollary of the standardized interface that OLE demands is that an out-of-process OLE server can act as an intermediary to perform the "thunking" required if a 32-bit application needs to call a function contained in a 16-bit DLL and vice versa.

Example: A 32-Bit Program Calling a 16-Bit DLL

This example creates a 16-bit OLE server to wrap the functions contained in a 16-bit DLL. In this example, the Mbf2ieee.dll is used. You must have this DLL in order to be able to complete this example.

This DLL converts floating point numbers in the earlier Microsoft Binary Format (MBF) into IEEE format.

For additional information, please see the following article in the Microsoft Knowledge Base:
Q113439 : VB3PRB: Converting MBF to IEEE in Visual Basic for Windows

The file can be downloaded from the Microsoft Software Library as Mbf2ieee.exe. This DLL is an example of a 16-bit DLL that is difficult to update to 32-bit because the DLL calls two C run-time functions that have not been ported over to 32-bit.

Use the steps below to create the OLE server:
  1. Start a new Visual Basic project in Visual Basic 16-Bit Edition.


  2. Add a class module to the project by clicking the Insert/Class Module menu option.


  3. Bring up the Property window for the class module and modify the following properties:
    
          Instancing:   2-Creatable MultiUse
          Name:         OLE2MBF2IEEE
          Public:       True
     


  4. In the new class module, place the following code in the General Declarations section:
    
          Private Declare Function cvs Lib "mbf2ieee.dll" _
          (x As String) As Single
          Private Declare Function cvd Lib "mbf2ieee.dll" _
          (x As String) As Double
          Private Declare Function mks Lib "mbf2ieee.dll" _
          (x As Single) As String
          Private Declare Function mkd Lib "mbf2ieee.dll" _
          (x As Double) As String
    
          Public Function CvsOle(x As String) As Single
              CvsOle = cvs(x)
          End Function
    
          Public Function cvdole(x As String) As Double
              cvdole = cvd(x)
          End Function
    
          Public Function mksole(x As Single) As String
              mksole = mks(x)
          End Function
    
          Public Function mkdole(x As Double) As String
              mkdole = mkd(x)
          End Function
     


  5. Add a module to the project by clicking the Insert/Module menu option. Add a single, empty subroutine to the code module:
    
          Sub Main()
          End Sub
     
    This is required because the OLE server must either start in a "Sub Main" subroutine or in a form. Because a form is not required for this OLE server, including it would not be a good use of resources.


  6. Add a name for the project. On the Tools/Options menu, click the Project tab, and enter the following settings:
    
          Startup Form:   Sub Main()
          Project Name:   MBFIEEEServer
     
    Also, in this same tab, select the OLE Server radio button in the StartMode frame.


  7. Build the server. On the File menu, click Make EXE, name the executable Mbfole.exe, and place the EXE into the \vb directory.


  8. Exit from Visual Basic 16-bit edition.


Now, it is a simple matter to make a client that makes uses of the server.
  1. Create a new project in Visual Basic, using any 32-bit edition. Form1 is created by default.


  2. On the Tools menu, click References and place an x next to the MBFIEEEServer option. Click Okay to close the dialog box.


  3. In the Form_Click event for the Form, place the following code:
    
          Private Sub Form_Click()
              Dim fInput As Single
              Dim CVSString As String
              Dim MBF as New OLE2MBF2IEEE
              Dim newresult As String
    
              fInput = 1234.6789  'IEEE format
    
          'Convert from Single to string
              newresult = MBF.mksole(fInput)
    
          'Convert from String to single
              CVSString = MBF.CvsOle(newresult)
              MsgBox CStr(CVSString)
          End Sub
     


Run the program by pressing the F5 key. Click on Form1, and a message box will appear with the original number dimensioned in the routine. The OLE server has successfully called the Cvs and Mks functions.


REFERENCES

"Microsoft OLE Today and Tomorrow: Technology Overview," created December, 1993. Found under Backgrounders and White Papers, Operating System Extensions, Microsoft Developer Network CD-ROM.

For additional information, please see the following article in the Microsoft Knowledge Base:

Q140520 : PRB: Converting MBF to IEEE in Visual Basic for Windows

Keywords : kbVBp400 IAPOLE VB4WIN
Version : 4.0
Platform : NT WINDOWS
Issue type :


Last Reviewed: June 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.