Tagi na forum.

Windows 14206 SQL Server 2132
sieci 6796 Windows XP 1922
SQL 6578 Outlook 1838
SBS 3868 Uprawnienia 1777
Windows 2003 2781 IIS 1636
Windows Server 2588 Office 1516
DNS 2315 Skrypt 1499

pokaż wszystkie tagi na forum

Problem wrapper c++/ c#. Metoda zwracająca tablice

wiatrak11 2011-08-06 20:01:53
0
avatar
 
 
 
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);
 
tagi: C#   C++


kkokosa  2011-08-06 21:51:52 #1
0
avatar
 
 
Hej,
Niestety nie ma tak łatwo. .Netowy P/Invoke potrafi wiele, ale na pewno nie tak wprost jak próbujesz - nie możesz pamięci zaalokowanej w kodzie niezarządzalnym ot tak zwrócić do środowiska zarządzalnego. Poza tym funkcja fun jest błędna, zwraca tablicę lokalną. Jeżeli funkcja ma jedynie operować na tablicy przekazanej jako argument, to sprawa jest jednak prosta :


[Kod]
class Program
{
[DllImport("TestWrapperTab.dll")]
public static extern void fun_update([In, Out] double[] tab1);


static void Main(string[] args)
{
double[] dane = new double[30];

#region Dane
...
#endregion Dane

double[] wynik = new double[30];
fun_update(dane);
for (int i = 0; i < 30; i++)
{
Console.WriteLine(dane[i].ToString());
}
Console.ReadLine();
}



Oraz:


[Kod]
extern "C" __declspec(dllexport) void fun_update(double tab1[]);







[Kod]
__declspec(dllexport) void fun_update(double tab1[])
{
for(int i = 0; i<30; i++)
{
tab1[i] = tab1[i] + 5;
}
}




Jeżeli zaś koniecznie chcesz zwracać nową tablicę, to trzeba troszkę więcej fatygi.

Udziel odpowiedzi

avatar
Treść wpisu:

Zaloguj się lub Zarejestruj się aby wykonać tę czynność.

Idź na górę strony