栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c#调用c++dll

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#调用c++dll

文章目录

前言1、使用步骤2、 C++程序3、 C#程序总结


前言

llimport导入C++的动态链接库

1、使用步骤

1.用C#创建控制台应用程序,将生成的C++DLL拷贝到工程根目录下。
2.简单介绍Dllimport的几个关键属性:
EntryPoint:我们要从DLL中导入的函数名称;(Sum)
CharSet:指示用在入口点种的字符集;(Ansi)
CallingConvention:指示入口点的调用约定;(StdCall)
3.使用Dllimport,首先要引用 System.Runtime.InteropServices 命名空间;主要代码如下:

项目代码
github仓库:https://github.com/2012Netsky/CSharp_CPlusPlus_Dll

2、 C++程序

代码如下(示例):
export.h


#ifndef EXPORT_H_
#define EXPORT_H_

#define  DLL_EXPORTS

#endif

C++DLL.h


#ifndef C_DLL_H_
#define C_DLL_H_

#ifdef __cplusplus
extern "C"{
#endif

#define DLL_STD_CALL __stdcall						

#ifdef DLL_EXPORTS
#define DLL_STD_CAPI __declspec(dllexport)		
#else
#define DLL_STD_CAPI __declspec(dllimport)			
#endif

DLL_STD_CAPI int DLL_STD_CALL Sum(int ,int);

#ifdef __cplusplus
}
#endif

#endif

C++DLL.cpp

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

#include "export.h"
#include "C++DLL.h"



DLL_STD_CAPI int DLL_STD_CALL Sum(int a ,int b)
{
	return a + b;
}
3、 C#程序

代码如下(示例):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
//可以正常使用
namespace ConsoleTestDll
{
    class Program
    {
        
        [Dllimport("C++DLL.dll", EntryPoint = "Sum", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int Sum(int a, int b);         
        
        static void Main(string[] args)
        {
            int ret = Program.Sum(3,4);
            Console.WriteLine(ret);
            Console.ReadLine();
        }
    }
}
总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号