How to import C++ dll?

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 

extern int Add(int a, int b)
{
	return a + b;
}


C# Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)]
        static extern int Add(int a, int b);

        static void 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 

extern int Add(int a, int b)
{
	return a + b;
}

extern int echoInt(int shout)
{
	return shout;
}


C# Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)]
        static extern int Add(int a, int b);
        static extern int echoInt(int shout);

        static void 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.

Thanks!
After intensive experimentation I have found a solution that works.

By importing the dll above EVERY line in C# program I solve the problem as it appears it only recognizes the static extern etc. directly below it.

The new C# code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)]
        static extern int Add(int a, int b);

        [DllImport("DllTest2.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int echoInt(int shout);
    

        static void 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!
Topic archived. No new replies allowed.