Probl�me avec Marshal.PtrToStructure
Salut,
Voil�, j'ai le probl�me suivant. :cry:
Code:
Marshal.PtrToStructure(ptrErr, eContext);
me soul�ve une exception de type 'System.AccessViolationException'
Ci dessous, le code ou l'exception est g�n�r�e:
Code:
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);}
} |
la fonction InitImage renvoie un IntPtr de type ErrorContext et prend une s�rie d'arguments dont un 'out' de type IntPtr.
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
Code:
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;
} |
:merci: d'avance pour votre aide pr�cieuse