Using Visual Studio 2017 community on Windows 10- 64 bits.
I am a C++ beginner and I have written a small C++ 64 bit dll so I can use it in (excel 2010 64bit)VBA .. This dll simply exports a very simple function "square" as described in this short video: https://www.youtube.com/watch?v=7aDAyf72S7o
I followed the steps correctly and the C++ project compiled corectly.
Problem:
When I call this from excel vba as shown below, I get Run time Error 453 "Can't find DLL Entry Point ...
This is my VBA code in Excel:
1 2 3 4 5
Declare PtrSafe Function Square Lib "C:\Users\Info-Hp\documents\visual studio 2017\Projects\square\x64\Debug\squareDll.dll" _ (ByRef x As Double) As Double
Sub Test()
MsgBox Square(2)
End Sub
When I open this squareDll C++ dll in Dependency Walker, it shows a red module that says : "Could not find the section that owns the Import Directory"
However, when calling the 'Square' function from my excel VBA, I still get the same 453 error.
Did I write the EXPORT MACRO shown at the top properly ? Do I have to leave the 'linker' word literally as well as the EXPORT word as they are or do I need to replace them with the actual linker and export names ?
I think the correct solution is actually using a .def file. Using __stdcall will lead again to the problem with name mangling. Dependy walker shows then _square@8 or sth. which isn't a correct name in VBA.
In this tutorial he show how to call a DLL from VB, so maybe it will work with VBA as well.
I don't have Office so I can't test it.
The info in the codeproject link turned out to be very useful .
I eventually managed to compile a working C++ 64bit dll that exports two functions which respectively set and remove a windows CBT hook using (SetWindowsHookEx) .. The purpose of this hook is to monitor the creation of any window whose Class name is #32770 and destroy them as soon as they are created in Excel.
Later on, I 'il post here the entire C++ project code and all the steps I followed in case anybody needs that info in the future.
Again thank you very much for your assistance.
LATE EDIT:
One could install this CBT hook directly in Excel VBA without the need of an external dll but if the VBA project is accidently reset (by pressing the IDE Stop button) or if an unhandled error occurs while the hook is installed, the whole excel application crashes!
On the other hand, setting the hook inside a dll is IDE safe as it prevents the above mentioned problem.