c++調用 c#的dll

以vs2012爲例 c++

1 用c#輸出dll c#

   (1)打開vs2012 spa

   (2)文件->新建->項目->c#->類庫 code

   (3)輸入代碼 orm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ClassLibrary1
{
    public class Class1
    {
        public void ShowMessage()
        {
            Console.WriteLine("成功調用了dll");
            Console.ReadLine();
        }

        public void ShowMessageBox()
        {
            MessageBox.Show("調用了Dll", "調用",
            MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        public int ShowArr(double[] a,int n)
        {
            for (int i = 0; i <= n; i++)
            {
                Console.WriteLine("a[{0}]={1}\n", i, a[i]);
            }
            return 0;
        }
    }
}


   (4)生成(B)->生成 ...(U) it

   (5)產生了一個dll文件 io

   (6)新建一個c++項目,項目屬性中 將 公共語言運行支持 選爲 公共運行時支持(/clr) class

   (7)VC++目錄中 將dll文件所在目錄 添加到 可執行文件目錄 包含目錄 引用目錄 庫目錄
cli

   (8)在VC++中寫入代碼 引用

// ConsoleApplicationdll.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#using "ClassLibrary1.dll"
using namespace ClassLibrary1;

int _tmain(int argc, _TCHAR* argv[])
{
	Class1 ^c=gcnew Class1();
	cli::array<double,1>^a=gcnew cli::array<double>(5);
	for(int i=0;i<5;i++){
		a[i]=i+0.1;
	}
	int n=4;
	//c->ShowMessage();
	c->ShowMessageBox();
	c->ShowArr(a,n);
	return 0;
}
相關文章
相關標籤/搜索