Struct avec tableau de struc

mouloud42 Messages postés 3 Date d'inscription vendredi 21 novembre 2008 Statut Membre Dernière intervention 11 mai 2009 - 11 mai 2009 à 20:04
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 - 12 mai 2009 à 19:43
salut,

j'ai un pb de compilation quand je met ce code, à chaque déclarations le compilo m'envoie boulé quand c'est pas une varialb c'est l'autre, un calvaire, ça fait 2 jours que je bloque là, en C ça marche mais en C# je mouline, si vous aver une idée. Merci
//structure d'un octect accessible bit à bit
[System.Runtime.InteropServices.
StructLayout(
LayoutKind.Explicit)] 

struct
byte_bool_struct
{ 
[System.Runtime.InteropServices.
FieldOffset(0)] 

BitArray b = 
new
BitArray(8);                                        
[System.Runtime.InteropServices.
FieldOffset(0)] 

byte octet;
};

//structure mise corélation entre les infos accessibles 
//du programme et le Buffer envoyé a l'USB
[System.Runtime.InteropServices.
StructLayout(
LayoutKind.Explicit)] 

struct
Send_USB
{ 
[System.Runtime.InteropServices.
FieldOffset(0)] 

byte Always_Zero;
[System.Runtime.InteropServices.
FieldOffset(1)] 

fixed
byte_bool_struct digital[8]; 
[System.Runtime.InteropServices.
FieldOffset(9)] 

Int16[] ana = 
new
Int16[8];
[System.Runtime.InteropServices.
FieldOffset(25)]

byte LCD_command;
[System.Runtime.InteropServices.
FieldOffset(26)]

byte[] LCD_charatere_ligne = 
new
Byte[20];
[System.Runtime.InteropServices.
FieldOffset(26)]

string LCD_ligne;
[System.Runtime.InteropServices.
FieldOffset(0)]

byte[] Buffer = 
new
Byte[65];
};

//structure mise corélation entrees les infos accessibles 
//du programme et le Buffer lu sur l'USB
[System.Runtime.InteropServices.
StructLayout(
LayoutKind.Explicit)] 

struct
Receive_USB
{ 
[System.Runtime.InteropServices.
FieldOffset(0)] 

byte_bool_struct digital[8]; 
[System.Runtime.InteropServices.
FieldOffset(8)]

Int16[] ana = 
new
Int16[8];
[System.Runtime.InteropServices.
FieldOffset(0)]

byte[] Buffer = 
new
Byte[64];
};

//déclaration
public
Send_USB TX_Report;

public
Receive_USB RX_Report;
A voir également:

2 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
12 mai 2009 à 19:16
Salut, il y'a beaucoup d'erreurs, apparemment tu ne sais pas marshaller les tableaux en C#.. Les tableaux ne sont pas inlines c'est des références ( des pointeurs ) , donc dans une structure, peu importe sa taille un tableau fait toujours 4 ou 8 octets.. On ne peut pas initialiser directement le tableau dans la structure.. il faut soit utiliser l'attribut MarshalAs pour simuler un tableau inline soit utiliser du code unsafe avec le mot clé fixed pour définir un vrai tableau inline.. etc..
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
12 mai 2009 à 19:43
Un exemple pour mieux comprendre :

[ StructLayout( LayoutKind.Sequential ) ]
public struct USB1
{
    [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 8 ) ]
    public byte[ ] Buffer;
}

// Compile uniquement en mode unsafe.
[ StructLayout( LayoutKind.Explicit ) ]
public unsafe struct USB2
{
    [ FieldOffset( 0 ) ]
    public fixed byte Buffer[ 8 ];
    [ FieldOffset( 0 ) ]
    public long Data;
}

MessageBox.Show( String.Format
(
    "USB1 Size: {0} USB2 Size: {1} Buffer Offset: {2} Data Offset: {3}",
    Marshal.SizeOf( typeof( USB1 ) ),
    Marshal.SizeOf( typeof( USB2 ) ),
    Marshal.OffsetOf( typeof( USB2 ), "Buffer" ),
    Marshal.OffsetOf( typeof( USB2 ), "Data" ) )
);
0
Rejoignez-nous