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_
    做个快乐的自己。
  • 相关阅读:
    osgEarath中elevation的tms切片写法
    3D数据格式
    osgearth调试源码无法打断点问题解决
    在软件中无法选择已经安装字体的解决方案
    [vb+mo] visual baisc 6.0 基于mapobjects 2.4 开发的数字化校园电子地图
    编译osg的vrml插件
    安全漏洞之grafanacve_2021_43798
    apk反编译工具dex2jar
    磁盘空间告急
    WEB漏洞扫描工具之OWASP ZAP
  • 原文地址:https://www.cnblogs.com/Jessy/p/2077546.html
Copyright © 2011-2022 走看看