码迷,mamicode.com
首页 > 编程语言 > 详细

C#调用C++Dll

时间:2016-01-18 20:54:59      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:c# dllimport

1,本人闲着的使用做的一个测试,原因:上家公司Unity3D使用C++封装的Socket用C#来调用.这是一个例子,事实上,如果一些运算量大的功能用C++来写,然后使用C#调用的话,一来可以优化性能(你懂的),而来可以隐藏(保护)代码.不多说了,上测试

C++代码:

// CSharpMInvoke.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"


extern "C" __declspec(dllexport) int Add( int x , int y ){
	return x + y;
}

技术分享

技术分享

技术分享

C#代码:

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

namespace CSharpDll
{
    public class CPPDll
    {
        [System.Runtime.InteropServices.DllImport("CSharpMInvoke.dll",  CallingConvention=System.Runtime.InteropServices.CallingConvention.Cdecl)]
        public static extern int Add(int x, int y);
    }
}

注意:将C++生成的dll文件(Bin文件夹中)复制到C#项目Bin/Debug中

这里是:CSharpMInvoke.dll

源文件:

技术分享

本文出自 “Better_Power_Wisdom” 博客,请务必保留此出处http://aonaufly.blog.51cto.com/3554853/1736279

C#调用C++Dll

标签:c# dllimport

原文地址:http://aonaufly.blog.51cto.com/3554853/1736279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!