473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# using classes from C++ DLL

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?

Nov 11 '06 #1
6 2316
Well, I'm no managed C++ buff, but google turns up a few possibles,
e.g.
http://www.ondotnet.com/pub/a/dotnet...cpp_part3.html

The main difference I can see is the __gc modifier. Any use?

Marc

Nov 11 '06 #2

"Rich" <ri************@us.army.milwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
| 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.
Nov 11 '06 #3

Marc Gravell wrote:
Well, I'm no managed C++ buff, but google turns up a few possibles,
e.g.
http://www.ondotnet.com/pub/a/dotnet...cpp_part3.html

The main difference I can see is the __gc modifier. Any use?

Marc
Good article - I added the __gc modifier and it does work. This did
require setting the switch for CLR "old syntax".

I'll eventually figure out the "new syntax" but this works for now -
thanks!

Nov 11 '06 #4
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

Nov 11 '06 #5

Marc Gravell wrote:
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.

Nov 11 '06 #6
On 11 Nov 2006 10:52:02 -0800, "Rich" <ri************@us.army.mil>
wrote:
>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
Nov 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Simon Wittber | last post by:
I've noticed that a few ASPN cookbook recipes, which are recent additions, use classic classes. I've also noticed classic classes are used in many places in the standard library. I've been...
7
by: Ganesh Gella | last post by:
Hi All, I am planning to use Xalan to transform XML data by applying xls stylesheets. Here tricky part is, Xalan provides several C++ APIs, which are very much useful if our requirement is...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
6
by: Robert | last post by:
Hello. I have been trying out the Lebans ToolTip Classes at http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a form. The classes I am using are located at...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
2
by: Kevin Frey | last post by:
Is it possible to get c# perform a using statement where the namespace for the using is not specified literally but instead comes from a variable, a token, a predefined value etc. We have...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.