Hi all,
I'm very new to C++ but have moderate experience with C#. I am trying to write a dll in C++ which I can then call methods from in my C# program.
I originally took some source code I found online that worked fine that was as follows:
C++ Header
1 2 3 4 5 6
#define DECLSPEC __declspec(dllexport)
extern"C"
{
DECLSPEC int Add(int a, int b);
}
C++ Code
1 2 3 4 5 6 7 8 9 10
// This is the main DLL file.
#include "stdafx.h"
#include "DllTest2.h"
#define DECLSPEC
externint Add(int a, int b)
{
return a + b;
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DllTest2Console
{
class Program
{
[DllImport("DllTest2.dll", CallingConvention=CallingConvention.Cdecl)]
staticexternint Add(int a, int b);
staticvoid Main(string[] args)
{
Console.WriteLine(Add(21, 21));
Console.ReadLine();
}
}
}
That code ran perfectly with an output of 42 as you would expect. I then added an 'echoInt' method, for which I tried to copy the exact syntax from the previous example. Here is the code with the echoInt method added.
C++ Header
1 2 3 4 5 6 7
#define DECLSPEC __declspec(dllexport)
extern"C"
{
DECLSPEC int Add(int a, int b);
DECLSPEC int echoInt(int shout);
}
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// This is the main DLL file.
#include "stdafx.h"
#include "DllTest2.h"
#define DECLSPEC
externint Add(int a, int b)
{
return a + b;
}
externint echoInt(int shout)
{
return shout;
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DllTest2Console
{
class Program
{
[DllImport("DllTest2.dll", CallingConvention=CallingConvention.Cdecl)]
staticexternint Add(int a, int b);
staticexternint echoInt(int shout);
staticvoid Main(string[] args)
{
Console.WriteLine(Add(21, 21));
Console.WriteLine(echoInt(17));
Console.ReadLine();
}
}
}
When I try and run this I get the following error:
An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll
Additional information: Could not load type 'DllTest2Console.Program' from assembly 'DllTest2Console, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'echoInt' has no implementation (no RVA).
This has me very confused as I used exactly the same syntax both before and after.
Im sure this is just me being a noob, but any help is appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DllTest2Console
{
class Program
{
[DllImport("DllTest2.dll", CallingConvention=CallingConvention.Cdecl)]
staticexternint Add(int a, int b);
[DllImport("DllTest2.dll", CallingConvention = CallingConvention.Cdecl)]
staticexternint echoInt(int shout);
staticvoid Main(string[] args)
{
Console.WriteLine(Add(21, 21));
Console.WriteLine(echoInt(17));
Console.ReadLine();
}
}
}
This is not a very elegant solution however, if I had 100 methods would I need to import 100 times or is there a more simple way of doing this? Again, any help is appreciated!