Exporting C++ dll to c# [closed]

限于喜欢 提交于 2019-12-23 06:40:25

问题


'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

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

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

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

  4. argument 2 cannot convert from 'int[]' to 'CvPoint[]'

  5. argument 3 cannot convert from 'int' to 'int[]'

  6. 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
)

回答1:


[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.



来源:https://stackoverflow.com/questions/14392468/exporting-c-dll-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!