?
Solved

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

Posted on 2007-10-21
9
Medium Priority
?
648 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:adam_pedley
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:lucky_james
ID: 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.
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 20121372
Is there a way to have them in the same assembly?
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20121390
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Author Comment

by:adam_pedley
ID: 20121567
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20162185
You can call unmanaged code directly from C# using DllImport attribute.

http://www.csharphelp.com/archives/archive52.html
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20162194
...I should add this presupposes you have build the unmanaged code into a DLL and exported the appropriate functions.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 1320 total points
ID: 20162233
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
 
LVL 4

Author Comment

by:adam_pedley
ID: 20163241
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20163960
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question