在.NET中调用C ++函数

时间:2018-06-22 10:46:28

标签: c# c++ .net dll dllimport

我有一个要在.NET中调用的c ++函数,但是出现错误

System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

这是我的c ++ dll代码

标题

#pragma once

#ifdef SIMPLEDEMO_EXPORTS
#define SIMPLEDEMO_API __declspec(dllexport)
#else
#define SIMPLEDEMO_API __declspec(dllimport)
#endif

// Increments the number passed to it by one
extern "C" SIMPLEDEMO_API int Increment(int a);

这是实现

// SimpleDemo.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "SImpleDemo.h"

SIMPLEDEMO_API int Increment(int a)
{
    return a + 1;
}

我已经构建了该文件,并将该dll与.NET可执行文件放置在相同的位置,并且在.NET客户端中有以下代码消耗了cpp dll

class Program
{
    [DllImport("SimpleDemo.dll")]
    public static extern int Incrememnt(int number);

    static void Main(string[] args)
    {
        Console.WriteLine(Incrememnt(1));

        // This is here so the .net app does not exit, to exist press any button when .net app is in focus
        Console.ReadLine();
    }
}

0 个答案:

没有答案