Import DLL struct Attempted to read or write protected memory. This is often an

anbariota Messages postés 6 Date d'inscription lundi 26 novembre 2007 Statut Membre Dernière intervention 19 juillet 2010 - 19 juil. 2010 à 17:08
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 - 21 juil. 2010 à 16:38
Bonjour,
J'ai un Outil C# qui importe une DLL C.
En effet, j'ai une exception "Attempted to read or write protected memory. This is often an indication that other memory has been corrupted".
J'ai besoin d'une aide !!

Déjà, je vous montre la fonction en C avec une structure.

INT16 G_DECL G_DSO_GetWaveDescriptor (const WORD16 DeviceHandle,const char * const TraceIdentifier,WF_DESCRIPTOR * const Descriptor);

typedef struct
{
WORD32 SweepsPerAcq;
double HorizOffset;
char HorizUnit[48];
WORD8 TrigerTimeMinutes;
float AcqVertOffset;
WORD16 WaveSource;
} WF_DESCRIPTOR;

Dans le code C#, voici comment sont déclarées les fonctions de la DLL :

[DllImport("C:/WINDOWS/system32/w32gdso.dll")]
internal static extern Int16 G_DSO_GetWaveDescriptor(short device, string voix, ref WF_DESCRIPTOR Descriptor);

[StructLayout(LayoutKind.Sequential)]
public struct WF_DESCRIPTOR{

[MarshalAs(UnmanagedType.I4)]
public Int32 SweepsPerAcq;

[MarshalAs(UnmanagedType.R8)]
public Double HorizOffset;

[MarshalAs(UnmanagedType.ByValArray,SizeConst = 48)]
public string[] HorizUnit;

[MarshalAs(UnmanagedType.I1)]
public Byte TrigerTimeMinutes;

[MarshalAs(UnmanagedType.R4)]
public Single AcqVertOffset;

[MarshalAs(UnmanagedType.I2)]
public Int16 WaveSource;
}

Appel des fonctions :

WF_DESCRIPTOR DESCRIPTOR = new WF_DESCRIPTOR();
Int16 res = G_DSO_GetWaveDescriptor(0, "C1", ref DESCRIPTOR );


Big merci d'avance !!

3 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
19 juil. 2010 à 17:33
Bonjour,

A vue de nez je dirais que la déclaration de HorizUnit n'est pas bonne.
Si je ne me trompe pas, la structure C déclare un tableau de 48 chars, pas un tableau de chaines de caractères.

A priori ça donnerait plutot quelque chose de ce genre :

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=48)]
public string HorizUnit;


/*
coq
MVP Visual C#
CoqBlog
*/
0
anbariota Messages postés 6 Date d'inscription lundi 26 novembre 2007 Statut Membre Dernière intervention 19 juillet 2010
19 juil. 2010 à 17:51
Merci Coq c'est vrai ce que tu as dis mais j'ai eu cet erreur lorsque j'ai mis [MarshalAs(UnmanagedType.ByValTStr, SizeConst=48)]

Cannot marshal field 'BlockFormat' of type 'WF_DESCRIPTOR': Invalid managed/unmanaged type combination (Arrays fields must be paired with ByValArray or SafeArray).

C'est pour ca j'ai utilisé
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 48)]

et C'est pas logique !!
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
21 juil. 2010 à 16:38
Salut, est-tu sûr d'avoir bien fait comme le propose Coq, il n'utilise pas un champ tableau mais une simple chaine.

Une autre possibilité c'est ByValArray avec un tableau de char et l'attribut CharSet.Ansi pour la fonction.
0
Rejoignez-nous