C++/CLR 클래스
Calculrator.cpp
#include "Calculator.h"
Calculator::Calculator()
{
}
int Calculator::Sum(int a, int b)
{
return a + b;
}
int Calculator::Max(int a, int b)
{
return System::Math::Max(a, b);
}
string Calculator::HelloWorld(string text)
{
return text.append("\tHello World!");
} |
Calculrator.h
#pragma once
#include <string>
using std::string;
ref class Calculator
{
public:
Calculator();
int Sum(int a, int b);
int Max(int a, int b);
string HelloWorld(string text);
}; |
C++/CLR Native 랩퍼 클래스
CalculratorWrapper.cpp
#include "CalculatorWrapper.h"
CalculatorWrapper::CalculatorWrapper()
{
_managedObject = gcnew Calculator();
}
CalculatorWrapper::~CalculatorWrapper()
{
}
int CalculatorWrapper::Sum(int a, int b)
{
return _managedObject->Sum(a, b);
}
int CalculatorWrapper::Max(int a, int b)
{
return _managedObject->Max(a, b);
}
string CalculatorWrapper::HelloWorld(string text)
{
return _managedObject->HelloWorld(text);
}
ICalculatorWrapper *ICalculatorWrapper::CreateInstance()
{
return ((ICalculatorWrapper *)new CalculatorWrapper());
}
void ICalculatorWrapper::Destroy(ICalculatorWrapper *instance)
{
delete instance;
} |
CalculratorWrapper.h
#pragma once
#include <vcclr.h>
#include <string>
#include "ICalculatorWrapper.h"
#include "Calculator.h"
class DLLAPI CalculatorWrapper : ICalculatorWrapper
{
private:
gcroot<Calculator ^> _managedObject;
public:
CalculatorWrapper();
~CalculatorWrapper();
int Sum(int a, int b);
int Max(int a, int b);
string HelloWorld(string text);
}; |
※ gcroot 템플릿 이용해서 C++/CLR 개체 사용
C++/CLR 랩퍼 인터페이스
ICalculratorWrapper.h
#pragma once
#include <string>
using std::string;
#ifdef CPLUSPLUSCLR_EXPORTS
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#pragma comment (lib, "CPlusPlusCLR.lib")
#endif
class DLLAPI ICalculatorWrapper
{
public:
virtual int Sum(int a, int b) = 0;
virtual string HelloWorld(string text) = 0;
virtual int Max(int a, int b) = 0;
static ICalculatorWrapper *CreateInstance();
static void Destroy(ICalculatorWrapper *instance);
}; |
랩퍼 클래스를 이용해 사용하는 Native 클래스
#include <iostream>
#include "ICalculatorWrapper.h"
using std::cout;
using std::endl;
void main()
{
ICalculatorWrapper *wrapper = ICalculatorWrapper::CreateInstance();
int sum = wrapper->Sum(12, 12);
int max = wrapper->Max(100, 9);
string text = wrapper->HelloWorld("test");
cout << sum << endl;
cout << text << endl;
ICalculatorWrapper::Destroy(wrapper);
} |