Is there a way to have them in the same assembly?
Main Topics
Browse All TopicsI have a CLR project with both .net classes and native C++ classes.
How can I get the native C++ classes to be visible in .net or to even create an instance of a native C++ class in .Net C++ class.
Thanks
Adam
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I chose a CLR C++ project which is C++ on the .net framework, but has the ability to use both native and .net classes. I have this working, I now want to interact with native and .net in the same assembly.
I would assume this is possible as I can find references to load a .net class in native classes, I just can't find examples on how to load a native C++ class from a .Net Class.
You can call unmanaged code directly from C# using DllImport attribute.
http://www.csharphelp.com/
I don't know if this helps but below is a mix of managed and unmanaged code mixed together...
#include "stdafx.h"
using namespace System;
// Unmanaged class
class fooUnManaged
{
public:
void bar() {}
};
// Managed class
__gc class fooManaged
{
public:
void bar() {}
};
int main()
{
fooUnManaged *pUnManaged = new fooUnManaged;
fooManaged *pManaged = new fooManaged;
pUnManaged->bar();
pManaged->bar();
// It is necessary to delete unmanaged type
delete pUnManaged;
// It is NOT necessary to delete unmanaged type
//delete pManaged;
return 0;
}
Hi Evilrix
The last solution was the exact one I was looking for, after search the net for weeks with no solution I finally stumbled across the solution myself by playing around with the code. I only discovered this about 12 hrs ago and even did a quick writeup for anyone wanting a more detailled step by step guide.
http://www.dotnetcore.com/
Thanks Evilrix
You're welcome. Just one additional thing to note...
when heap allocating an unmanaged class you really should consider using the RAII idiom and use a smart pointer container to avoid potential memory leaks. The STL comes with auto_ptr, which can be used; although this has issues since it is not reference counted, rather it uses move semmantics. I would strongly recommend looking at boost shared_ptr.
http://www.boost.org/libs/
Consider...
void bar()
{
// Using RAII to avoid memory leaks
boost::shared_ptr<int> pN = new int;
// Call a function that can throw
FnMayThrow();
// pN is ALWAYS released
}
void foo()
{
// Using raw pointer -- imposible to avoid leaks
int * pN = new int;
// Call a function that can throw
FnMayThrow();
// May never get called
delete pN;
}
-Rx.
Business Accounts
Answer for Membership
by: lucky_jamesPosted on 2007-10-21 at 23:38:33ID: 20121152
For this you should keep c++ classes in a seperate assembly and .net ones in other.
make c++ dll a COM one.
Then reference it from .net dll.
Now you can use classes as cppdll.classname
Hope it helps.