Arijeet Ghosh

Arijeet Ghosh

  • NA
  • 210
  • 33.6k

Marshaling code2 : Problem described below

Jul 1 2020 7:32 AM
Though I don't know VC++ have I've searched the net. Please check it...
 
The VC++ code:-
 
a)plus.h:
  1. #ifndef PLUS_H  
  2. #define PLUS_H  
  3. int __declspec(dllexport) add(int a, int b);  
  4. #endif  
b)plus.cpp:
  1. #include "pch.h"  
  2. #include "plus.h"  
  3. int add(int a, int b)  
  4. {  
  5. return a + b;  
  6. }  
Now in C#
  1. public partial class Form1 : Form  
  2. {  
  3. public Form1()  
  4. {  
  5. InitializeComponent();  
  6. }  
  7. private void button1_Click(object sender, EventArgs e)  
  8. {  
  9. Test t = new Test();  
  10. int fooPtr = Test.add(5, 9);  
  11. int LENGTH = 1;  
  12. var foo = new int[LENGTH];  
  13. /// Marshal.Copy(fooPtr, foo, 0, LENGTH);  
  14. MessageBox.Show(fooPtr.ToString());  
  15. }  
  16. }  
  17. public class Test  
  18. {  
  19. [DllImport("F:/TestMultipleProject/FirstDll/Debug/FirstDll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]  
  20. public static extern int add(int a, int b);  
  21. }  
Reference of VC ++ project is added to C# and they both reside in same solution
 
NOW PROBLEM is unable to find an entry point named add in the dll....