2017-03-09 1 views
0

Ich verstehe C++ nicht viel, also füge ich den ganzen Code hinzu, aus dem dll gemacht wurde.Wie kann ich struct von C# verwaltet in C++ unmanaged DLL übergeben und struct im Ergebnis erhalten?

C++ ** ** Calculator.h

#ifndef CALCULATOR_H 
#define CALCULATOR_H 

#include "global.h" 

struct CALCULATORSHARED_EXPORT Input { 
    double a; 
    double b; 
}; 

struct CALCULATORSHARED_EXPORT Result { 
    double sum; 
    double diff; 
    double prod; 
    double div; 
}; 

global.h

#ifndef CALCULATOR_GLOBAL_H 
#define CALCULATOR_GLOBAL_H 

#if defined(CALCULATOR_EXPORTS) 
# define CALCULATORSHARED_EXPORT __declspec(dllexport) 
#else 
# define CALCULATORSHARED_EXPORT __declspec(dllimport) 
#endif 

#endif // CALCULATOR_GLOBAL_H 

calculator.h

#include "calculator.h" 

Result calculate(const Input& input) { 
    Result result; 

    result.sum = input.a + input.b; 
    result.diff = input.a - input.b; 
    result.prod = input.a * input.b; 
    result.div = input.a/input.b; 

    return result; 
} 

C#

[DllImport("Calculator.dll", CallingConvention = CallingConvention.Cdecl)] 
     public static extern Result calculate(Input input); 


     [StructLayout(LayoutKind.Sequential)] 

     public struct Input 
     { 
      public double a; 
      public double b; 
     }; 

     [StructLayout(LayoutKind.Sequential)] 

     public struct Result 
     {    
      public double sum; 
      public double diff; 
      public double prod; 
      public double div; 
     }; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Input input; 
      Result result; 
      input.a = 5; 
      input.b = 6; 
      result = calculate(input);  
     } 

Ich erhalte Unable to find an entry point named 'calculate' in DLL 'Calculator.dll'.

+0

Sind Sie tatsächlich exportieren 'calculate' von' Calculator.dll'? – GSerg

+0

@GSerg Ich verstehe nicht ganz, was Sie meinen, indem Sie aus Calculator.dll berechnen. Ich habe meine Frage mit allem C++ Code aktualisiert, von dem diese DLL gemacht wurde. – Arbaaz

Antwort

1

Sie zu tun haben:

extern "C" 
{ 
    CALCULATORSHARED_EXPORT Result calculate(const Input& input) 
    { 
    } 
} 

Sie brauchen nicht CALCULATORSHARED_EXPORT die beiden Strukturen (Input und Result) zu markieren.

Die extern "C" Sonst wird der Name der Funktion gemangelt, die CALCULATORSHARED_EXPORT sonst wird die Funktion nicht exportiert.

Und die # Unterschrift C sein muss:

public static extern Result calculate(ref Input input); 

weil in C++ es ein Input& ist.

Offensichtlich dann

result = calculate(ref input); 
+0

Perfekt! Vielen Dank! – Arbaaz

+0

können Sie mir bitte sagen, was wäre das C# -Aquivalent von 'std :: vector < double > c;'? Ich füge es der Eingabe zusammen mit Double a und b innerhalb der Eingabestruktur hinzu. Ich kann es als eine neue Frage veröffentlichen, wenn Sie das wollen. – Arbaaz

+1

@Arbaaz keine Hoffnung, einen 'Vektor <>' Marshalling. Übergeben Sie ein Array + Länge des Arrays – xanatos