Calling c++ function in .NET

0

I have a c++ function that I would like to call in .NET however I get an error

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

Here is my c++ dll code

Header

#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);

and here is the implementation

// 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;
}

I have built this and have placed the dll in the same location as my .NET executable and have the following code in my .NET client consuming the 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();
    }
}
c#
c++
.net
dll
dllimport
asked on Stack Overflow Jun 22, 2018 by DorkMonstuh

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0