0

dll C++

extern "C"  
{
     __declspec(dllexport) int mainfun()
     {
         return x;
     }
}

In C#

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mainfun();

I only know how to return and call one variable from C++ to C#. I am writing a program where i need to call two different varables in C# from c++ dll (like return x,y;). Please i need help.

EDIT1:

In C++

struct Point
{
    int count_err;
    int statement;
} obj;
extern "C"  
{
     __declspec(dllexport) Point mainfun()
     {
         return obj;
     }
}

In C#

[StructLayout(LayoutKind.Sequential)] 
public struct Point 
{ 
   public int errsize; 
   public int statmnt; 
} 
[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]    
public static extern Point mainfun();

errsize = mainfun();
statmnt = mainfun();

Here errsize is giving an error-"the name 'errsize' does not exist in the current context".. What to do?

EDIT2:

In C#

total_errors.Text = p.errsize.ToString();

giving same error-"the name 'p' does not exist in the current." context"

Abdul Arif
  • 45
  • 1
  • 10

2 Answers2

2

Define new struct or array of data. Something like this:

C++:

struct Point
{
    int count_err;
    int statement;
} obj;
extern "C"  
{
     __declspec(dllexport) Point mainfun()
     {
         return obj;
     }
}

C#:

[StructLayout(LayoutKind.Sequential)] 
public struct Point 
{ 
   public int errsize; 
   public int statmnt; 
} 
[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]    
public static extern Point mainfun();

Point p = mainfun();
var errsize = p.errsize;
var statmnt = p.statmnt;
Backs
  • 24,430
  • 5
  • 58
  • 85
  • - Can i write like this ---__declspec(dllexport) pair mainfun() { return make_pair(x,y); } – Abdul Arif Aug 19 '15 at 07:32
  • @AbdulArif as i know - no, because `pair` is not a struct, and you need to return pointers... much better to declare custom struct and return strongly declared types – Backs Aug 19 '15 at 07:35
  • 1
    @Backs Your code will throw an error of `type conversion`. Because your `mainfun()` is returning a `point` (structure) and return type is `int`. You may need to change that in order that to work. – Amnesh Goel Aug 19 '15 at 07:50
  • - Error i'm encountering error C2065: 'p' : undeclared identifier error C2228: left of '.x' must have class/struct/union What went wrong i wrote has it is? – Abdul Arif Aug 19 '15 at 08:15
  • @AbdulArif i changed declaration of Point, look at it – Backs Aug 19 '15 at 08:17
  • @AbdulArif do you use C++ or plain C? – Backs Aug 19 '15 at 08:18
  • I'm writing program in C++ – Abdul Arif Aug 19 '15 at 08:35
  • `[StructLayout(LayoutKind.Sequential)] public struct Point { public int errsize; public int statmnt; } [DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)] public static extern Point mainfun(); errsize = mainfun()` where errsize is giving an error the name 'errsize' does not exist in the current context – Abdul Arif Aug 19 '15 at 11:26
  • @AbdulArif add it in your question – Backs Aug 19 '15 at 11:30
  • Ok...got it..Thanks but how to use `var errsize` any where again in the program – Abdul Arif Aug 19 '15 at 12:10
  • @AbdulArif man, really? you should learn about fields and properties https://msdn.microsoft.com/en-us/library/vstudio/ms173118(v=vs.100).aspx – Backs Aug 19 '15 at 17:04
0

First understand that if you want to return more than one value from any function, then you will need an object which can hold multiple values like struct, class object, list etc. But in your case you cannot use LIST or KeyValuePairList of C# because you have direct dependency with C++ code.

So use structure which is same in both the platforms. Now first you need to create a suitable data structure and change the return type of mainfun() when you are calling it as follows..

public struct abc {
    public int a;
    public int b;
}

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern abc mainfun();

Now in your C++ library, add the data structure and change the function definition.

typedef struct {
    int a;
    int b;
} abc;

extern "C"  
{
     __declspec(dllexport) abc mainfun()
     {
         abc obj;
         obj.x = 1;
         obj.y = 2;
         return obj;
     }
}
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • - Error i'm encountering error C2065: 'obj' : undeclared identifier error C2228: left of '.a' must have class/struct/union What went wrong i wrote has it is? – Abdul Arif Aug 19 '15 at 08:14