Определение заголовка
c ++:
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
Функция c ++:
int Detect(Pos* pfps);
Определение структуры c ++:
struct FaceAngle
{
int yaw;
int pitch;
int roll;
float confidence;
};
typedef struct tagPOINT
{
LONG x;
LONG y;
}
POINT, * PPOINT, NEAR * NPPOINT, FAR * LPPOINT;
struct Pos
{
RECT rcFace;
POINT ptNose;
FaceAngle fAngle;
int nQuality;
BYTE pFacialData[512];
Pos()
{
memset(&rcFace, 0, sizeof(RECT));
memset(&ptNose, 0, sizeof(POINT));
memset(&fAngle, 0, sizeof(FaceAngle));
nQuality = 0;
memset(pFacialData, 0, 512);
}
};
c ++ Телефонный код:
void detect()
{
Pos ptfp[10];
Detect(ptfp);
}
================================================================================
Определение функции c #:
[DllImport(THFaceDLLName, EntryPoint = "Detect")]
public static extern int Detect([In, Out] Pos[] posArray);
c # structопределение:
[System.Runtime.InteropServices.StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet = System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
public struct Pos
{
public RECT rcFace;
public POINT ptNose;
public FaceAngle fAngle;
int nQuality;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
byte[] pFacialData;
};
[System.Runtime.InteropServices.StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[System.Runtime.InteropServices.StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
public struct POINT
{
public int x;
public int y;
}
[System.Runtime.InteropServices.StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)]
public struct FaceAngle
{
int yaw;
int pitch;
int roll;
float confidence;
};
c # код вызова:
void detect()
{
Pos[] facePosArray = new Pos[10];
int faceNum = Detect(facePosArray);
}
Исключение «System.AccessViolationException» возникает после выполнения функции обнаружения, должен ли я преобразовать Pos [] в Intptr?может у кого есть хорошее решение