zoukankan      html  css  js  c++  java
  • c#调用C++库

    本人c#开发,有个项目要调用c++写好的dll库函数。捣腾了几天,把经验记录下。

    使用vs2010,非托管方式。

    c++:

    1、创建项目: Win32->Win32项目;选择DLL (D) ->完成;

    2、解决方案资源管理器中,鼠标右键项目,打开属性;

    3、创建类

    C#不能直接调用C++类库中的类,需要一种变通的解决方式,通过再做一个C++类库把要调用的类成员方法暴露出来,

    比如下面这个C++类:(类)

    //SampleCppClass.h                                                       //SampleCppClass.cpp

              

     再做一个类:(类似接口)调用SampleCppClass中的Add和Sub两个方法

     

    4、项目编译生成dll文件(c++项目鼠标右键,选择“重新生成”),一般在win32文件夹下方;

    c#

    1、在C#中,再调用SampleCppWrapper.dll中的公共方法:

    (首先要将生成的dll文件,放置c#项目debug下方)

    public class DllR{

    [DllImport("SampleCppWrapper.dll")]
    private static extern int Add(int n1, int n2);
    [DllImport("SampleCppWrapper.dll")]
    private static extern int Sub(int n1, int n2);
    }

    2、c#中调用 DllR.Add(1,1);

    为了调试能直接在c++库中设置断点,可以在c#项目属性中设置;

  • 相关阅读:
    ABAP-Spotlight on ABAP for SAP HANA – Again
    ABAP-ABAP Development
    ABAP-Performance Guidelines for ABAP Development on the SAP HANA Database
    ABAP-Getting Started with ABAP Core Data Services (CDS)
    ABAP-Technology and Runtime Environment
    ABAP-Test and Analysis Tools
    ABAP-Connectivity Wiki
    python爬虫
    python爬虫
    python爬虫
  • 原文地址:https://www.cnblogs.com/xiaohua19920/p/15625275.html
Copyright © 2011-2022 走看看