Reputation: 11
I have a simple DLL test case that I have compiled in Visual Studio 2022, with the intent to call it from Excel VBA (64 bit):
primefact.cpp:
#include "pch.h"
#include "primefact.h"
#define DllExport __declspec( dllexport )
DllExport bool __stdcall isPrimeNumber(long long z)
{
long long i;
if (z < 2) return false;
if (z == 2) return true;
if (z % 2 == 0) return false;
for (i = 3; i * i <= z; i += 2)
{
if (z % i == 0) return false;
}
return true;
primefact.h
#pragma once
__declspec(dllexport) bool isPrimeNumber(long long z);
VBA code:
Declare PtrSafe Function isPrimeNumber Lib "C:\Users\theld\source\repos\PrimeFactors\x64\Debug\PrimeFactors.dll" (ByVal z As LongLong) As Boolean
Sub test()
Dim z As LongLong
Dim a As Boolean
z = 11
a = isPrimeNumber(z)
MsgBox a
End Sub
When compiled in the Debug configuration, VBA can successfully call the DLL. The same code, compiled in the Release configuration causes a Run-time Error 453, Can't find DLL entry point. And yes, I did change the directory in the "Declare" VBA statement to point to the Release subdirectory.
When I run DUMPBIN on the two DLLs, the Debug version outputs (among other things):
1 0 00011299 isPrimeNumber = @ILT+660(?isPrimeNumber@@YA_N_J@Z)
while the Release version outputs:
1 0 00001010 ?isPrimeNumber@@YA_N_J@Z = ?isPrimeNumber@@YA_N_J@Z (bool __cdecl isPrimeNumber(__int64))
so apparently the Release configuration is not demangling the function export, while Debug is.
I have tried including a module definition file (and not):
LIBRARY PrimeFactors
EXPORTS
isPrimeNumber
including (and not) the __declspec( dllexport ) and/or the __stdcall directives without affecting the VBA error and the DUMPBIN output. I have also tried changing the Configuration Properties/C/C++/Code Generation/Runtime Library among the various options, with no effect.
Upvotes: 0
Views: 50
Reputation: 11
First, in response to Hans Passant's response - yes, there is (or was) a problem, since Excel VBA could not find the DLL entry point.
Second, a huge thank you to Hermann Baum, the author of the tutorial I was following (Integrate your own DLL (64 bit) created with Visual Studio 2022 into Excel VBA). He was able to reproduce my problem, and found the solution. You have to tell Visual Studio the name of the module definition file again (Configuration Properties/Linker/Input/Module Definition File) when you switch from Debug mode to Release mode. Despite Microsoft's documentation stating otherwise, you need the module definition file to get the linker to assign demangled names (i.e. __declspec ( dllexport ) is not sufficient).
Now the code works!
Upvotes: 1