0

I have a DLL that I wrote using C++ that contains functions like this:

bool _stdcall vbaBoolTest(LPSAFEARRAY *values)

I have set up Visual Studio to let me debug the DLL using Excel. I declare the function in VBA like this:

Private Declare Function vbaBoolTest Lib "myDLL.dll" (ByRef values() As Double) As Boolean

and I noticed that the function would always evaluate to True on the VBA side of things. I can debug the macro that uses these functions and set break points in the C++ code as well. I have tried forcing the value to be false in C++ but the macro always reads the return value as False. I got around it by changing the return type to BOOL in C++ and returning either TRUE or FALSE.

I wanted to know if this is the recommended way of doing things or if there is a better way.

kaineub
  • 162
  • 10
  • 2
    A `bool` is not the same as a `BOOL`. The C++ function should be using `BOOL`. – PaulMcKenzie Nov 23 '16 at 18:55
  • I knew that they weren't the same. I didn't know that the Boolean type in VBA wasn't the same as bool in C++ though. I'll use BOOL from now on. Thanks. – kaineub Nov 23 '16 at 19:32
  • You should be using the types that the Windows API has defined when passing or returning values like this. This includes `LONG`, `BOOL`, `ULONG`, etc. A `bool` is a C++ type that can only be used in C++ applications. It can vary in size and implementation, which is why you should not use it for VBA purposes. [For more reading...](http://stackoverflow.com/questions/4897844/is-sizeofbool-defined) – PaulMcKenzie Nov 23 '16 at 19:43
  • 3
    VB's `Boolean` is actually `VARIANT_BOOL` in C/C++. Also see [BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool](https://blogs.msdn.microsoft.com/oldnewthing/20041222-00/?p=36923) – Remy Lebeau Nov 23 '16 at 21:28
  • 2
    @PaulMcKenzie: You should delete your comments. At least the first one is blatantly wrong, with the second one being misleading. The upvotes on your first comment may be misinterpreted as authoritative affirmation. – IInspectable Nov 23 '16 at 22:24

0 Answers0