Troubleshooting VC++ DLL in VB.Net

Posted by Jolyon on Programmers See other posts from Programmers or by Jolyon
Published on 2012-09-02T20:50:05Z Indexed on 2012/09/02 21:48 UTC
Read the original article Hit count: 426

Filed under:
|
|

I'm trying to make a solution in Visual Studio that consists of a VC++ DLL and a VB.Net application. To figure this out, I created a VC++ Class Library project, with the following code (I removed all the junk the wizard creates):

mathfuncs.cpp:

#include "MathFuncs.h"
namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }
}

mathfuncs.h:

using namespace System;
namespace MathFuncs
{
    public ref class MyMathFuncs
    {
    public:
        static double Add(double a, double b);
    };
}

This compiles quite happily. I can then add a VC++ console project to the solution, add a reference to the original project for this new project, and call it as follows:

test.cpp:

using namespace System;
int main(array<System::String ^> ^args)
{
    double a = 7.4;
    int b = 99;
    Console::WriteLine("a + b = {0}",
        MathFuncs::MyMathFuncs::Add(a, b));
    return 0;
}

This works just fine, and will build to test.exe and mathsfuncs.dll.

However, I want to use a VB.Net project to call the DLL. To do this, I add a VB.Net project to the solution, make it the startup project, and add a reference to the original project. Then, I attempt to use it as follows:

MsgBox(MathFuncs.MyMathFuncs.Add(1, 2))

However, when I run this code, it gives me an error: "Could not load file or assembly 'MathFuncsAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Do I need to expose the method somehow?

I'm using Visual Studio 2008 Professional.

© Programmers or respective owner

Related posts about visual-studio

Related posts about vb.net