导出C ++的dll到C#

编程入门 行业动态 更新时间:2024-10-26 10:36:01
本文介绍了导出C ++的dll到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用系统; '使用System.Collections.Generic; '使用System.Drawing中; '使用System.Windows.Forms的; '使用Emgu.CV; '使用Emgu.CV.Structure; '使用Emgu.CV.CvEnum; '使用System.IO; '使用System.Diagnostics程序; '使用System.Runtime.InteropServices; '命名空间FaceStat '{'Form1类:表格 { //公共静态外部无效AsmSearchDll(OUT INT nlmarks,OUT INT lmarks ,串imgname,字节imgdata,诠释的宽度,高度INT,INT is_color,串conf_file0,串conf_file1); 函数[DllImport(@../数据/ stasm_dll.dll)] 公共静态外部无效AsmSearchDll( OUT INT pnlandmarks,//出来:一些标志性建筑,0若能吨得到里程碑式的 OUT INT []地标,//出来:地标,来电必须分配 [的MarshalAs(UnmanagedType.LPStr)字符串IMAGE_NAME,//在:内部错误消息中使用,如果需要的话 [的MarshalAs(UnmanagedType.LPStr)字符串IMAGE_DATA,//在:图像数据,每像素3字节,如果is_color INT宽度,//在:图像 INT高度的宽度//:影像 INT is_color的高度,//:1,如果RGB图像,0,如果灰度 [的MarshalAs(UnmanagedType.LPStr)字符串conf_file0,//在:1配置文件名,NULL默认 [的MarshalAs(UnmanagedType.LPStr)字符串conf_file1 //在:第2配置文件名,NULL默认,为没有); 静态无效的主要(字串[] args) {串IMAGE_NAME =../data/Chupul.jpg; 的IplImage IMG = cvlib.cvLoadImage(IMAGE_NAME,cvlib.CV_LOAD_IMAGE_COLOR); 如果(IMG == NULL) { Console.Write(错误:无法打开图像{0} \\\,IMAGE_NAME); 的回报; } INT nlandmarks; INT [] =标志性建筑新INT [500]; //为X空间,多达250个地标 AsmSearchDll的ŸCOORDS(出nlandmarks,一些重要的标志,IMAGE_NAME,img.imageData,img.width,img.height,1,NULL,NULL); 如果(nlandmarks == 0) { Console.Write(\\\Error:无法找到{0} \\\ landmmarks,IMAGE_NAME); 的回报; } INT [] P =标志; cvlib.cvPolyLine(IMG,P,nlandmarks,1,真实,cvlib.CV_RGB(255,0,0)); cvlib.cvShowImage(ASM榜样,IMG); cvlib.cvWaitKey(0); cvlib.cvDestroyWindow(ASM例如); cvlib.cvReleaseImage(IMG); 的回报; } } '}

我有 6个错误

  • 有关'FaceStat.Form1.AsmSearchDll的最佳重载的方法匹配(OUT INT,出INT [],字符串,字符串,INT,INT,INT,字符串,字符串)'有一些无效参数

  • 4的说法无法从转换'System.IntPtr 到串

  • 为最佳重载方法匹配'cvlib.cvPolyLine(CvArr,CvPoint [],INT [],INT,INT,CvScalar) 有一些无效参数

  • 参数2不能从转换'INT []'到'CvPoint []

  • 参数3无法从'诠释'转换为'廉政[]

  • 参数5:无法从布尔转换为 INT

  • 感谢

    的C ++ DLL

    无效AsmSearchDll( 为int * pnlandmarks,//出来:一些地标0,如果不能获得地标 INT地标[],//出来:地标,调用者必须在分配 为const char IMAGE_NAME [],//:在内部错误消息中使用,如果需要的话 为const char IMAGE_DATA [],//在:图像数据,每像素3字节,如果is_color const int的宽度,在// :图像 const int的高度宽度,//在:图像 const int的is_color的高度,//在:1,如果RGB图像,0,如果灰度 为const char conf_file0 [],//在:1配置文件名,NULL默认 为const char conf_file1 [] //中:第2配置文件名,NULL默认,如果没有)

    解决方案

    函数[DllImport(@../数据/ stasm_dll.dll)] 内部静态外部无效AsmSearchDll ( [出]出的Int32 pnlandmarks,〔输出]出的Int32 []地标, [中,的MarshalAs(UnmanagedType.LPStr)]字符串IMAGE_NAME, [中,的MarshalAs(UnmanagedType.LPStr)]字符串IMAGE_DATA, [IN]的Int32宽度, [IN]的Int32高度, [IN]的Int32 is_color, [中,的MarshalAs(UnmanagedType.LPStr)]字符串conf_file0, [中,的MarshalAs(UnmanagedType.LPStr)]字符串conf_file1 ); 的IplImage IMG = cvlib.cvLoadImage(IMAGE_NAME,cvlib.CV_LOAD_IMAGE_COLOR); 字符串为imageData = Marshal.PtrToStringAnsi(img.imageData); AsmSearchDll(出nlandmarks,一些重要的标志,IMAGE_NAME,为imageData,img.width,img.height,1,NULL,NULL);

    您没有指定cvPolyLine ...所以我不能检查参数。反正......你只是传递错误的值类型的方法。像这样简单。

    'using System; 'using System.Collections.Generic; 'using System.Drawing; 'using System.Windows.Forms; 'using Emgu.CV; 'using Emgu.CV.Structure; 'using Emgu.CV.CvEnum; 'using System.IO; 'using System.Diagnostics; 'using System.Runtime.InteropServices; 'namespace FaceStat '{ 'class Form1 : Form { // public static extern void AsmSearchDll(out int nlmarks, out int lmarks, string imgname, byte imgdata, int width, int height, int is_color, string conf_file0, string conf_file1); [DllImport(@"../data/stasm_dll.dll")] public static extern void AsmSearchDll( out int pnlandmarks, // out: number of landmarks, 0 if can't get landmark out int[] landmarks, // out: the landmarks, caller must allocate [MarshalAs(UnmanagedType.LPStr)]string image_name, // in: used in internal error messages, if necessary [MarshalAs(UnmanagedType.LPStr)]string image_data, // in: image data, 3 bytes per pixel if is_color int width, // in: the width of the image int height, // in: the height of the image int is_color, // in: 1 if RGB image, 0 if grayscale [MarshalAs(UnmanagedType.LPStr)]string conf_file0, // in: 1st config filename, NULL for default [MarshalAs(UnmanagedType.LPStr)]string conf_file1 // in: 2nd config filename, NULL for default, "" for none ); static void Main(string[] args) { string image_name = "../data/Chupul.jpg"; IplImage img = cvlib.cvLoadImage(image_name, cvlib.CV_LOAD_IMAGE_COLOR); if (img == null) { Console.Write("Error: Can't open the image. {0}\n", image_name); return; } int nlandmarks; int[] landmarks = new int[500]; // space for x,y coords of up to 250 landmarks AsmSearchDll(out nlandmarks, out landmarks, image_name, img.imageData, img.width, img.height, 1, null, null); if (nlandmarks == 0) { Console.Write("\nError: Cannot locate landmmarks in {0}\n", image_name); return; } int[] p = landmarks; cvlib.cvPolyLine(img, p, nlandmarks, 1, true, cvlib.CV_RGB(255,0,0)); cvlib.cvShowImage("Asm example", img); cvlib.cvWaitKey(0); cvlib.cvDestroyWindow("Asm example"); cvlib.cvReleaseImage(img); return; } } '}

    I've got 6 errors

  • The best overloaded method match for 'FaceStat.Form1.AsmSearchDll(out int, out int[], string, string, int, int, int, string, string)' has some invalid arguments

  • argument 4 cannot convert from 'System.IntPtr' to 'string'

  • The best overloaded method match for 'cvlib.cvPolyLine(CvArr, CvPoint[], int[], int, int, CvScalar)' has some invalid arguments

  • argument 2 cannot convert from 'int[]' to 'CvPoint[]'

  • argument 3 cannot convert from 'int' to 'int[]'

  • Argument 5: cannot convert from 'bool' to 'int'

  • Thanks

    The C++ dll

    void AsmSearchDll ( int *pnlandmarks, // out: number of landmarks, 0 if can't get landmarks int landmarks[], // out: the landmarks, caller must allocate const char image_name[], // in: used in internal error messages, if necessary const char image_data[], // in: image data, 3 bytes per pixel if is_color const int width, // in: the width of the image const int height, // in: the height of the image const int is_color, // in: 1 if RGB image, 0 if grayscale const char conf_file0[], // in: 1st config filename, NULL for default const char conf_file1[] // in: 2nd config filename, NULL for default, "" if none )

    解决方案

    [DllImport(@"../data/stasm_dll.dll")] internal static extern void AsmSearchDll ( [Out] out Int32 pnlandmarks, [Out] out Int32[] landmarks, [In, MarshalAs(UnmanagedType.LPStr)] String image_name, [In, MarshalAs(UnmanagedType.LPStr)] String image_data, [In] Int32 width, [In] Int32 height, [In] Int32 is_color, [In, MarshalAs(UnmanagedType.LPStr)] String conf_file0, [In, MarshalAs(UnmanagedType.LPStr)] String conf_file1 ); IplImage img = cvlib.cvLoadImage(image_name, cvlib.CV_LOAD_IMAGE_COLOR); String imageData = Marshal.PtrToStringAnsi(img.imageData); AsmSearchDll(out nlandmarks, out landmarks, image_name, imageData, img.width, img.height, 1, null, null);

    You didn't specified cvPolyLine... so I can't check arguments. Anyway... you are just passing wrong value types to your method. As simple as this.

    更多推荐

    导出C ++的dll到C#

    本文发布于:2023-06-10 18:31:12,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/616426.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:dll

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!