Using CBuilder DLLs with Microsoft Visual C

   

Using C++Builder DLLs with Microsoft Visual C++

Sometimes it's important to be sure that programs created by other compilers such as Visual C++ or Visual Basic can access a DLL created by C++Builder. This ensures other vendors can write applications that can access the DLL. There shouldn't be much difficulty for a Visual C++ application to dynamically link a C++Builder DLL by using LoadLibrary() , GetProcessAddress() , and FreeMemory() during execution. But for static linking, which requires a LIB file, compatibility between Visual C++ and C++Builder can be a much different matter. This is because of the different exporting conventions used by the two different companies. As stated earlier, Microsoft supports the Common Object Format File (COFF), whereas Borland uses the Object Model Format (OMF). Fortunately, there is a way to create a Microsoft's COFF import library file that represents a Borland-built DLL.

To create an MS COFF import library file from a Borland OMF DLL a definition (DEF) file must first be created. This is accomplished using the Borland IMPDEF command-line tool at the DOS prompt.

 impdef mybcbdll_coff.def mybcbdll.dll 

This creates a definition file called mybcbdll_coff.def using the Borland IMPDEF utility, which extracts the function calls from mybcbdll.dll . Now, to keep the names consistent so the Microsoft linker knows exactly what to look for in the library representing the DLL, it's important to modify the newly created .def file and remove the additional underscore in front of the function names. For example, suppose the definition file created previously looks like the following:

 LIBRARY     MYBCBDLL.DLL  EXPORTS      @@Aboutform@Finalize           @4   ; __linkproc__ Aboutform::Finalize      @@Aboutform@Initialize         @3   ; __linkproc__ Aboutform::Initialize      _AboutBox                      @2   ; _AboutBox      _FormAbout                     @6   ; _FormAbout      _MsgQuery                      @1   ; _MsgQuery      ___CPPdebugHook                @5   ; _ 

Notice the underscores; they need to be removed. The .def file should be modified as follows :

 LIBRARY     MYBCBDLL.DLL  EXPORTS      @@Aboutform@Finalize           @4   ; __linkproc__ Aboutform::Finalize      @@Aboutform@Initialize         @3   ; __linkproc__ Aboutform::Initialize      AboutBox                      @2   ; _AboutBox      FormAbout                     @6   ; _FormAbout      MsgQuery                      @1   ; _MsgQuery      ___CPPdebugHook                @5   ; ___CPPdebugHook 

Next, the Microsoft Library Manager (LIB)c utility, which ships with Microsoft Visual C++, should be used to create the COFF import library file as follows:

 lib /DEF:mybcbdll_coff.def 

NOTE

Make sure to have access to the lib command-line program located in the Visual C++ bin directory by including this folder as part of the system Path.


The mybcbdll_coff.lib, which is generated by issuing the preceding command line, can now be added and linked by a Microsoft Visual C++ project.

Figure 16.18 provides an illustration of a Visual C++ application using a form contained with a C++Builder DLL. The code for both the C++Builder DLL and the Visual C++ example application is provided on the CD-ROM under the MyVCProg folder. (Library Manager)

Figure 16.18. A Visual C++ Application using a C++Builder DLL.

graphics/16fig18.gif


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net