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

c#调用c++类库

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

c#调用c++类库

1.c++ 创建类库(无公共语言运行时)

2.添加类
mydll.h文件
#pragma once
extern "C" _declspec(dllexport) int _stdcall add(int a,int b);
mydll.cpp文件
#include "stdafx.h"
#include "mydll.h"
int _stdcall add(int a, int b)
{
    return (a+b);
}

2.c#

using System;
using System.Runtime.InteropServices;

namespace ConsoleAppTemp
{
    class Program
    {
        [Dllimport("DllTest.dll")] //Dllimport 必须引用using System.Runtime.InteropServices;
        public static extern int add(int a, int b);

        //可以通过EntryPoint特性指定函数入口,然后为函数定义别名
        [Dllimport("HelloWorldLib.dll", EntryPoint = "add")]
        public static extern int CustomName(int a, int b);
        static void Main(string[] args)
        {
            int a = add(1, 2);
            Console.WriteLine("输出结果为:" + a.ToString());
            //跟上面是一样的
            int b = add(1, 2);
            Console.WriteLine("输出结果为:" + b.ToString());
            Console.ReadKey();
        }
    }
}

参考:一、C++类库与C#类库相互调用 - 十色 - 博客园 (cnblogs.com)

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

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

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