C# using classes from C++ DLL

R

Rich

Yes, I'm a noob with .NET looking for help.

My goal is to eventually write a managed DLL in C++ that is a wrapper
for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.

Initially, I am just trying to write a very simple managed DLL in C++
and use it from C#. In my C++ DLL I define a namespace containing one
public class, this class contains a couple public functions. In C# I
reference the DLL and I can see the namespace and class, but I cannot
see any members of the class. What am I doing wrong?

Here are the details . . .

My C++ DLL project contains one source file that looks like this:

#define DLLEXPORT __declspec( dllexport )

namespace test_ns {

public class DLLEXPORT testclass {
public:
int x, y;

int add_function(int a, int b)
{
return(a + b);
}

int sub_function(int a, int b)
{
return(a - b);
}
};
}



On the C# side this is what I have:

using System;
using System.Collections.Generic;
using System.Text;
using test_ns;
using anothername;


namespace anothername
{
public class anotherclass
{
public int x, y;

public int addstuff(int a, int b)
{
return (a + b);
}
}
}


namespace MyFirstApplication
{
class Program
{
static void Main(string[] args)
{
int i;

testclass tc = new testclass();
anotherclass ac = new anotherclass();

/*
* At this point, I can see testclass but
* I cannot access any members (variables
* or functions) - ???
*/
// The compiler chokes on this with the error
// 'test_ns.testclass' does not contain a definition for
'add_function'
i = tc.add_function(5, 6);

// This works fine (class defined in C#)
i = ac.addstuff(12, 13);
Console.WriteLine("i = " + i);
}
}
}

I'm sure there is something simple I am missing. Can anybody enlighten
me on this issue?
 
W

Willy Denoyette [MVP]

| Yes, I'm a noob with .NET looking for help.
|
| My goal is to eventually write a managed DLL in C++ that is a wrapper
| for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.
|
| Initially, I am just trying to write a very simple managed DLL in C++
| and use it from C#. In my C++ DLL I define a namespace containing one
| public class, this class contains a couple public functions. In C# I
| reference the DLL and I can see the namespace and class, but I cannot
| see any members of the class. What am I doing wrong?
|
| Here are the details . . .
|
| My C++ DLL project contains one source file that looks like this:
|
| #define DLLEXPORT __declspec( dllexport )
|
| namespace test_ns {
|
| public class DLLEXPORT testclass {
| public:
| int x, y;
|
| int add_function(int a, int b)
| {
| return(a + b);
| }
|
| int sub_function(int a, int b)
| {
| return(a - b);
| }
| };
| }
|
|

This is an unmanaged class definition not a managed class.
Managed types use a different syntax, I would suggest you first take a look
at the C++ language documentation in msdn
http://msdn2.microsoft.com/en-us/library/xey702bw.aspx before you start
using managed C++.
Your class should look something like...

public ref class TestClass {
public: int x, y;
int add_function(...)
{...}

};

Note also that you may get better answers when posting C++ questions to the
vc NG.

Willy.
 
M

Marc Gravell

MSDN will probably tell you the new syntax - the other respondant
suggests "ref".

Also, note that you may [possibly] be able to PInvoke the unmanaged dll
directly from C# without even needing the MC++ wrapper, unless you are
trying to do something specific.

Marc
 
R

Rich

Marc said:
MSDN will probably tell you the new syntax - the other respondant
suggests "ref".

Also, note that you may [possibly] be able to PInvoke the unmanaged dll
directly from C# without even needing the MC++ wrapper, unless you are
trying to do something specific.

Marc

I had tried "ref" unsuccessfully - I'm sure I was doing something wrong
and with a little research I'll eventually figure it out. I have been
looking through various MSDN articles but I haven't entirely solved the
puzzle just yet.

I initially looked into the PInvoke approach, but this can get complex
when you have lots of C structures that need to be used - one article
suggested the C++ managed wrapper DLL as a better approach for cases
like this.

Thanks again.
 
M

MrAsm

My goal is to eventually write a managed DLL in C++ that is a wrapper
for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.

To use the unmanaged C DLL from C#, maybe you might consider wrapping
the umanaged C DLL into an ActiveX (use MFC or ATL and standard
unmanaged C++), and use C# COM interop.

If I don't mistake, given an ActiveX to Visual C# 2005, it can
automatically wrap it into C# class.

Mr.Asm
 

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