Microsoft KB Archive/141939

= How To Create an OLE Server to Implement "Thunking" =

Article ID: 141939

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q141939



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 about how to download Mbf2ieee.dll, click the article number below to view the article in the Microsoft Knowledge Base:

113439 PRB: VB3: Converting MBF to IEEE in Visual Basic for Windows

The file can be downloaded from the Microsoft Download Center 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:

 Start a new Visual Basic project in Visual Basic 16-Bit Edition. Add a class module to the project by clicking the Insert/Class Module menu option.  Bring up the Property window for the class module and modify the following properties:      Instancing:   2-Creatable MultiUse Name:        OLE2MBF2IEEE Public:      True

  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

  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.   Add a name for the project. On the Tools/Options menu, click the Project tab, and enter the following settings: <pre class="fixed_text">     Startup Form:   Sub Main Project Name:  MBFIEEEServer

Also, in this same tab, select the OLE Server radio button in the StartMode frame. </li> Build the server. On the File menu, click Make EXE, name the executable Mbfole.exe, and place the EXE into the \vb directory.</li> Exit from Visual Basic 16-bit edition.</li></ol>

Now, it is a simple matter to make a client that makes uses of the server. <ol> Create a new project in Visual Basic, using any 32-bit edition. Form1 is created by default.</li> On the Tools menu, click References and place an x next to the MBFIEEEServer option. Click Okay to close the dialog box.</li>  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

</li></ol>

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.

<div class="references_section">