I am importing a function into Excel VBA from a DLL. The DLL was created in Microsoft Visual C++ (with a mostly unmodified version of the default example). The return value of the function call in VB is not correct, but I do not know the reason why.
The value I see is 0, but I expect to get a value of 42.
I found an almost identical question here. I tried their experiment, where I called the function from the spreadsheet. I saw the same behavior, the spreadsheet return was correct and the return in vba code was not.
The C++ code looks like this:
addnum.h
#ifdef ADDNUM_EXPORTS
#define ADDNUM_API __declspec(dllexport)
#else
#define ADDNUM_API __declspec(dllimport)
#endif
extern "C" {
ADDNUM_API int fnaddnum(void);
}
addnum.cpp
#include "stdafx.h"
#include "addnum.h
ADDNUM_API int fnaddnum(void)
{
return 42;
}
The VB code looks like this:
Declare Function fnaddnum _
Lib " ... path to dll ... " _
() As Integer
Sub use_dll()
Dim return_val As Integer
return_val = fnaddnum()
MsgBox ("Value is" & Str(return_var))
End Sub