Add-on components that enhance and customize the Visual Studio integrated development environment
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↔ VBADouble - C++
float↔ VBASingle - C++
int/ 32-bitlong↔ VBALong - C++ pointer or handle ↔ VBA
LongPtr - C++ 64-bit integer ↔ VBA
LongLongon 64-bit Office - C++ value parameter ↔ VBA
ByVal - C++ pointer/output parameter ↔ normally VBA
ByRefor aLongPtr, 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:
- Excel’s 32/64-bit status.
- The DLL’s x86/x64 build target.
- One C++ signature and its matching VBA
Declare. - The
dumpbin /exportsline for that function.
References