Salut,
Voilà, j'ai le problème suivant.
me soulève une exception de type 'System.AccessViolationException'
Code : Sélectionner tout - Visualiser dans une fenêtre à part Marshal.PtrToStructure(ptrErr, eContext);
Ci dessous, le code ou l'exception est générée:
la fonction InitImage renvoie un IntPtr de type ErrorContext et prend une série d'arguments dont un 'out' de type IntPtr.
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 [DllImport(@"madll.dll", EntryPoint = "InitImage", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr InitImage(IntPtr ptrRaw, int width, int height, int resolution, int rank, out IntPtr ptrOut); static void Main(string[] args) { Bitmap bmp= new Bitmap(@"Image.bmp"); byte[] raw = GetBytes(bmp); IntPtr ptrRaw = Marshal.AllocHGlobal(Marshal.SizeOf(raw[0]) * raw.Length); Marshal.Copy(raw, 0, ptrRaw, raw.Length); QualityAssessment qa = new QualityAssessment(); qa.imgData = raw; qa.imgHeight = bmp.Height; qa.imgWidth = bmp.Width; qa.imgResolution = 500; qa.fingerRank = 0; qa.initok = 123; qa.st = QualityAssessment.Type.fingerPrint; IntPtr ptrOut = Marshal.AllocHGlobal(Marshal.SizeOf(qa)); Marshal.StructureToPtr(qa, ptrOut, false); ErrorContext eContext = new ErrorContext(); IntPtr ptrErr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ErrorContext))); ptrErr = InitImage(ptrRaw, bmp.Width, bmp.Height, 500, 1, out ptrOut); try { QualityAssessment2 qa = new QualityAssessment2(); Marshal.PtrToStructure(ptrOut, qa); Console.WriteLine("Finger height : {0}", qa.imgHeight); Console.WriteLine("St : {0}", qa.st); //Une exception de type System.AccessViolationException est levée Marshal.PtrToStructure(ptrErr, eContext); } catch(Exception e) {Console.WriteLine(e.Message);} }
J'arrive à récupérer correctement les informations de ptrOut, le parametre out, mais pas ceux de ptrErr.
Voici la définition des classes ErrorContext et QualityAssessment
d'avance pour votre aide précieuse
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 [StructLayout(LayoutKind.Sequential)] class ErrorContext { [MarshalAs(UnmanagedType.I4)] public Returned_Code returnedCode;//en référence à une énumération [MarshalAs(UnmanagedType.LPStr, SizeConst = 100)] public String errorMessage; [MarshalAs(UnmanagedType.LPStr, SizeConst = 256)] public String errorFile; [MarshalAs(UnmanagedType.U4)] public UInt32 errorLine; } [StructLayout(LayoutKind.Sequential)] public class QualityAssessment { public enum structType { fingerPrint = 111, slap = 222, thumbs = 333 } public byte[] imgData = new byte[525420]; public Int32 imgWidth; public Int32 imgHeight; public Int32 imgResolution; public sbyte fingerRank; public Int32 initok; public structType st; }
Partager