Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C++/CLI

Native under Managed

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
26 Jan 2012CPOL5 min read 71.7K   933   38  
A guide about how to mix native and managed code in one solution
#include <assert.h>
#include <exception>
#include <eh.h>
#include <vcclr.h>

#include "num_dot_net.h"
#include "num_wrapper_util.h"

using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;

static gcroot<num::MyCliClass^> gCli = nullptr;

static gcroot<num::MyCliClass^> &getCli(void) {
	return gCli;
}

#ifndef _NUM_TRY
#	define _NUM_TRY try {
#endif // _NUM_TRY
#ifndef _NUM_CATCH
#	define _NUM_CATCH \
	} catch(const NumBaseException &ex) { \
		String^ msg = WrapperUtil::ConvertString(ex.what()); \
		throw gcnew Exception(msg); \
	} catch(const std::exception &ex) { \
		String^ msg = WrapperUtil::ConvertString(ex.what()); \
		throw gcnew Exception(msg); \
	}
#endif // _NUM_CATCH

namespace num {

namespace detail {

static void __FuncPtrEvent(const ParamList &p) {
	MyCliClass::_FuncPtrEvent(p);
}

class __Listener : public MyNativeClass::Listener {

public:
	__Listener() {
	}
	virtual ~__Listener() {
	}

	virtual void Callback(const ObjParamDict &p) {
		getCli()->_ListenerEvent(p);
	}

};

}

Int32 MyCliClass::IntegerProp::get() {
	return mNative->GetIntegerProp();
}

Void MyCliClass::IntegerProp::set(Int32 v) {
	mNative->SetIntegerProp(v);
}

void MyCliClass::FuncPtrEvent::add(MyFuncPtrEventHandler^ _d) {
	OnFuncPtrEvent += _d;
}

void MyCliClass::FuncPtrEvent::remove(MyFuncPtrEventHandler^ _d) {
	OnFuncPtrEvent -= _d;
}

void MyCliClass::FuncPtrEvent::raise(List<Object^>^ p) {
	if(OnFuncPtrEvent)
		OnFuncPtrEvent->Invoke(p);
}

void MyCliClass::ListenerEvent::add(MyListenerEventHandler^ _d) {
	OnListenerEvent += _d;
}

void MyCliClass::ListenerEvent::remove(MyListenerEventHandler^ _d) {
	OnListenerEvent -= _d;
}

void MyCliClass::ListenerEvent::raise(Dictionary<Object^, Object^>^ p) {
	if(OnListenerEvent)
		OnListenerEvent->Invoke(p);
}

MyCliClass::MyCliClass() {
	mNative = new MyNativeClass;
	mLsn = new detail::__Listener;
	gCli = this;

	mNative->SetFuncPtr(detail::__FuncPtrEvent);
	mNative->SetListener((detail::__Listener*)mLsn);
}

MyCliClass::~MyCliClass() {
	mNative->SetListener(NULL);

	delete (detail::__Listener*)mLsn;

	delete mNative;
}

Void MyCliClass::Fun0(List<Object^>^ p) {
	_NUM_TRY
		ParamList _p = WrapperUtil::ConvertParamList(p);
		mNative->Fun0(_p);
	_NUM_CATCH
}

Void MyCliClass::Fun1(Dictionary<Object^, Object^>^ p) {
	_NUM_TRY
		ObjParamDict _p = WrapperUtil::ConvertParamDict(p);
		mNative->Fun1(_p);
	_NUM_CATCH
}

Void MyCliClass::FuncPtrInvoke(const ParamList &p) {
	_NUM_TRY
		List<Object^>^ l = WrapperUtil::ConvertParamList(p);
		FuncPtrEvent(l);
	_NUM_CATCH
}

Void MyCliClass::ListenerInvoke(const ObjParamDict &p) {
	_NUM_TRY
		Dictionary<Object^, Object^>^ _p = WrapperUtil::ConvertParamDict(p);
		ListenerEvent(_p);
	_NUM_CATCH
}

void MyCliClass::_FuncPtrEvent(const ParamList &p) {
	_NUM_TRY
		if(!getCli()) return;
		getCli()->FuncPtrInvoke(p);
	_NUM_CATCH
}

void MyCliClass::_ListenerEvent(const ObjParamDict &p) {
	_NUM_TRY
		if(!getCli()) return;
		getCli()->ListenerInvoke(p);
	_NUM_CATCH
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
China China
Video game player & creator; Hardware geek & maker.

Comments and Discussions