Quantcast
Channel: MicroStation Programming Forum - Recent Threads
Viewing all articles
Browse latest Browse all 7260

RE: [CONNECT C#] IViewMonitor

$
0
0

[quote user="Jan Slegr"]It's a question how your C++/CLI code is structured. What is usually recommended (and my experience with it is very good) is to have always two classes, which help to split both NET vs. native code and in the first place to help manage separately objects on a managed heap and native heap. So outer class is a kind of NET API and it holds and takes care about a pointer to native class, which is created in constructor and removed when the outer class is disposed.[/quote]

 

After additional research, I found an example of this approach and implemented it. So far, it seems to work (a lot more testing is needed).

 

// remove /LTCG from the link command line to improve linker performance
// Compile with "/clr /LD"

namespace MyNamespace
{
	// Create an native (unmanaged) class
	class MyNativeClass
	{
	private:

	public:
	//	MyNativeClass();
	//	~MyNativeClass();

	MyNativeClass::MyNativeClass()
	{
		printf("Construct MyNativeClass::MyNativeClass()\n");
	}

	MyNativeClass::~MyNativeClass()
	{
		printf("Destruct MyNativeClass::MyNativeClass()\n");
	}

	};	// end of MyNativeClass class


	///////////////////////////////////////////////////////////////////////////
	// use "ref" to denote a managed class or struct
	public ref  class MyManagedClass
	{
	private:
		MyNativeClass *m_Impl;

	protected:
		// Deallocate the native object on the finalizer just in case no destructor is called
		!MyManagedClass() {
			delete m_Impl;
		}

	public:
		// Allocate the native object on the C++ Heap via a constructor
		MyManagedClass::MyManagedClass()
			: m_Impl(new MyNativeClass)
		{
			printf("Construct MyManagedClass::MyManagedClass()\n");
			//mdlDialog_openInfoBox(L"MyNamespace::MyNamespace()");
		}

		// Deallocate the native object on a destructor
		MyManagedClass::~MyManagedClass()
		{
			printf("Destruct MyManagedClass::MyManagedClass()\n");
			delete m_Impl;
		}

	}; // end of MyManagedClass class

} // end of MyNamespace namespace


/////////// use in C#
using MyNamespace;
class FindInstancesOnElementTool : DgnElementSetTool
{
    private MyNamespace.MyManagedClass myManagedClass = new MyNamespace.MyManagedClass();
};

 

 

Bruce


Viewing all articles
Browse latest Browse all 7260

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>