Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How work a C++ Class in C#? It could be a simple example

// My try:
// C++
***************
// Computing.h
#pragma once
class Computing
{
public:
	Computing();
	int Compute(int a, int b);
	~Computing();
***************
// Computing.cpp
#include "Computing.h"
Computing::Computing()
{
}
int Computing::Compute(int a, int b)
{
	return a + b;
}
Computing::~Computing()
{
}
***************
// main.h
#pragma once
#include "Computing.h"

extern "C"
{
	extern __declspec(dllexport) Computing* CreateComputingObject();
	extern __declspec(dllexport) void DeleteComputingObject(Computing* computing);
	extern __declspec(dllexport) int CallCompute(Computing* computing, int a, int b);
}
***************
// main.cpp
#include "main.h"
#include <cstddef>
#include "Computing.h"

extern "C" __declspec(dllexport) Computing* CreateComputerObject()
{
	return new Computing();
}

extern "C" __declspec(dllexport) void DeleteCountingObject(Computing* computing)
{
	if(computing != NULL)
	{
		delete computing;
		computing = NULL;
	}
}

extern "C" __declspec(dllexport) int CallCompute(Computing* computing, int a, int b)
{
	int res = 0;
	if(computing != NULL)
	{
		res = computing->Compute(a, b);
	}
	return res;
}
*****************
// C#
// Program.cs
using System;
using System.Runtime.InteropServices;

namespace MainProgram
{
    public class CSUnmanagedTestClass : IDisposable
    {
        [DllImport("CppDLL.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr CreateComputingObject();

        [DllImport("CppDLL.dll", CharSet = CharSet.Unicode)]
        public static extern void DeleteComputingObject(IntPtr computing);

        [DllImport("CppDLL.dll", CharSet = CharSet.Unicode)]
        public static extern int CallCompute(IntPtr computing, int a, int b);

        private IntPtr m_pNativeObject;

        public CSUnmanagedTestClass()
        {
            this.m_pNativeObject = CreateComputingObject();
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool bDisposing)
        {
            if (this.m_pNativeObject != IntPtr.Zero)
            {
                DeleteComputingObject(this.m_pNativeObject);
                this.m_pNativeObject = IntPtr.Zero;
            }

            if (bDisposing)
            {
                GC.SuppressFinalize(this);
            }
        }
        ~CSUnmanagedTestClass()
        {
            Dispose(false);
        }

        public void PassInt(int a, int b)
        {
            CallCompute(this.m_pNativeObject, a, b);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            CSUnmanagedTestClass testClass = new CSUnmanagedTestClass();
            testClass.PassInt(42, 5);
            System.Console.WriteLine("The sting returned was: ");

            testClass.Dispose();

            Console.Read();
        }
    }
}


What I have tried:

How work a C++ Class in C#? It could be a simple example
Posted
Updated 21-Jun-17 23:16pm
Comments
Philippe Mori 23-Jun-17 10:17am    
Your question is not clear... If you want to translate the code to C# code then Solution 1 is the way to go. If the code above is what you have tried so far to interop between C# and C++, then you need to explain your problem. Also, in practice the best solution depends a lot on the actual C++ code as you many options (port code to C#, P/Invoke and mixed-mode C++ to name a few).

1 solution

The "translation" of your C# class would be:
C#
public class Computing
   {
   public Computing() {}
   public int Compute(int a, int b)
      {
      return a + b;
      }
   }
...
   Computing comp = new Computing();
   Console.WriteLine("{0} + {1} = {2}", 10, 20, comp.Compute(10, 20));
(You don't strictly need a parameterless constructor in C#, one will be defaulted for you if you don't provide any constructors)
But that's "bad" C# - it would be better as:
C#
public static class Computing
   {
   public static int Compute(int a, int b)
      {
      return a + b;
      }
   }
...
   Console.WriteLine("{0} + {1} = {2}", 10, 20, Computing.Compute(10, 20));
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900