Bonjour,
J'ai une structure que je dois marshaler :
Me penchant pour corriger un autre probleme à coté, quand je veux sauvegarder la structure dans un fichier,
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 public struct T_ACQUISITION2000 { public short sVersion; public short sVersionM; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)] public String tcPrestation; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)] public String tcConfiguration; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)] public String tcMarche; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public String tcNom; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)] public String tcInfo1; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)] public String tcInfo2; public int timeCreation; // date creation public uint uiNumero; // numero public uint uiDuree; // duree public long iArret; // motif arret // int public double dDeb; public double dFin; public uint uiNbEchantillon; public uint uiSize; public short sJournal; // meteo public short sTemps; public float fVent; public float fHumidite; public float fTemperature; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)] public byte[] RFU; [MarshalAs(UnmanagedType.Struct)] public T_ACQ rAcqTvm; public short sCirculation; // short public short sSens; // short [MarshalAs(UnmanagedType.Struct)] public T_ACQ rAcqConf; }
Au moment de convertir la structure en bytearray via ce code :
Quand je passe sur la ligne avec le commentaire, cela m'affiche cette exception :
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 public static byte[] StructureToByteArray<T>(object obj) { if(obj.GetType().Equals(typeof(string))) return Encoding.ASCII.GetBytes((string)obj); int len = Marshal.SizeOf(obj); byte[] arr = new byte[len]; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(obj, ptr, true); // Exception Marshal.Copy(ptr, arr, 0, len); Marshal.FreeHGlobal(ptr); return arr; }
Je n'arrive pas à comprendre d'où vient le probleme.Impossible de marshaler le type, car la longueur d'une instance de tableau incorporée ne correspond pas à la longueur déclarée dans la disposition.
Pourriez vous m'aider ?
Merci.
Flow.
Partager