如何在C#中使用C ++类?这可能是一个简单的例子 [英] How work a C++ class in C#? It could be a simple example

查看:85
本文介绍了如何在C#中使用C ++类?这可能是一个简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

你的C#类的翻译将是:
The "translation" of your C# class would be:
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));

(您不需要在C#中严格使用无参数构造函数,如果您不这样做,将会默认一个提供任何构造函数)

但这是坏C# - 它会更好:

(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:

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));


这篇关于如何在C#中使用C ++类?这可能是一个简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆