Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
.NET Framework
[C#] Passaggio Struct libreria C++
venerdì 06 aprile 2007 - 10.25
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
spikome
Profilo
| Newbie
1
messaggi | Data Invio:
ven 6 apr 2007 - 10:25
Slave gente,
ho un problema con il passaggio di una Struct ad una libreria scritta in c++ spero mi aiutiate a trovare la strada giusta. nel dettaglio:
ho una dll scritta in C++ che esporta la seguente funzione:
extern "C" __declspec(dllexport) bool recv_pkt( Packet& RX );
dove Packet è una struttura del tipo:
typedef struct {
unsigned char Len;
unsigned char Len_;
unsigned char Type;
unsigned char Data[250];
unsigned char Crc;
unsigned char Crc_;
} Packet;
Ho importato correttamente la libreria in C# ma adesso mi trovo davanti al problema di creare la stessa struttura Packet in C# e passarne il riferimento.
Ho creato una struttura del tipo:
[StructLayout(LayoutKind.Sequential)]
struct Packet {
byte Len;
byte Len_;
byte Type;
byte[] Data;
byte Crc;
byte Crc_;
} ;
[DllImport("MyDll.dll", EntryPoint = "recv_pkt")]
public static extern bool recv_pkt( [MarshalAs(UnmanagedType.Struct)]ref Packet RX);
poi:
Packet RX = new Packet();
RX.Data = new byte[250];
bool finish = recv_pkt(ref RX);
Il problema è che non tutti i campi del pacchetto vengono riempiti correttamente (problema con i riferimenti credo) e quando la dll va a riempire il vettore Data[] viene sollevata l'eccezione:
SafeArrayTypeMismatchException
Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.
Spero riusciate a dirmi qual'è il problema.
ringrazio anticipatamente!
munissor
Profilo
| Senior Member
270
messaggi | Data Invio:
mar 10 apr 2007 - 18:39
Aggiungi attributo MarshalAs al vettore...prova qual'è il valore più opportuno per il tipo, poi imposta SizeConst=250.
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !