public static ui.model.SearchResult Search(byte[] imgContent) { Contract.Requires(imgContent != null); Contract.Requires(imgContent.Length != 0); IntPtr imgContentUnmanagedBuf = IntPtr.Zero; var result = new structs.SearchResult() { items = IntPtr.Zero, itemsPositions = IntPtr.Zero }; try { imgContentUnmanagedBuf = Marshal.AllocCoTaskMem(imgContent.Length); Marshal.Copy(imgContent, 0, imgContentUnmanagedBuf, imgContent.Length); var query = new structs.ImgQuery() { exampleContent = imgContentUnmanagedBuf, exampleContentSize = (UInt32)imgContent.Length, tolerance = 0.5 }; Search(query, out result); if (result.opStatus != 0) { throw new Exception("Call to the unmanaged DLL failed. Search cannot be performed"); } if (result.items == IntPtr.Zero || result.itemsPositions == IntPtr.Zero || result.arraySize == 0) { //no images found return(new ui.model.SearchResult()); } long[] imgIds = new long[result.arraySize]; double[] positions = new double[result.arraySize]; Marshal.Copy(result.items, imgIds, 0, (int)result.arraySize); Marshal.Copy(result.itemsPositions, positions, 0, (int)result.arraySize); return(new ui.model.SearchResult(imgIds, positions)); } finally { if (imgContentUnmanagedBuf != IntPtr.Zero) { Marshal.FreeCoTaskMem(imgContentUnmanagedBuf); } if (result.items != IntPtr.Zero) { Marshal.FreeCoTaskMem(result.items); } if (result.itemsPositions != IntPtr.Zero) { Marshal.FreeCoTaskMem(result.itemsPositions); } } }
private static extern void Search(structs.ImgQuery query, out structs.SearchResult result);