5

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?

5
  • OK, I seem to have misinterpreted your 1st version too early not being a valid question. Now I see, you wanted to provide another Q&A by giving the solution. I'd recommend you put your solution part as an answer to the question. You can do so here, it's valid and encouraged. Commented Jul 4, 2014 at 18:00
  • 2
    Consider posting your solution as an answer Commented Jul 4, 2014 at 18:01
  • @wgrant /OT Note: You don't need to click save to get intermediate versions of your edits backed up here (SO backs up your drafts while you're editing after a certain time). As soon you click save, you're publishing your stuff, and you may receive downvotes and critique about your actually unfinished stuff. Sorry for the initial down/close votes, you're welcome in general. Put your solution as an answer as suggested, and I'd even upvote both of it. Commented Jul 4, 2014 at 18:11
  • @david-heffernan Nice action, but my initial intention was to get the OP learning s.th. about how the site works. You're right, I should have been clarifying using another comment (did so now). Commented Jul 4, 2014 at 20:02
  • Thanks for the insight everyone. I had intended to provide the answer immediately as @Niall and πάντα ῥεῖ suggested but since I had less than 10 rep, SO made me wait 24 hours before posting an answer. You guys beat me to the punch :) Commented Jul 7, 2014 at 18:44

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

A quick point: your problem might not be a parameter mismatch in your external function, or in the declaration that imports it - there's a known compiler glitch in VBA that raises error 49.

'Stacker pstraton's answer to 'Excel - Runtime Error 49: Bad DLL calling convention'

You're not completely blameless - you probably have a parameter mismatch with a returned type that should work (Long populating an Int, Variants populating just about anything) in the calling function - but I have never heard of anyone banishing a persistent Runtime Error 49 by better coding. You just recompile, or export and reimport the modules.

The worst of it is that a genuine Forty-Niner is far harder to debug than it needs to be: the statement that triggers the error message usually isn't a call to the misdeclared external function. If you're lucky, it's the 'Exit Sub' of the caller; and some of the time it's something further up the stack that you can trace back to the bad call; but, sometimes, it isn't obvious at all and there's no deterministic debugging strategy at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.