I am writing a DLL to export functions to be used in Excel VBA - I have found a way to be able to pass parameters in but with mangled names. If I set up without name mangling then I can not pass parameters and get a calling convention error
I use the standard declaration for calling DLL exported functions from VBA:
VBA
Public Declare Function foo Lib "C:\ ... \helloworld.dll" (ByVal bar As Long) As Long
My function is set up as so:
helloworld.cpp
extern "C" __declspec(dllexport) long foo(long bar){
return bar * 2;
}
I compile with cl.exe /LD helloworld.cpp
using cl.exe
(Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30145 for x86
)
and dumplib/exports helloworld.dll
yields
Dump of file helloworld.dll
File Type: DLL
Section contains the following exports for helloworld.dll
00000000 characteristics
FFFFFFFF time date stamp
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 foo
Summary
2000 .data
6000 .rdata
1000 .reloc
A000 .text
If I call the function from VBA
VBA
dim x as long
x = foo(2)
I get the VBA error Bad DLL calling convention (Error 49)
If I add __stdcall
to the function signature,
extern "C" __declspec(dllexport) long __stdcall foo(long bar){
return bar * 2;
}
I get the following DLL export
Dump of file helloworld.dll
File Type: DLL
Section contains the following exports for helloworld.dll
00000000 characteristics
FFFFFFFF time date stamp
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 _foo@4
Summary
2000 .data
6000 .rdata
1000 .reloc
A000 .text
And the function now works if I use the alias in the VBA declaration
Public Declare Function foo Lib "C:\ ... \helloworld.dll" Alias "_foo@4" (ByVal bar As Long) As Long
VBA
dim x as long
x = foo(2)
'foo sets x = 4
Is it possible to pass parameters to functions but not have a mangled/ordinal name?