Bonjour,
Je récupère un pointeur pointeurManaged sur un structure non managée (déclarée et initialisée dans une dll) via une méthode Get_Adresse exportée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 // Code du C# Init(0); IntPtr pointeurManaged = (IntPtr)Get_Adresse(0);je caste ensuite ce "pointeurManaged" de 2 façons différentes :
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 // Code de la DLL typedef struct _DLL_TEST_STRUCT { unsigned char Attribut_Bool1; unsigned char Attribut_Bool2; short Attribut_Short1; int Attribut_Int1; double Attribut_Double1; } DLL_TEST_STRUCT; int* Get_Adresse(int notused) { return (int*) Test_Struct; } void Init(int notusded) { Test_Struct = calloc(1, sizeof(DLL_TEST_STRUCT)); Test_Struct->Attribut_Bool1 = 1; Test_Struct->Attribut_Bool2 = 1; Test_Struct->Attribut_Short1 = 1; Test_Struct->Attribut_Int1 = 1; Test_Struct->Attribut_Double1 = 1; }
Méthode 1 :
ET
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 DLL_TEST_STRUCT* pointeurManagedStructure = (DLL_TEST_STRUCT*)pointeurManaged;
Méthode 2 :
Si je modifie la structure via le pointeur de la méthode 1, je modifie bien en mémoire la structure :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 [StructLayout(LayoutKind.Sequential, Pack = 4)] public unsafe struct CS_TEST_STRUCT { public bool Attribut_Bool1; public bool Attribut_Bool2; public short Attribut_Short1; public Int32 Attribut_Int1; public double Attribut_Double1; } object objectManaged = *(CS_TEST_STRUCT*)pointeurManaged;
Par contre, j'ai beau modifié mon objet "objectManaged ", je ne modifie pas en mémoire la structure non managée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 pointeurManagedStructure->Attribut_Bool1 = false; pointeurManagedStructure->Attribut_Bool2 = false; pointeurManagedStructure->Attribut_Short1 = 10; pointeurManagedStructure->Attribut_Int1 = 1000; pointeurManagedStructure->Attribut_Double1 = 10000;
Pourquoi ? Est ce possible ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 Type _type = objectManaged.GetType(); FieldInfo[] fields = _type.GetFields(); for (int j = 0; j < fields.Length; j++) { if (fields[j].FieldType.ToString() == "System.Int32") { fields[j].SetValue(objectManaged, 0); } }
Merci de votre aide.
Pascal
Partager