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

Pertti Honkanen 30 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

Answer accepted by question author

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?

30+ people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Pertti Honkanen 30 Reputation points
    2026-11-28T22:59:22.77+00:00

    I thank for the answers and comments. I solved the problem when I studied the 32bit/64bit status of Excel. It is 64 bit. And then I made the corresponding change to the C++ projects. The system is now functioning, but there are still some minor problems with some Excel files.

    Pertti Honkanen

    Was this answer helpful?

    10 people found this answer helpful.
    0 comments No comments

  2. Brian Pham (WICLOUD CORPORATION) 1,595 Reputation points Microsoft External Staff Moderator
    2026-11-30T02:49:39.95+00:00

    Hi @Pertti Honkanen ,

    I'm glad to hear that updating your C++ projects to match the 64-bit version of Excel has resolved the main issue and that your system is now functioning again.

    Regarding the minor issues you mentioned with some Excel files, one additional thing worth checking is whether those workbooks contain older VBA declarations or references to previous DLL locations.

    If the affected workbooks use separate VBA modules, you may want to compare their declarations with those in a workbook that is now working correctly. Also, try recalculating the affected workbook using Ctrl + Alt + F9 to force a full recalculation, particularly if some cells still display previous error values.

    If the remaining issues persist, identifying whether they occur only in specific workbooks or with particular functions may help narrow down the cause.

    If my support has been helpful to you, I’d truly appreciate your interaction on my answer. Knowing that my support has made a difference is always appreciated and encourages me to continue providing the best support I can.

    Thank you.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

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.