Using functions written in C++ in Visual Basic and Excel

Pertti Honkanen 0 Reputation points
2026-11-27T19:52:13.0433333+00:00

I have a made a big system of functions calculating taxes and social transfers. The functions are written by C++ using Microsoft Visual Studio. I export the functions from the DLL files with the help DEF-files in C++ and Declaration statements in Visual Basic.  I have used the functions mainly in Microsoft Excel files. For many years the system has been working well in the Windows 10 system. Now I have a Windows 11 system, Excel version 2611. The function system is not working any more. There are no error statements when I compile the DLL files or when I use the functions in Excel. But the only return value is the #VALUE! error value. There no numeric return values, and this is valid for all functions (there are some 200 different functions). I have studied the Visual Basic documentation, but I cannot find any help.  Can anybody give me some advice?

Developer technologies | Visual Studio | Extensions

1 answer

Sort by: Most helpful
  1. AI answer

    2026-11-27T19:53:14.6966667+00:00

    The DLL architecture must match the Excel process architecture, after which every VBA Declare must exactly match the C++ function’s exported name, parameter types, return type, and value/reference semantics.

    1. Identify whether Excel is 32-bit or 64-bit

    In Excel, open File → Account → About Excel and look for 32-bit or 64-bit.

    A 64-bit Office process cannot load a 32-bit native DLL; Windows 11 being 64-bit does not by itself determine which DLL Excel can load.^1^

    You can also test the VBA platform in a standard module:

    Public Sub ShowPlatform()
    #If Win64 Then
        MsgBox "Excel/VBA is 64-bit"
    #Else
        MsgBox "Excel/VBA is 32-bit"
    #End If
    End Sub
    

    Use Win64, not Win32, as the decisive test: Win32 is true on both 32-bit and 64-bit VBA platforms.^2^

    Then build accordingly:

    • 64-bit Excel: Visual Studio project platform must be x64.
    • 32-bit Excel: project platform must be Win32/x86.
    • If both Office editions must be supported, produce separate x86 and x64 DLLs.

    2. Correct the VBA declarations

    For 64-bit Office, each declaration must contain PtrSafe. Adding PtrSafe alone is insufficient: pointers and handles must use LongPtr, while genuine 64-bit integer values use LongLong.^3^

    Do not replace every Long with LongPtr. A C++ int or long remains a 32-bit VBA Long; only pointers, handles, addresses, and pointer-sized values become LongPtr.

    For example:

    extern "C" __declspec(dllexport)
    double __stdcall CalculateTax(double income, int category)
    {
        return income * 0.25 + category;
    }
    

    The matching VBA declaration is:

    Private Declare PtrSafe Function CalculateTax Lib "TaxFunctions.dll" _
        (ByVal income As Double, ByVal category As Long) As Double
    
    Public Function TaxTest(ByVal income As Double) As Double
        TaxTest = CalculateTax(income, 1)
    End Function
    

    Pay particular attention to:

    • C++ double ↔ VBA Double
    • C++ float ↔ VBA Single
    • C++ int / 32-bit long ↔ VBA Long
    • C++ pointer or handle ↔ VBA LongPtr
    • C++ 64-bit integer ↔ VBA LongLong on 64-bit Office
    • C++ value parameter ↔ VBA ByVal
    • C++ pointer/output parameter ↔ normally VBA ByRef or a LongPtr, depending on the C++ signature

    VBA defaults parameters to ByRef; therefore, omitting ByVal where C++ expects a value changes the native call. The number, order, type, and passing mode of arguments must exactly agree.^4^

    3. Verify the actual exported names

    Run this from a Developer Command Prompt for Visual Studio:

    dumpbin /exports "C:\path\TaxFunctions.dll"
    

    Confirm that the names used by VBA appear exactly in the output; DLL entry-point names are case-sensitive. dumpbin /EXPORTS displays the DLL’s export table.^5^

    For C++ functions, use extern "C" to avoid C++ name decoration, or place the exact decorated names in the .def file. Decorated names can vary by compiler, making extern "C" preferable for a stable VBA interface.^6^

    Test one minimal exported function returning a constant before converting all 200 declarations. If that function works, add one parameter and return type at a time.

    4. If architecture and declarations are already correct

    Check whether the DLL has unresolved C/C++ runtime dependencies:

    dumpbin /imports "C:\path\TaxFunctions.dll"
    

    /IMPORTS lists the DLLs imported by the binary, including delay-loaded DLLs.^7^

    In Visual Studio, also inspect:

    Project → Properties → Configuration Properties → C/C++ → Code Generation → Runtime Library

    A build using /MD depends on the corresponding Microsoft Visual C++ runtime. Microsoft recommends deployment through the applicable Redistributable package rather than manually copying arbitrary runtime DLLs.^8^

    The most useful next result would be:

    1. Excel’s 32/64-bit status.
    2. The DLL’s x86/x64 build target.
    3. One C++ signature and its matching VBA Declare.
    4. The dumpbin /exports line for that function.

    References

    1. Compatibility between the 32-bit and 64-bit versions of Office
    2. Compiler constants
    3. PtrSafe keyword
    4. Bad DLL calling convention (Error 49)
    5. /EXPORTS
    6. Exporting from a DLL Using DEF Files
    7. /IMPORTS (DUMPBIN)
    8. Determine which DLLs to redistribute
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    20+ people found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.