using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.InteropServices; namespace BLL { [UnmanagedFunctionPointer(CallingConvention.StdCall)] public unsafe delegate void CheckCallBack( string label, double rongcha ); public class libOpenCV { [DllImport("LibOpenCV.dll",CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)] private extern static void CheckImage(IntPtr path, [MarshalAs(UnmanagedType.FunctionPtr)] CheckCallBack cb); [DllImport("LibOpenCV.dll",CallingConvention=CallingConvention.StdCall)] private extern static int TrainImage(IntPtr picpath,IntPtr label); public delegate int CheckImageDG(IntPtr imagePath, CheckCallBack ccb); public static void DoCheckImage(string picpath, CheckCallBack ccb) { IntPtr ptrPicPath = mallocIntptr(picpath); CheckImage(ptrPicPath,ccb);;// Marshal.GetFunctionPointerForDelegate(ccb)); Marshal.FreeHGlobal(ptrPicPath); } public static int DoTrainImage(string picpath, string label) { int ret = 0; IntPtr ptrPicPath = mallocIntptr(picpath); IntPtr ptrLabel = mallocIntptr(label); ret = TrainImage(ptrPicPath, ptrLabel); ;// Marshal.GetFunctionPointerForDelegate(ccb)); Marshal.FreeHGlobal(ptrPicPath); Marshal.FreeHGlobal(ptrLabel); return ret; } /// /// 根据数据的长度申请非托管空间 /// /// 要申请非托管空间的数据 /// 指向非拖管空间的指针 private static IntPtr mallocIntptr(string strData) { //先将字符串转化成字节方式 Byte[] btData = System.Text.Encoding.Default.GetBytes(strData); //申请非拖管空间 IntPtr m_ptr = Marshal.AllocHGlobal(btData.Length); //给非拖管空间清0 Byte[] btZero = new Byte[btData.Length + 1]; //一定要加1,否则后面是乱码,原因未找到 Marshal.Copy(btZero, 0, m_ptr, btZero.Length); //给指针指向的空间赋值 Marshal.Copy(btData, 0, m_ptr, btData.Length); return m_ptr; } /// /// 根据长度申请非托管空间 /// /// 要申请非托管空间的大小 /// 指向非拖管空间的指针 private static IntPtr mallocIntptr(int length) { //申请非拖管空间 IntPtr m_ptr = Marshal.AllocHGlobal(length); //给非拖管空间清0 Byte[] btZero = new Byte[length + 1]; //一定要加1,否则后面是乱码,原因未找到 Marshal.Copy(btZero, 0, m_ptr, btZero.Length); //给指针指向的空间赋值 Marshal.Copy(btZero, 0, m_ptr, length); return m_ptr; } /* * 一、IntPtr 与 string互转 string str = "aa"; IntPtr init = Marshal.StringToHGlobalAnsi(str); string ss= Marshal.PtrToStringAnsi(init); //最后释放掉 Marshal.FreeHGlobal(init); 二、char*与string互转 string a = "11"; char* aChar = (char*)System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(a).ToPointer(); string result = Marshal.PtrToStringAnsi((IntPtr)aChar); 三、char* 与 IntPtr互转 可以直接强制类型转换 IntPtr init = (IntPtr)aChar; char* aChar = (char*)init; */ } public class DllInvoke { [DllImport("kernel32.dll", EntryPoint="LoadLibrary", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] private extern static IntPtr LoadLibrary(String path); [DllImport("kernel32.dll", EntryPoint="GetProcAddress")] private extern static IntPtr GetProcAddress(IntPtr lib, String funcName); [DllImport("kernel32.dll")] private extern static bool FreeLibrary(IntPtr lib); [DllImport("kernel32.dll")] private extern static IntPtr GetLastError(); private IntPtr hLib; public DllInvoke(String DLLPath) { hLib = LoadLibrary(DLLPath); if (hLib == IntPtr.Zero) { hLib=GetLastError(); } } ~DllInvoke() { FreeLibrary(hLib); } public Delegate Invoke(String APIName, Type t) { IntPtr api = GetProcAddress(hLib, APIName); return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t); }    } }