I am having trouble getting this to work. I created a test solution(VS2015) with a c++ DLL Project, a c++ ConsoleApplication and a c# ConsoleApplication. Both Console Application call the a DLL function in their main method and print the result to the console. All projects build to the same directory.
This works fine in the c++ ConsoleApplication since I simply added a reference to the DLL-Project and included the header.
In the c# ConsoleApplication I cant get this to work. I know its unmanaged code and that I need to use DLLImport.
When I try wo run the c# app it raises a System.EntryPointNotFoundException on calling test(). And says the Entry point "test" could not be found.
So what am I doing wrong?
DLL.h
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API int test(void);
DLL.cpp
#include "DLL.h"
// This is an example of an exported function.
DLL_API int test(void)
{
return 42;
}
Which compiles to DLL.dll.
Programm.cs
using System;
using System.Runtime.InteropServices;
namespace DLLTestCSharp
{
class Program
{
[DllImport("DLL.dll")]
public static extern int test();
static void Main(string[] args)
{
Console.WriteLine(test());
Console.Read();
}
}
}
Which compiles to DLLTestCSharp.exe.