hej
Potrzebuje napisać wprapper z c++ do C# .
Napisałam prosty przykład metodę w c++ która zwraca mi tablice i mam problem w wywolaniem jej w c#. Builduje sie ok , ale problem pojawia sie w debugowaniu .. wyskakuje sie mi blad .
An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveEx ception' occurred in WrapperTab.exe
Additional information: Nie można zorganizować 'return value': Nieprawidłowa kombinacja typw zarządzanych/niezarządzanych.
Kod w c#. Form1.cs
[Kod]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WrapperTab
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Run();
}
public static void Run()
{
double[] dane = new double[30];
#region Dane
dane[0] = 2.3;
dane[1] = 2.4;
dane[2] = 4.5;
dane[3] = 5.5;
dane[4] = 5.5;
dane[5] = 6.7;
dane[6] = 5.6;
dane[7] = 4.5;
dane[8] = 5.5;
dane[9] = 4.4;
dane[10] = 2.3;
dane[11] = 2.3;
dane[12] = 4.5;
dane[13] = 5.5;
dane[14] = 5.5;
dane[15] = 6.7;
dane[16] = 2.4;
dane[17] = 4.5;
dane[18] = 5.5;
dane[19] = 5.5;
dane[20] = 6.7;
dane[21] = 5.6;
dane[22] = 4.5;
dane[23] = 5.5;
dane[24] = 4.4;
dane[25] = 2.3;
dane[26] = 2.3;
dane[27] = 4.5;
dane[28] = 5.5;
dane[29] = 5.5;
#endregion Dane
double[] wynik = new double[30];
wynik = UnsafeNativeMethods.fun(dane);
for (int i = 0; i < 30; i++)
{
MessageBox.Show(wynik[i].ToString());
}
}
}
}
Class1.cs
[Kod]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WrapperTab
{
internal static class UnsafeNativeMethods
{
const string dllLocation = "D:\\TestWrapperTab\\WrapperTab\\WrapperTab\\TestWrapperTab.dll";
[DllImport(dllLocation)]
public static extern double[] fun(double[] tab1);
}
}
Metoda w c++ :
[Kod]
#include "stdio.h"
#include <iostream>
extern "C"
{
__declspec(dllexport) double* fun(double tab1[30])
{
double tabWyn[30];
for(int i =0; i<30;i++)
{
tabWyn[i]=tab1[i] + 5;
}
return tabWyn;
}
}
Jeśli to możliwe prosiłabym o jakąś pomoc, good advice , jakies uwagi :)
Program wykłada mi sie przy wywolaniu metody :
wynik = UnsafeNativeMethods.fun(dane);