Excel VBA: "Run-time error '49': Bad DLL calling convention" calling C++ dll

c++, dll, excel, vba

Solution

In VBA, functions that return `void` need to be declared as `Sub` instead of `Function`

So the declaration in VBA should be:

Private Declare Sub InitMCR Lib "MCRBoilerplate.dll" Alias "?initMCR@@YGXXZ" ()

See MSDN page on VBA Declare statement

Problem

I am attempting to call a C++ DLL from Excel-VBA. I know the DLL function is being executed as I inserted fputs() logging calls to track execution and the stamps are showing up in my log file. The problem is, whenever the DLL function returns, I get Error 49. Here is the declaration in VBA: ``` Private Declare Function InitMCR Lib "MCRBoilerplate.dll" Alias "?initMCR@@YGXXZ" () ``` and here is the declaration in C++ ``` __declspec(dllexport) void __stdcall initMCR() { ... } ``` Why am I getting this Error 49 behavior, even though the DLL calls appear to be working?

Original source