zoukankan      html  css  js  c++  java
  • C++如何调用C#编写的 DLL

    由于C#编绎出来的DLL不是计算机所能直接识别的二进制指令码,需要CLS进行再解释,说到这,我想有些朋友应该知道C#项目需要引用C++编写的DLL时,可以直接引用DLLMPORT来实现调用,而反向的话,C++项目却不能简单靠引用来使用C#编写的DLL。由于C++项目默认配置是没有公共语言运行支持的,因此我们需要更改一些配置,来实现C++项目对C#编写DLL的调用。具体如何操作,我会在接下来的文章中进行说明,以供大家参考。
    
    示例用C#类库文件
    1、打开Microsoft Visual Studio 2010,选择文件->新建->项目。 
    这里写图片描述 
    2、在新建项目窗口中选择其他语言->Visual C#->类库,设置名称:MathDLL,设置解决方案名:MathDLL。 
    这里写图片描述 
    3、单击确定完成项目创建 
    4、将类Class1重命名为”MathTest“ 
    5、为类MathTest添加以下代码:
    
    
    [csharp] view plain copy
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
      
    namespace MathDLL  
    {  
        public class MathTest  
        {  
            public int demoAdd(int x, int y)  
            {  
                int sum;  
                sum = x + y;  
                return sum;  
            }  
        }  
    }  
    
    C++客户端程序创建
    1、打开Microsoft Visual Studio 2010,选择文件->新建->项目。 
    这里写图片描述 
    
    2、在新建项目窗口中选择其他语言->Visual C++->Win 32控制台应用程序,设置名称:MathCon,设置解决方案名:MathCon。
    
    
    
    3、单击确定,在出现的Win32 应用程序向导的概述对话框中点击下一步。
    
    这里写图片描述
    
    4、在应用程序设置中,选择应用程序类型下的控制台应用程序,勾选附加选项下的”预编译头“。
    
    
    5、单击完成创建项目
    
    6、将C#编写的DLL文件放置在C++的可执行程序目录
    
    7、使用#using引用C#编写的DLL文件MathDLL.dll,具体代码如下:
    
    
    [cpp] view plain copy
    #include "stdafx.h"  
    #using "../debug/MathDLL.dll"  
    using namespace MathDLL;  
    int _tmain(int argc, _TCHAR* argv[])  
    {  
        int sum,x,y;  
        x=10;  
        y=22;  
    MathTest ^a = gcnew MathTest();  
            sum=a->demoAdd(x,y);  
        sum=x+y;  
    [cpp] view plain copy
    printf("计算结果:%d",sum);  
        return 0;  
    }  
    
    8、配置C++项目MathCon的属性配置:添加公共语言运行支持。
    
    
    9、运行结果如下:
  • 相关阅读:
    leetcode_357. Count Numbers with Unique Digits
    leetcode_712. Minimum ASCII Delete Sum for Two Strings
    leetcode_865. Smallest Subtree with all the Deepest Nodes
    leetcode_919. Complete Binary Tree Inserter
    leetcode_1014. Capacity To Ship Packages Within D Days
    leetcode_998. Maximum Binary Tree II
    leetcode_654. Maximum Binary Tree
    leetcode_894. All Possible Full Binary Trees
    leetcode_486. Predict the Winner
    二叉树的遍历
  • 原文地址:https://www.cnblogs.com/profession/p/7427393.html
Copyright © 2011-2022 走看看