Je fais un programme d'interface qui doit gérer à terme un programme central en c++. Pour ce faire je fais une interface graphique en c# et une dll C++ que j'importe.
Après lecture du tutorial sur l'interroperabilité j'ai utilisé la p/invoke ainsi:
code de la dll:
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 .h typedef struct DATAS { int * tabINT; int * tabBOOL; wchar_t **tabCHAR; }t_COMPUTDATA; .cpp t_COMPUTDATA *getStructureDataTest() { t_COMPUTDATA *data = new t_COMPUTDATA; data->tabINT= new int[size_set_datas]; data->tabBOOL= new int[size_set_commands]; data->tabCHAR= new char*[size_set_errors]; data->tabINT[0] = 12; data->tabINT[1] = 13; data->tabINT[2] = 222; data->tabBOOL[0]=0; data->tabBOOL[1]=1; data->tabBOOL[2]=0; data->tabCHAR[0]="Les sanglots longs de l'automne bercent mon coeur d'une langueur monotone"; return(data); }
dans le programme en c#
dans class FData:
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 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = (int)DATA.SIZE_SET_DATAS)] public Int32[] tabINT; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = (int)COMMAND.SIZE_SET_COMMANDS)] public Int32[] tabBOOL; [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)ERROR.SIZE_SET_ERRORS)] public IntPtr[] tabCHAR; public FData() { tabINT = new Int32[(int)DATA.SIZE_SET_DATAS]; tabBOOL = new Int32[(int)COMMAND.SIZE_SET_COMMANDS]; tabCHAR = new IntPtr[(int)ERROR.SIZE_SET_ERRORS]; } la fonction d'affichage: public void sreenFDatas() { Console.WriteLine("FData"); Console.WriteLine("FData.tabINT"); Console.WriteLine(tabINT[0]); Console.WriteLine(tabINT[1]); Console.WriteLine(tabINT[2]); Console.WriteLine("FData.tabBOOL"); Console.WriteLine(tabBOOL[0]); Console.WriteLine(tabBOOL[1]); Console.WriteLine(tabBOOL[2]); Console.WriteLine("FData.tabCHAR"); Console.WriteLine(tabCHAR[0]); } l'appel dans la winform fData = new FData(); IntPtr DataT = getStructureDataTest(); Marshal.PtrToStructure(DataT, fData);
Ce que j'obtient sur la console
FData.tabINT
59338008
59338032
59338052
FData.tabBOOL
196611
524697
13
FData.tabCHAR
222
Quelqu'un a une idée pour comprendre ce qui se passe? (conversion foireuse? écriture décalée de la mémoire? dois je remplacer les int32 par des int par exmple?). Merci d'avance!
Partager