In the first part of this series we saw the problems involved with using DLLs in an environment different from the one in which they were developed. In this part we will learn more about resolving some of these problems.
DLL Conventions: Issues and Solutions - Part II - Use IMPLIB (Page 4 of 4 )
Next, if you find that all exported symbols use the __stdcall and a .def has indeed been used while building the VC++ DLL, you may directly use the IMPLIB utility to generate a Borland (OMF) compatible import library. We can now simply add the generated .lib file along with the DLL header file to the Borland builder project, and use the imported functions as we wish. To use IMPLIB, follow the syntax at command prompt as under:
:>IMPLIB <libfile name to generate> <VC++ DLL name>
But if any function uses the __cdecl calling convention, or the .def was not used while building the VC++ DLL, then you're bound to do some more work before using the DLL in Borland Builder project. This is due to the fact that __cdecl names are decorated and, in the absence of the .def file, the __stdcall names are also decorated.
Here you first need to resolve the naming issues manually, and then create the import library from this modified information. To do so, you'll need the IMPDEF utility and build. There are command line tools that ship with Borland C++ builder that ease this task for you, but still you'll need to do some manual work. If we see this process in two steps, the first one would be to generate a .def file from the VC++ DLL (using the IMPDEF utility) and edit it, and the second one would be to build the .lib file using the above mentioned IMPLIB utility. The syntax to use for IMPDEF is as shown below:
:> IMPDEF <def file name> <VC++ DLL File name>
In order to make the names compatible you'll need to edit them so that they're the same as expected by the Borland Linker. After you've generated a def file you can edit it using notepad or any standard editor. Just provide an alias name for each export/linker name you wish to modify. For example the line containing FooCDECL will be changed to _FooCDECL = FooCDECL.You may ignore the @1 @2 numbers at the end of lines. Here the name at the left of = sign is the name expected by the Borland Linker whereas the name at the right is the true linker name exported by the VC++ Compiler. By providing the alias manually you've just made them equivalent.
And finally to create an import library from this aliased definition file you again use the IMPLIB utility as shown below:
:> IMPLIB <lib file name to generate> <aliased .def file name>
When you add this generated lib to your Borland project, all unresolved externals will be found by the linker, and it will be happy to link them in your code.
This article explained the different calling conventions in use with the most prominent compilers available today, and the differences between them. We also observed the differences due to non-availability of standards for the naming conventions for symbols exported from DLLs, and finally learned how to use available tools to analyze and bridge these gaps in order to use the DLLs built with VC++ 6.0 in Borland C++ Builder projects.
Worth mentioning is the COFF2OMF utility, which does most of this work you. It is available with the command line tools in the Borland Bin directory. But again, there is no guarantee that it will do all the work for you. The points that you learned and observed here will surely help you handle similar problems with DLLs. In case you have any suggestions or questions, feel free to write to me at Digvijay.Chauhan@gmail.com.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.