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


    摘自《Borland C++BuilderT 6 Developer's Guide》  一书

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

    As powerful as C++Builder is, the majority of DLLs developed and used within the software community are built using Visual C++. Therefore, chances are you will need to interface your C++Builder code with a Visual C++ DLL. Again, there are two ways for a DLL to attach to an application. Either it can be loaded in dynamically, or it can be loaded in statically if the LIB file associated to the DLL is linked at compile time. If that LIB file is a Microsoft LIB file, the DLL won't be able to load. The reason is because of the compatibility issues between the LIB file format for Visual C++ and C++Builder. Both vendors use different exporting conventions. Microsoft supports the Common Object Format File (COFF), whereas Borland uses the Object Model Format (OMF). Fortunately, there is a way to create a Borland OMF import library file that represents a Microsoft built DLL.

    To create a Borland compatible .lib file for a Visual C++ DLL, you can use the COFF2OMF command-line tool from Borland, which resides in the Bin folder under C++Builder. COFF2OMF takes two arguments: the first is the source library's filename; and the second is the destination's filename.

    Coff2Omf MyDll.lib MyDll_bor.lib 

    In this example, COFF2OMF will generate a new OMF library file called MyDll_bor.lib. Within your C++Builder project, be sure to link using this MyDll_bor.lib file as part of the project file listing.

    NOTE

    The COFF2OMF utility only works on lib files with simple exported C functions. If C++ classes are exported, it will not work.

    If this doesn't work, you need to find out how the functions are being exported and give them an alias that C++Builder will like. To do this, first you should use Impdef.exe to create a definition file (or .def file), which enables you to view all the exported functions' names and ordinal numbers. Next, modify the exported functions in the .def file, so the function looks like this:

    Old export section 
    EXPORTS 
        _Add@8                        =_Add               @1 
    
    New export section 
    EXPORTS 
       Add=_Add@8 

    After you've made the changes to the library, save the .def file. Now you can use Implib.exe on this file to create a new library file that C++Builder should like. Implib.exe also takes two parameters: the destination and the source. For example

    Implib MyDll.lib MyDll.def 

    Because you now have a library in C++Builder style, you should be able to include it in your project and use the DLL and .lib.

    See the VCppProject folder on the CD-ROM that accompanies this book for the complete C++Builder project CallVCppDll.bpr. This uses the Visual C++ DLL mentioned previously.

    本人新博客网址为:http://www.hizds.com
    本博客注有“转”字样的为转载文章,其余为本人原创文章,转载请务必注明出处或保存此段。c++/lua/windows逆向交流群:69148232
  • 相关阅读:
    334 Increasing Triplet Subsequence 递增的三元子序列
    332 Reconstruct Itinerary 重建行程单
    331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化
    330 Patching Array
    329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径
    328 Odd Even Linked List 奇偶链表
    327 Count of Range Sum 区间和计数
    326 Power of Three 3的幂
    Java中的Class.forName
    巧用Java中Calendar工具类
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/2677001.html
Copyright © 2020-2023  润新知