Plus précisement, je cherche à manipuler ces structures imbriquées :
Pour cela j'utilise les fonctions suivantes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public struct stInformationsMOXA { public bool bEtatConnexion; public char[] szAdresseIP; public int nNumeroPort; } public struct stInformationsSILDB { public bool bEtatConnexion; public char[] szNomBase; } public enum ListeTypeCommunication { eComHousseuse, eComSILDB }; public enum ListeEtatPalette { ePaletteEntreeHousseuse, ePaletteSortieHousseuse }; public struct stDonneesEchangees { public ListeTypeCommunication TypeCommunication; public DateTime date; public char[] szDonneesBrutes;// = new char[200]; public int nNumPalette; public char[] szNumeroOF; // = new char[20]; public int nCodeHoussage; public ListeEtatPalette EtatPalette; } public struct stDonneesIHM { public stInformationsMOXA InformationsMOXA; public stInformationsSILDB InformationsSILDB; public stDonneesEchangees[] DonneesEchangees; public int nIndexDernierstDonneesEchangees; }
que j'utilise de cette manière :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public static byte[] ecrire(object St) { int rawsize = Marshal.SizeOf(St); IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.StructureToPtr(St, buffer, false); byte[] rawdatas = new byte[rawsize]; Marshal.Copy(buffer, rawdatas, 0, rawsize); Marshal.FreeHGlobal(buffer); return rawdatas; } public static stDonneesIHM lire(BinaryReader br) { stDonneesIHM Str = new stDonneesIHM(); byte[] buff = br.ReadBytes(Marshal.SizeOf(Str)); int rawsize = Marshal.SizeOf(Str); if (rawsize > buff.Length) return Str; IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.Copy(buff, 0, buffer, rawsize); Str = (stDonneesIHM)Marshal.PtrToStructure(buffer, Str.GetType()); Marshal.FreeHGlobal(buffer); return Str; }
Le code est généré sans aucune erreur ni warning, par contre à l'execution, j'ai une erreur "Specified array was not of the expected type." sur la ligne "Marshal.StructureToPtr(St, buffer, false);" (en gras dans le code) de la fonction ecrire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 // Initialisation des éléments graphiques InitializeComponent(); //création des structures de données stInformationsMOXA InfoMOXA = new stInformationsMOXA(); InfoMOXA.szAdresseIP = new char[20]; stInformationsSILDB InfoSILDB = new stInformationsSILDB(); InfoSILDB.szNomBase = new char[20]; stDonneesEchangees DonEch = new stDonneesEchangees(); DonEch.szDonneesBrutes = new char[200]; DonEch.szNumeroOF = new char[20]; stDonneesIHM DonIHM = new stDonneesIHM(); DonIHM.DonneesEchangees = new stDonneesEchangees[200]; //remplissage des sturctures InfoMOXA.bEtatConnexion = true; InfoMOXA.nNumeroPort = 6; String AdIP = "gsd4565"; InfoMOXA.szAdresseIP = AdIP.ToCharArray(); InfoSILDB.bEtatConnexion = false; String NomBD = "vsds"; InfoSILDB.szNomBase = NomBD.ToCharArray(); DonEch.date = DateTime.Now; DonEch.EtatPalette = ListeEtatPalette.ePaletteEntreeHousseuse; DonEch.nCodeHoussage = 2; DonEch.nNumPalette = 25; String DonBr = "dgzgzgr"; DonEch.szDonneesBrutes = DonBr.ToCharArray(); String NumOF = "dg4"; DonEch.szNumeroOF = NumOF.ToCharArray(); DonEch.TypeCommunication = ListeTypeCommunication.eComHousseuse; DonIHM.DonneesEchangees[0] = DonEch; DonIHM.InformationsMOXA = InfoMOXA; DonIHM.InformationsSILDB = InfoSILDB; DonIHM.nIndexDernierstDonneesEchangees = 0; //gestion fichiers binaires FileStream fw = null; FileStream fr = null; BinaryReader br = null; BinaryWriter bw = null; try { // si Le fichier n'existe pas, on le crée, sinon ouverture fw = File.Create("H:/DonIHM.bin"); bw = new BinaryWriter(fw); bw.Write(ecrire(InfoMOXA)); bw.Close(); fw.Close(); // Ouverture du contenu du fichier fr = File.Open("H:/DonIHM.bin", FileMode.Open); br = new BinaryReader(fr); stInformationsMOXA stInfoMOXA = lire(br); LB_Data.Items.Add("MOXA connectée : " + stInfoMOXA.bEtatConnexion); LB_Data.Items.Add("Numero port : " + stInfoMOXA.nNumeroPort); LB_Data.Items.Add("adresse IP : " + stInfoMOXA.szAdresseIP); fr.Close(); br.Close(); } finally { if (br != null) br.Close(); if (bw != null) bw.Close(); }
Cela vient-il du fait que j'ai des structures imbriquées ?
Comment faire dans mon cas particulier ?
Partager