2
votes

I am trying to use a Visual C++ dll in a VB.NET windows application, both created in VS2010. In the Windows project I can successfully add the dll to my references but only functions without pointer arguments are usable in the program and visible in the object browser. I borrowed a simple example I found and changed one of the function arguments to be a pointer. header file:

// TestDLL.h

using namespace System;
namespace MathFuncs
{
    public ref class MyMathFuncs
    {
        public: 
            static double Add(double *a, double B);
            static double Subtract(double a, double B);
            static double Multiply(double a, double B);
            static double Divide(double a, double B);
    };
}

cpp file

// TestDLL.cpp
// compile with: /clr /LD

#include "TestDLL.h"

namespace MathFuncs
{
    double MyMathFuncs::Add(double *a, double B)
    {
        return *a + b;
    }

    double MyMathFuncs::Subtract(double a, double B)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double B)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double B)
    {
        if (b == 0)
        {
            throw gcnew DivideByZeroException("b cannot be zero!");
        }

        return a / b;
    }
}

The dll compiles successfully with no warnings. When I reference the dll in a simple windows form I get the error:

"Error 1 'Add' has a return type that is not supported or parameter types that are not supported."

If I remove the pointer the dll works fine. From other forums I thought the calling convention might be a problem and tried using __stdcall but I got another error saying reference classes can't use __stdcall.

I also tried not referencing the dll and instead declaring the dll functions from a module in the windows application. I got an error saying "entry point not found" which I think is because C++ decorates the function name. I tried dumpbin.exe/EXPORTS "dll PATH" but it would not show the decorated function names. I have also tried creating an module definition file and using extern "c" although I most likely didn't use them properly. All solutions I have found to these problems have been for unmanaged C++ but do not work for managed Visual C++.

I would rather be able to reference the dll because it seems simpler. Any insight would be greatly appreciated.

1
Does the VB.NET support native pointers?Marian Spanik
Yes I believe so. C++ pointers are equivalent to variable passed "ByRef" in VB.NET.chimi007
Isn't the ByRef a managed reference, the same as double %a in C++/CLI? I am asking because I don't know anything about programming in VB.NET, I am just guessing, it might be an answer...Marian Spanik
I too am not an expert. A double passed ByRef in VB.NET is the same as a double* in C++/CLI. I don't think it is a type issue because Visual Studio assigns VB.NET equivalent types when the c++ dll is referenced.chimi007

1 Answers

1
votes

Change your definition to :

    double Add(double % a, double b)
    {
        return a + b;
    }

This compiles as ByRef and works:

enter image description here