Using C++ DLL in C#

S

Steffen Loringer

Hello,

I'm trying to understand usage of an C++ DLL in an C# application. The
example is easy but stops when execution the C# Console App with the
following exception:

System.EntryPointNotFoundException was unhandled
WrapperTest.wrapper.Multiply(Int32 a, Int32 b)


My DLL,compiled in VS 2005:

// Header "testdll.h"
class __declspec(dllexport) Testclass
{
public:
Testclass();
~Testclass();
int Multiply(int a, int b);
};

// Sourcefile "testdll.cpp"
#include <iostream>
using namespace std;
#include "testdll.h"

int Testclass::Multiply(int a, int b)
{

return (a * b);
}


and finally the C# Console App

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WrapperTest
{
class Program
{
static void Main(string[] args)
{
int x;
x = wrapper.Multiply(2, 3);
System.Console.WriteLine("C# Application startet");
System.Console.WriteLine(x);
}
}

class wrapper
{
[DllImport("TestDLL.dll")]
public static extern int Multiply(int a, int b);
}
}

Any hints for me?

Thanks a lot
Steve
 
M

Mattias Sjögren

Any hints for me?

You can't consume exported C++ classes via PInvoke, just separate
functions. So change the C++ code to something like

extern "C" declspec(dllexport) int _stdcall Multiply(int a, int b)


Mattias
 
F

Frans Bouma [C# MVP]

Mattias said:
You can't consume exported C++ classes via PInvoke, just separate
functions. So change the C++ code to something like

extern "C" declspec(dllexport) int _stdcall Multiply(int a, int b)

And then be sure you compiled the C++ dll with _stdcall ;) (default is
cdecl in VC++ 2005 I think)

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
B

Ben Voigt [C++ MVP]

Frans Bouma said:
And then be sure you compiled the C++ dll with _stdcall ;) (default is
cdecl in VC++ 2005 I think)

The __stdcall keyword will override the compiler command line option on a
per-function basis.
 
B

Ben Voigt [C++ MVP]

Steffen Loringer said:
Hello,

I'm trying to understand usage of an C++ DLL in an C# application. The
example is easy but stops when execution the C# Console App with the
following exception:

System.EntryPointNotFoundException was unhandled
WrapperTest.wrapper.Multiply(Int32 a, Int32 b)


My DLL,compiled in VS 2005:

// Header "testdll.h"
class __declspec(dllexport) Testclass

"__declspec(dllexport)" and "class" are not useful together in any way you
might imagine they are useful. There's a thread over in vc.language where
Doug Harrison and I are discussing whether there's any use for the
combination at all.
{
public:
Testclass();
~Testclass();
int Multiply(int a, int b);
};

// Sourcefile "testdll.cpp"
#include <iostream>
using namespace std;
#include "testdll.h"

int Testclass::Multiply(int a, int b)
{

return (a * b);
}


and finally the C# Console App

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WrapperTest
{
class Program
{
static void Main(string[] args)
{
int x;
x = wrapper.Multiply(2, 3);
System.Console.WriteLine("C# Application startet");
System.Console.WriteLine(x);
}
}

class wrapper
{
[DllImport("TestDLL.dll")]
public static extern int Multiply(int a, int b);
}
}

Any hints for me?

Thanks a lot
Steve
 
F

Frans Bouma [C# MVP]

Ben said:
The __stdcall keyword will override the compiler command line option
on a per-function basis.

Thanks, that's good to know, I assumed the command line option was
overriding all call directives. Makes sense indeed to have the keyword
override the command line option.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top