GithubHelp home page GithubHelp logo

lsalamon / csharp-to-cpp-cli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lodle/csharp-to-cpp-cli

0.0 2.0 0.0 328 KB

Convert C# classes to be used with C++/CLi DLL which then can be used with c++

C# 81.73% C++ 17.72% C 0.55%

csharp-to-cpp-cli's Introduction

This creates a native wrapper around managed C# classes using C++/Cli.

Include a reference to CSharpToCpp.DLL and mark the classes you want exposed using the ExposeToCpp Attribute.

Then run CSharpToCppGenerator pointed at your Dll and it will parse it generating the files you need to complie 
into a c++/cli dll.

Works with String and native types (int, double, bool). Support for List and Dictonary soon.

For Example:

[ExposeToCpp]
public class TestClass
{
	public String ClassName { get; set; }
	public int ClassCount { get; set; }

	public TestClass()
	{
	}
}
	
	
Produces:

////////////////////////////// Header //////////////////////////////

#pragma once
#include "BaseClass.h"

#ifndef DLLFN
	#ifndef DLL_EXPORT
		#define DLLFN __declspec( dllimport )
	#else
		#define DLLFN __declspec( dllexport )
	#endif
#endif


class TestClassI
{
public:
	virtual void GetClassName(char* szOutBuff, size_t nOutBuffSize)=0;
	virtual void SetClassName(const char* _ClassName)=0;

	virtual int GetClassCount()=0;
	virtual void SetClassCount(int _ClassCount)=0;

	virtual int GetInt()=0;

	virtual void ToString(char* szOutBuff, size_t nOutBuffSize)=0;

	virtual int GetHashCode()=0;
};

extern "C"
{
	DLLFN TestClassI* NewTestClass();

#ifdef DLL_EXPORT
	TestClassI* NewTestClassCPP(CSharpTestLib::TestClass^ _Internal);
#endif
}


////////////////////////////// CPP //////////////////////////////

#define DLL_EXPORT
#include "TestClass.h"

using namespace CSharpTestLib;

class TestClassCPP : public TestClassI
{
public:
	TestClassCPP() { m_TestClass = gcnew TestClass(); }
	TestClassCPP(TestClass^ _Internal) { m_TestClass = _Internal; }

	virtual void GetClassName(char* szOutBuff, size_t nOutBuffSize)
	{
		System::String^ ret = m_TestClass->ClassName;
		std::string szRet = marshal_as<std::string>(ret);
		strncpy_s(szOutBuff, nOutBuffSize, szRet.c_str(), szRet.size());
	}

	virtual void SetClassName(const char* _ClassName)
	{
		m_TestClass->ClassName = gcnew System::String(_ClassName);
	}

	virtual int GetClassCount()
	{
		return m_TestClass->ClassCount;
	}

	virtual void SetClassCount(int _ClassCount)
	{
		m_TestClass->ClassCount = _ClassCount;
	}

	virtual int GetInt()
	{
		return m_TestClass->GetInt();
	}

	virtual void ToString(char* szOutBuff, size_t nOutBuffSize)
	{
		System::String^ ret = m_TestClass->ToString();
		std::string szRet = marshal_as<std::string>(ret);
		strncpy_s(szOutBuff, nOutBuffSize, szRet.c_str(), szRet.size());
	}

	virtual int GetHashCode()
	{
		return m_TestClass->GetHashCode();
	}

	TestClass^ InternalObject(){return m_TestClass;}
	virtual void Destroy(){delete this;}

private:
	gcroot<TestClass^> m_TestClass;
};

extern "C"
{
	DLLFN TestClassI* NewTestClass()
	{
		return new TestClassCPP();
	}

	TestClassI* NewTestClassCPP(TestClass^ _Internal)
	{
		return new TestClassCPP(_Internal);
	}
}
 //////////////////////////////

csharp-to-cpp-cli's People

Contributors

mohitideoris avatar sidorares avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.