Create a new C++/CLI project in visual studio and add a reference to your C# dll. Assume we have a C# dll called DotNetLib.dll
with this class in:
namespace DotNetLib
{
public class Calc
{
public int Add(int a, int b)
{
return a + b;
}
}
}
Now add a CLR C++ class to your C++/CLI project:
// TestCPlusPlus.h
#pragma once
using namespace System;
using namespace DotNetLib;
namespace TestCPlusPlus {
public ref class ManagedCPlusPlus
{
public:
int Add(int a, int b)
{
Calc^ c = gcnew Calc();
int result = c->Add(a, b);
return result;
}
};
}
This will call C# from C++.
Now if needed you can add a native C++ class to your C++/CLI project which can talk to the CLR C++ class:
// Native.h
#pragma once
class Native
{
public:
Native(void);
int Add(int a, int b);
~Native(void);
};
and:
// Native.cpp
#include "StdAfx.h"
#include "Native.h"
#include "TestCPlusPlus.h"
Native::Native(void)
{
}
Native::~Native(void)
{
}
int Native::Add(int a, int b)
{
TestCPlusPlus::ManagedCPlusPlus^ c = gcnew TestCPlusPlus::ManagedCPlusPlus();
return c->Add(a, b);
}
You should be able to call the Native class from any other native C++ dll's as normal.
Note also that Managed C++ is different to and was superceeded by C++/CLI. Wikipedia explains it best:
http://en.wikipedia.org/wiki/C%2B%2B/CLI
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…