Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

excel

vba

dll

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?

like image 570
wgrant Avatar asked Nov 01 '22 21:11

wgrant


1 Answers

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

like image 126
5 revs, 2 users 94% Avatar answered Nov 15 '22 03:11

5 revs, 2 users 94%