![](/img/trans.png)
[英]Passing structure containing an array of structures between C and C# (DLL and P invoke)
[英]P/Invoke marshaling and unmarshalling 2D array, structure and pointers between C# and unmanaged DLL
Flann C ++庫具有針對C , C ++ , Python , Matlab和Ruby的包裝器,但沒有可用的C#包裝器。 我試圖圍繞從此處下載的flann.dll
32位非托管DLL創建一個C#包裝器。
作為PInvoke /編組的新手,我敢肯定我沒有正確執行對DLL的C#P / Invoke調用。 我基本上是試圖鏡像C#中可用的Python包裝器 。 混亂的主要方面是:
float*
即指向以行主要順序存儲的查詢集的指針(根據flann.h中的注釋 ) 。 struct FLANNParameters*
IntPtr
是否適合引用typedef void*
和int* indices
? 我需要使用的從flann.h公開導出的C ++方法如下:
typedef void* FLANN_INDEX; /* deprecated */
typedef void* flann_index_t;
FLANN_EXPORT extern struct FLANNParameters DEFAULT_FLANN_PARAMETERS;
// dataset = pointer to a query set stored in row major order
FLANN_EXPORT flann_index_t flann_build_index(float* dataset,
int rows,
int cols,
float* speedup,
struct FLANNParameters* flann_params);
FLANN_EXPORT int flann_free_index(flann_index_t index_id,
struct FLANNParameters* flann_params);
FLANN_EXPORT int flann_find_nearest_neighbors(float* dataset,
int rows,
int cols,
float* testset,
int trows,
int* indices,
float* dists,
int nn,
struct FLANNParameters* flann_params);
這是基於上述公開方法的C#包裝器。
using System;
using System.Runtime.InteropServices;
namespace FlannWrapper
{
/// <summary>
/// Methods to map between native unmanaged C++ DLL and managed C#
/// Trying to mirror: https://github.com/mariusmuja/flann/blob/master/src/cpp/flann/flann.h
/// </summary>
public class NativeMethods
{
/// <summary>
/// 32-bit flann dll obtained from from http://sourceforge.net/projects/pointclouds/files/dependencies/flann-1.7.1-vs2010-x86.exe/download
/// </summary>
public const string DllWin32 = @"C:\Program Files (x86)\flann\bin\flann.dll";
/// <summary>
/// C++: flann_index_t flann_build_index(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
/// </summary>
[DllImport(DllWin32, EntryPoint = "flann_build_index", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern IntPtr flannBuildIndex([In] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R4)] float[,] dataset, // ??? [In] IntPtr dataset ???
int rows, int cols,
ref float speedup, // ???
[In] ref FlannParameters flannParams); // ???
/// <summary>
/// C++: int flann_free_index(flann_index_t index_ptr, FLANNParameters* flann_params)
/// </summary>
[DllImport(DllWin32, EntryPoint = "flann_free_index", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int flannFreeIndex(IntPtr indexPtr, // ???
[In] ref FlannParameters flannParams); // ??? [In, MarshalAs(UnmanagedType.LPStruct)] FlannParameters flannParams);
/// <summary>
/// C++: int flann_find_nearest_neighbors_index(flann_index_t index_ptr, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
/// </summary>
[DllImport(DllWin32, EntryPoint = "flann_find_nearest_neighbors_index", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int flannFindNearestNeighborsIndex(IntPtr indexPtr, // ???
[In] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R4)] float[,] testset, // ??? [In] IntPtr dataset ???
int tCount,
[Out] IntPtr result, // ??? [Out] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R4)] int[,] result,
[Out] IntPtr dists, // ???
int nn,
[In] ref FlannParameters flannParams); // ???
}
}
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FlannWrapper
{
[TestClass]
public class FlannTest : IDisposable
{
private IntPtr curIndex;
protected FlannParameters flannParams;
// protected GCHandle gcHandle;
[TestInitialize]
public void TestInitialize()
{
this.curIndex = IntPtr.Zero;
// Initialise Flann Parameters
this.flannParams = new FlannParameters(); // use defaults
this.flannParams.algorithm = FlannAlgorithmEnum.FLANN_INDEX_KDTREE;
this.flannParams.trees = 8;
this.flannParams.logLevel = FlannLogLevelEnum.FLANN_LOG_WARN;
this.flannParams.checks = 64;
}
[TestMethod]
public void FlannNativeMethodsTestSimple()
{
int rows = 3, cols = 5;
int tCount = 2, nn = 3;
float[,] dataset2D = { { 1.0f, 1.0f, 1.0f, 2.0f, 3.0f},
{ 10.0f, 10.0f, 10.0f, 3.0f, 2.0f},
{ 100.0f, 100.0f, 2.0f, 30.0f, 1.0f} };
//IntPtr dtaasetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * dataset2D.Length);
float[,] testset2D = { { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f},
{ 90.0f, 90.0f, 10.0f, 10.0f, 1.0f} };
//IntPtr testsetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * testset2D.Length);
int outBufferSize = tCount * nn;
int[] result = new int[outBufferSize];
int[,] result2D = new int[tCount, nn];
IntPtr resultPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * result.Length);
float[] dists = new float[outBufferSize];
float[,] dists2D = new float[tCount, nn];
IntPtr distsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * dists.Length);
try
{
// Copy the array to unmanaged memory.
//Marshal.Copy(testset, 0, testsetPtr, testset.Length);
//Marshal.Copy(dataset, 0, datasetPtr, dataset.Length);
if (this.curIndex != IntPtr.Zero)
{
// n - number of bytes which is enough to keep any type used by function
NativeMethods.flannFreeIndex(this.curIndex, ref this.flannParams);
this.curIndex = IntPtr.Zero;
}
//GC.KeepAlive(this.curIndex); // TODO
float speedup = 0.0f; // TODO: ctype float
Console.WriteLine("Computing index.");
this.curIndex = NativeMethods.flannBuildIndex(dataset2D, rows, cols, ref speedup, ref this.flannParams);
NativeMethods.flannFindNearestNeighborsIndex(this.curIndex, testset2D, tCount, resultPtr, distsPtr, nn, ref this.flannParams);
// Copy unmanaged memory to managed arrays.
Marshal.Copy(resultPtr, result, 0, result.Length);
Marshal.Copy(distsPtr, dists, 0, dists.Length);
// Clutching straws, convert 1D to 2D??
for(int row=0; row<tCount; row++)
{
for(int col=0; col<nn; col++)
{
int buffIndex = row*nn + col;
result2D[row, col] = result[buffIndex];
dists2D[row, col] = dists[buffIndex];
}
}
}
finally
{
// Free unmanaged memory -- [BREAKPOINT HERE]
// Free input pointers
//Marshal.FreeHGlobal(testsetPtr);
//Marshal.FreeHGlobal(datasetPtr);
// Free output pointers
Marshal.FreeHGlobal(resultPtr);
Marshal.FreeHGlobal(distsPtr);
}
}
[TestCleanup]
public void TestCleanup()
{
if (this.curIndex != IntPtr.Zero)
{
NativeMethods.flannFreeIndex(this.curIndex, ref flannParams);
Marshal.FreeHGlobal(this.curIndex);
this.curIndex = IntPtr.Zero;
// gcHandle.Free();
}
}
}
}
嘗試鏡像Python FLANNParameters類和C struct FLANNParameters 。
using System;
using System.Runtime.InteropServices;
namespace FlannWrapper
{
// FieldOffsets set based on assumption that C++ equivalent of int, uint, float, enum are all 4 bytes for 32-bit
[StructLayout(LayoutKind.Explicit)]
public class FLANNParameters
{
[FieldOffset(0)]
public FlannAlgorithmEnum algorithm;
[FieldOffset(4)]
public int checks;
[FieldOffset(8)]
public float eps;
[FieldOffset(12)]
public int sorted;
[FieldOffset(16)]
public int maxNeighbors;
[FieldOffset(20)]
public int cores;
[FieldOffset(24)]
public int trees;
[FieldOffset(28)]
public int leafMaxSize;
[FieldOffset(32)]
public int branching;
[FieldOffset(36)]
public int iterations;
[FieldOffset(40)]
public FlannCentersInitEnum centersInit;
[FieldOffset(44)]
public float cbIndex;
[FieldOffset(48)]
public float targetPrecision;
[FieldOffset(52)]
public float buildWeight;
[FieldOffset(56)]
public float memoryWeight;
[FieldOffset(60)]
public float sampleFraction;
[FieldOffset(64)]
public int tableNumber;
[FieldOffset(68)]
public int keySize;
[FieldOffset(72)]
public int multiProbeLevel;
[FieldOffset(76)]
public FlannLogLevelEnum logLevel;
[FieldOffset(80)]
public long randomSeed;
/// <summary>
/// Default Constructor
/// Ref https://github.com/mariusmuja/flann/blob/master/src/python/pyflann/flann_ctypes.py : _defaults
/// </summary>
public FlannParameters()
{
this.algorithm = FlannAlgorithmEnum.FLANN_INDEX_KDTREE;
this.checks = 32;
this.eps = 0.0f;
this.sorted = 1;
this.maxNeighbors = -1;
this.cores = 0;
this.trees = 1;
this.leafMaxSize = 4;
this.branching = 32;
this.iterations = 5;
this.centersInit = FlannCentersInitEnum.FLANN_CENTERS_RANDOM;
this.cbIndex = 0.5f;
this.targetPrecision = 0.9f;
this.buildWeight = 0.01f;
this.memoryWeight = 0.0f;
this.sampleFraction = 0.1f;
this.tableNumber = 12;
this.keySize = 20;
this.multiProbeLevel = 2;
this.logLevel = FlannLogLevelEnum.FLANN_LOG_WARN;
this.randomSeed = -1;
}
}
public enum FlannAlgorithmEnum : int
{
FLANN_INDEX_KDTREE = 1
}
public enum FlannCentersInitEnum : int
{
FLANN_CENTERS_RANDOM = 0
}
public enum FlannLogLevelEnum : int
{
FLANN_LOG_WARN = 3
}
}
?result2D
{int[2, 3]}
[0, 0]: 7078010
[0, 1]: 137560165
[0, 2]: 3014708
[1, 0]: 3014704
[1, 1]: 3014704
[1, 2]: 48
?dists2D
{float[2, 3]}
[0, 0]: 2.606415E-43
[0, 1]: 6.06669328E-34
[0, 2]: 9.275506E-39
[1, 0]: 1.05612418E-38
[1, 1]: 1.01938872E-38
[1, 2]: 1.541428E-43
如您所見,在Debug模式下運行Test時,我沒有收到任何錯誤,但是我知道輸出肯定是不正確的-由於內存尋址不正確而導致垃圾值。 我還包括了我嘗試過但未成功的其他封送簽名(請參閱帶有???的評論)。
為了找出正確的結果,我使用可用的Python庫PyFlann進行了快速測試。
import pyflann
import numpy as np
dataset = np.array(
[[1., 1., 1., 2., 3.],
[10., 10., 10., 3., 2.],
[100., 100., 2., 30., 1.] ])
testset = np.array(
[[1., 1., 1., 1., 1.],
[90., 90., 10., 10., 1.] ])
flann = pyflann.FLANN()
result, dists = flann.nn(dataset, testset, num_neighbors = 3,
algorithm="kdtree", trees=8, checks=64) # flann parameters
# Output
print("\nResult:")
print(result)
print("\nDists:")
print(dists)
在幕后,PyFlann.nn()調用了公開的C方法,正如我們從index.py看到的那樣 。
Result:
[[0 1 2]
[2 1 0]]
Dists:
[[ 5.00000000e+00 2.48000000e+02 2.04440000e+04]
[ 6.64000000e+02 1.28500000e+04 1.59910000e+04]]
對此方法的任何正確幫助將不勝感激。 謝謝。
當使用p / invoke時,您必須停止思考“托管”,而應該考慮物理二進制布局,32 vs 64位等。而且,當被調用的本機二進制始終在進程中運行時(例如此處,但是使用COM服務器可能會有所不同),它比進程外處理更容易,因為您不必考慮封送處理/序列化,引用與輸出等問題。
另外,您無需告訴.NET它已經知道的信息。 float數組是R4的LPArray,您不必指定它。 越簡單越好。
因此,首先是flann_index_t
。 它在C中定義為void *
,因此它顯然必須是IntPtr
(“某物”上的不透明指針)。
然后,構造。 如果將其定義為struct
則可以將其作為C中的簡單指針傳遞的結構作為ref
參數傳遞。 如果將其定義為class
,請不要使用ref
。 通常,我更喜歡將struct用於C結構。
您必須確保結構定義正確。 通常,您使用LayoutKind.Sequential
因為.NET p / invoke將以與C編譯器相同的方式打包參數。 因此,您不必使用顯式的,特別是當參數是標准的(不是一點點的東西),例如int,float時,因此,如果所有成員都已正確聲明,則可以刪除所有FieldOffset並使用LayoutKind.Sequential ...但這不是案件。
對於類型,就像我說的那樣,您真的必須考慮二進制,並問自己使用的每種類型,二進制的布局是什么,大小是多少? int
是(使用99.9%C編譯器)32位。 float和double是IEEE標准,因此永遠不會有問題。 枚舉通常基於int
,但這可能有所不同(在C和.NET中,為了能夠與C匹配)。 long
(有99.0%的C編譯器)32位,而不是 64位。 因此,.NET等效為Int32(int),而不是Int64(long)。
因此,您應該更正FlannParameters
結構,並將long
替換為int
。 如果您確實想確定給定的結構,請使用與用於編譯要調用的庫相同的C編譯器,針對C的sizeof(mystruct)
對照Marshal.SizeOf(mystruct)
進行檢查。 它們應該是相同的。 如果不是,則說明.NET定義中存在錯誤(包裝,成員大小,訂單等)。
這是似乎有效的修改后的定義和調用代碼。
static void Main(string[] args)
{
int rows = 3, cols = 5;
int tCount = 2, nn = 3;
float[,] dataset2D = { { 1.0f, 1.0f, 1.0f, 2.0f, 3.0f},
{ 10.0f, 10.0f, 10.0f, 3.0f, 2.0f},
{ 100.0f, 100.0f, 2.0f, 30.0f, 1.0f} };
float[,] testset2D = { { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f},
{ 90.0f, 90.0f, 10.0f, 10.0f, 1.0f} };
var fparams = new FlannParameters();
var index = NativeMethods.flannBuildIndex(dataset2D, rows, cols, out float speedup, ref fparams);
var indices = new int[tCount, nn];
var idists = new float[tCount, nn];
NativeMethods.flannFindNearestNeighborsIndex(index, testset2D, tCount, indices, idists, nn, ref fparams);
NativeMethods.flannFreeIndex(index, ref fparams);
}
[DllImport(DllWin32, EntryPoint = "flann_build_index", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr flannBuildIndex(float[,] dataset,
int rows, int cols,
out float speedup, // out because, it's and output parameter, but ref is not a problem
ref FlannParameters flannParams);
[DllImport(DllWin32, EntryPoint = "flann_free_index", CallingConvention = CallingConvention.Cdecl)]
public static extern int flannFreeIndex(IntPtr indexPtr, ref FlannParameters flannParams);
[DllImport(DllWin32, EntryPoint = "flann_find_nearest_neighbors_index", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int flannFindNearestNeighborsIndex(IntPtr indexPtr,
float[,] testset,
int tCount,
[In, Out] int[,] result, // out because it may be changed by C side
[In, Out] float[,] dists,// out because it may be changed by C side
int nn,
ref FlannParameters flannParams);
[StructLayout(LayoutKind.Sequential)]
public struct FlannParameters
{
public FlannAlgorithmEnum algorithm;
public int checks;
public float eps;
public int sorted;
public int maxNeighbors;
public int cores;
public int trees;
public int leafMaxSize;
public int branching;
public int iterations;
public FlannCentersInitEnum centersInit;
public float cbIndex;
public float targetPrecision;
public float buildWeight;
public float memoryWeight;
public float sampleFraction;
public int tableNumber;
public int keySize;
public int multiProbeLevel;
public FlannLogLevelEnum logLevel;
public int randomSeed;
}
注意:我嘗試使用特定的flann參數值,但是在這種情況下庫崩潰了,我不知道為什么...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.