February 14th, 2003, 10:47 AM
Visual C++ DLL: How to build a DLL to be used from another programming language?
Q: How to build a DLL to be used from another programming language?
Q: How to a DLL and have the exported functions called via LoadLibrary() and GetProcAddress()?
A: When you create a DLL with Visual C++, and you want to use the DLL with other languages, there are a few things that you should do (creating a DLL isn't just compiling code).
- Make sure your exported functions are declared as extern "C". The reason for this is so that the C++ name mangling is not used on the exported function names.
- Make sure your exported functions use '__stdcall' calling convention. The reason for this is that most other Window's languages assume the exported functions retrieve parameters starting from the rightmost parameter, and that the function called is responsible for stack cleanup on return. By default C and C++ functions assume '__cdecl' calling convention.
You won't know when this problem exists until you call the exported function in your Delphi program. You did not declare
your function as '__stdcall', so your [insert porgramming language, other than C/C++, here] program would have crashed as soon as you called the function.
- Use '__declspec(dllexport)'. This is so that the function can be exported (of course).
- Use a module definition file (.def-file). This removes the "@x" decoration that appears at the end of VC++ exported function names, where "x" is the total number of bytes in the arguments passed to the function. When the user of the DLL is using another language, the programmer would rather call "tempest", not "tempest@4". Yes, many of these other languages have an "aliasing" features so that you can access the function by a different name, but it is a pain in the neck. Just use a .def-file to remove the additional decoration and you won't have to do anything else.
Note: Just exporting the name does *not* remove all the decoration - you must follow steps a) *and* d). A lot think that you only need to do a) and b) and c). This is if you plan on using the DLL in only Visual C++. If you leave the module definition file out of these steps, you will get what you're seeing now -- an exported name that is not what you expect.
If you do a search on CodeGuru, your question has been asked many times in various ways, and the reason for the problem is almost always that step d) was left out.
Here is a sample of a module-definition fie (DEF file).
The LIBRARY command must match the name of your DLL. So if your DLL is named YOURDLL.DLL, the LIBRARY command is followed by YOURDLL.
DESCRIPTION 'This is my DLL
The DESCRIPTION is any description you feel is appropriate for your DLL. The actual description is preceeded by a single quote (').
The EXPORTS lists the functions that you exported, along with an ordinal number. It doesn't matter what ordinal numbers you use, so long as you maintain different numbers for each function, and you shouldn't change the ordinal numbers when you create an upgraded version of your DLL -- persons preferring to use ordinal numbers to call your functions will get angry.
FAQ contributed by: [Paul McKenzie]
Last edited by Andreas Masur; July 25th, 2005 at 01:48 AM.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!