Calling DLL Functions from Visual Basic Applications
In order for Visual Basic applications (or applications in other languages such as Pascal or Fortran) to call functions in a C/C++ DLL, the functions must be exported using the correct calling convention without any name decoration done by the compiler.
__stdcall creates the correct calling convention for the function (the called function cleans up the stack and parameters are passed from right to left) but decorates the function name differently. So, when __declspec(dllexport) is used on an exported function in a DLL, the decorated name is exported.
The __stdcall name decoration prefixes the symbol name with an underscore (_) and appends the symbol with an at sign (@) character followed by the number of bytes in the argument list (the required stack space). So, the function when declared as:
int __stdcall func (int a, double b)
is decorated as:
The C calling convention (__cdecl) decorates the name as
To get the decorated name, use /MAP. Use of __declspec(dllexport) does the following:
- If the function is exported with the C calling convention (_cdecl), it strips the leading underscore (_) when the name is exported.
- If the function being exported does not use the C calling convention (for example, __stdcall), it exports the decorated name.
Because there is no way to override where the stack cleanup occurs, you must use __stdcall. To undecorate names with __stdcall, you must specify them by using aliases in the EXPORTS section of the .DEF file. This is shown below for the following function declaration:
int __stdcall MyFunc (int a, double b); void __stdcall InitCode (void);
In the .DEF file:
EXPORTS MYFUNC=_MyFunc@12 INITCODE=_InitCode@0
For DLLs to be called by programs written in Visual Basic, the alias technique shown in this article is needed in the .DEF file. If the alias is done in the Visual Basic program, use of aliasing in the .DEF file is not necessary. It can be done in the Visual Basic program by adding an alias clause to the Declare statement.
What do you want to know more about?
- Exporting from a DLL
- Exporting from a DLL using .DEF files
- Exporting from a DLL using __declspec(dllexport)
- Exporting C++ functions for use in C-language executables
- Determining which exporting method to use
- Decorated names