zoukankan      html  css  js  c++  java
  • C#如何引用C++的代码

    C++的Cpp文件

    #include <iostream>

    #define MODULEAPI
    #include 
    "tracewrite.h"

    using namespace std;

    void Func1() { cout<<"i am function 1"<<endl; }
    void Func2() { cout<<"i am function 2"<<endl; }
    void Func3() { cout<<"i am function 3"<<endl; }

    对应的头文件:(.h)

    #ifndef _MODULE_
    #define _MODULE_

    #ifndef MODULEAPI
    #define MODULEAPI __declspec(dllexport)
    #else
    #define MODULEAPI __declspec(dllimport)
    #endif

    //头文件

    #ifdef __cplusplus
    extern "C" {
    #endif

    MODULEAPI 
    void Func1();
    MODULEAPI 
    void Func2();
    MODULEAPI 
    void Func3();

    #ifdef MODULEAPI
       }
    #endif


    #endif // _MODULE_

    C#文件来引用上面的C++的方法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Func1();
            }

            [DllImport(
    "TestProj.dll")]
            
    private static extern void Func1();
        }
    }

    要引用上面的C++代码的C++代码:

    #include "stdafx.h"


    #include 
    "traceWrite.h"

    #pragma comment(lib , "TestProj.lib")

    void main()

    {

        Func1(); 
        Func2();

        
    //...

    }

    其对应的头文件如下:

    #ifndef _MODULE_
    #define _MODULE_

    #ifndef MODULEAPI
    #define MODULEAPI __declspec(dllexport)
    #else
    #define MODULEAPI __declspec(dllimport)
    #endif

    //头文件

    #ifdef __cplusplus
    extern "C" {
    #endif

    MODULEAPI 
    void Func1();
    MODULEAPI 
    void Func2();
    MODULEAPI 
    void Func3();

    #ifdef MODULEAPI
       }
    #endif


    #endif // _MODULE_
    做个快乐的自己。
  • 相关阅读:
    java读取ldif文件并创建新的节点
    AngularJS的基本概念和用法
    前端开发环境需要的工具
    解决:使用ajax验证登录信息返回前端页面时,当前整个页面刷新。
    js中switch语句不执行
    使用html5中required属性
    H-ui.admin v3.1学习之路(一):导航栏信息无法在内容区显示
    解决:@Auarowired为null
    scrapy框架整理
    django项目的部署
  • 原文地址:https://www.cnblogs.com/Jessy/p/2077546.html
Copyright © 2011-2022 走看看