Bonjour,
Je récupère un pointeur [I][B]pointeurManaged [/B][/I]sur un structure non managée (déclarée et initialisée dans une dll) via une méthode Get_Adresse exportée.
[CODE]
// Code du C#
Init(0);
IntPtr pointeurManaged = (IntPtr)Get_Adresse(0);[/CODE]
[CODE]
// 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;
}
[/CODE]
je caste ensuite ce "[U][I]pointeurManaged[/I][/U]" de 2 façons différentes :
Méthode 1 :
[CODE]
DLL_TEST_STRUCT* pointeurManagedStructure = (DLL_TEST_STRUCT*)pointeurManaged;
[/CODE]
ET
Méthode 2 :
[CODE]
[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;
[/CODE]
Si je modifie la structure via le pointeur de la méthode 1, je modifie bien en mémoire la structure :
[CODE]
pointeurManagedStructure->Attribut_Bool1 = false;
pointeurManagedStructure->Attribut_Bool2 = false;
pointeurManagedStructure->Attribut_Short1 = 10;
pointeurManagedStructure->Attribut_Int1 = 1000;
pointeurManagedStructure->Attribut_Double1 = 10000;
[/CODE]
Par contre, j'ai beau modifié mon objet "[U][I]objectManaged [/I][/U]", je ne modifie pas en mémoire la structure non managée.
[CODE]
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);
}
}
[/CODE]
Pourquoi ? Est ce possible ?
Merci de votre aide.
Pascal