bjhmsoft_lhj 2021-08-05 22:54 采纳率: 0%
浏览 44

C# 调用C++的问题

C++ 的接口


```c++
typedef enum {
    YT_IMG_BGR_8UC3,
    YT_IMG_RGB_8UC3,
    YT_IMG_GRAY_8UC1,
    YT_IMG_DEPTH_16UC1,
} yt_img_type;

typedef struct yt_image_t {
    unsigned char *data;
    int width;
    int height;
    yt_img_type type;
} yt_image;

typedef struct yt_rect_t {
    int x;
    int y;
    int width;
    int height;
} yt_rect;

typedef struct yt_pointf_t {
    float x;
    float y;
} yt_pointf;
/**
 * @brief 追踪接口,适合于视频流检测,内部封装了检测、5点配准、稳定模块,具有内部状态
 *
 * @param[in] handle 实例句柄
 * @param[in] image 图片,图片类型:YT_IMG_BGR_8UC3
 * @param[out] tracked_faces 人脸追踪结果
 * @param[out] tracked_faces_count 检测到的人脸数量
 * @return YT_SUCCESS:成功,其他:失败
 */

YT_PUBLIC int yt_face_tracker_track(yt_handle handle, const yt_image image, yt_tracked_face **tracked_faces, int *tracked_faces_count);



C#代码

public enum yt_img_type
        {
            YT_IMG_BGR_8UC3,
            YT_IMG_RGB_8UC3,
            YT_IMG_GRAY_8UC1,
            YT_IMG_DEPTH_16UC1,
        }        
      
        public struct yt_image
        {
            public IntPtr data;
            public int width;
            public int height;
            public yt_img_type type;
        };
        public struct yt_pointf
        {
            float x;
            float y;
        }

        public struct yt_rect_t
        {
            int x;
            int y;
            int width;
            int height;
        }

        public struct yt_tracked_face_t
        {
            yt_pointf face_5_points;   
            yt_pointf face_rect;
            int frame_id;      ///< 帧ID
            int trace_id;      ///< 轨迹ID
            bool consecutive;  ///< 轨迹是否存在

            float pitch;  ///< 人脸俯仰角
            float yaw;    ///< 人脸偏航角
            float roll;   ///< 人脸翻滚角
        }



```c#
[DllImport(@"libYTFaceTracker.dll", CallingConvention = CallingConvention.Cdecl)]
       
private extern static int yt_face_tracker_detect(IntPtr handle, yt_image image, yt_face_tracker_param_t param, ref IntPtr yt_tracked_face, ref int tracked_faces_count);

Mat mat = new Mat(@"\_MG_0721.JPG");
 yt_image yt_img = new yt_image
 {
  data = mat.Data,
 width = 1616,
 height = 1077,
 type = yt_img_type.YT_IMG_BGR_8UC3
 };

 YTFaceTrackerHelper.yt_face_tracker_param_t param= new yt_face_tracker_param_t
 {
 min_face_size = 50,
 max_face_size = 100,
threshold =70.0f,
bigger_face_mode = 0,
  detect_interval = 6
  };
            yt_tracked_face_t s = new yt_tracked_face_t();
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(s));
            Marshal.StructureToPtr(s, ptr, false);
            int factcount = 0;
 result = face_tracker_detect(Handle, yt_img, param, ref ptr, ref factcount);

执行后提示错误:尝试读取或写入受保护的内存。这通常指示其他内存已损坏

  • 写回答

3条回答 默认 最新

  • 没事干写博客玩 2021-08-06 07:04
    关注

    能看看YT_PUBLIC吗,这个情况如果排除c++方法内指针动内存,那怀疑是dll弹栈时清理过了栈空间,c#这边的cdel又清理了一遍,栈被破坏后导致的

    评论

报告相同问题?

问题事件

  • 创建了问题 8月5日

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow