C#调用C++创建的DLL

C#调用C++写的DLL
第一步:用C++写好一个DLL,里面有一个导出函数  int Add(int plus1,int plus2),方法过程如下:
        1)新建工程-->MFC AppWizard[dll],选择Dynamic link
        2)新建头文件testDll.h,写入以下代码
                #ifndef TestDll_H_
  #define TestDll_H_
  #ifdef MYLIBDLL
  #define MYLIBDLL extern "C" _declspec(dllimport)
  #else
  #define MYLIBDLL extern "C" _declspec(dllexport)
  #endif
  MYLIBDLL int Add(int plus1, int plus2);
  #endif
       3)新建源文件testDll.cpp,写入以下代码
                #include "stdafx.h"
  #include "testdll.h"

  int Add(int plus1,int plus2)
  {
      int add_result = plus1 + plus2;
             return add_result;
  }
       4)修改向导生成的MyDll.def文件,如下所示
               ; MyDll.def : Declares the module parameters for the DLL.

  LIBRARY      "MyDll"
  DESCRIPTION  'MyDll Windows Dynamic Link Library'

  EXPORTS
  Add @1
      ; Explicit exports can go here
       
第二步:将C++写的DLL程序编译得到DLL文件,如MyDll.dll.
第三步:新建C#工程,并将MyDll.dll拷贝到C#工程.\bin\Debug目录下
第四步:在C#工程中声明MyDll及其中的导出函数,示例如下:
        [DllImport("MyDll",EntryPoint="Add",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
        public static extern int Add(int a,int b);
第五步:在C#工程类函数中调用Add函数测试是否成功调用

#c#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值