C++/CLI (.NET Classes) with C++ native classes

I 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
LVL 4
adam_pedleyAsked:
Who is Participating?
 
evilrixSenior Software Engineer (Avast)Commented:
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;
}
0
 
lucky_jamesCommented:
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.
0
 
adam_pedleyAuthor Commented:
Is there a way to have them in the same assembly?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lucky_jamesCommented:
i dont think so...... :(
i feel in above scenario, you need to tell the compiler that in what language you are using in it.( you remember you start the project wizard with 'a c# application' or a 'c++ application').
0
 
adam_pedleyAuthor Commented:
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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
You can call unmanaged code directly from C# using DllImport attribute.

http://www.csharphelp.com/archives/archive52.html
0
 
evilrixSenior Software Engineer (Avast)Commented:
...I should add this presupposes you have build the unmanaged code into a DLL and exported the appropriate functions.
0
 
adam_pedleyAuthor Commented:
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/tutorial.aspx?id=38

Thanks Evilrix
0
 
evilrixSenior Software Engineer (Avast)Commented:
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/smart_ptr/shared_ptr.htm

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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.