日期:2014-05-18  浏览次数:20796 次

这个dll的方法怎么调用
搞了一天总是不正确,报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”,应该是C#结构体里的参数定义有问题
//************************************
// Method: FP_Match
// FullName: FP_Match
// Access: public 
// Returns: int __stdcall
// Qualifier: 提交特征比对,获取比对结果
// Parameter: 输入,FP_MatchParam * pMatchParam 比对参数
// Parameter: 输出,FP_MatchResult * pResult 比对结果,函数内部分配,需要调用FP_FreeMatchResult释放
//************************************
int __stdcall FP_Match(FP_MatchParam * pMatchParam, FP_MatchResult * pResult);

两个参数都是机构体,具体如下
//比对参数结构体
struct FP_MatchParam
{
int nFPosArr[2];//指位信息,0-9为合法值
unsigned char* probe_finger[2];//指纹特征数组,NULL表示无指纹特征
int nThreshold; //比对相似度阈值,建议设为260
int nCandidateN; //最大返回候选数量,通常设为1
};

//比对结果结构体
struct FP_MatchResult
{
int nCandidateN;//实际返回候选数量
char** id_arr;//候选结果ID列表
int* SimArr;//候选结果相似度列表
};

要怎么用C#调用才正确。。。

------解决方案--------------------
如果理解正确,请参照此帖回复
http://topic.csdn.net/u/20090522/20/AE470EF3-B964-4C45-AA89-4B07AE7B4EF6.html
------解决方案--------------------
Parameter: 输出,FP_MatchResult * pResult 比对结果,函数内部分配,需要调用FP_FreeMatchResult释放
//************************************
int __stdcall FP_Match(FP_MatchParam * pMatchParam, FP_MatchResult * pResult);

我觉得参数有错误,返回参数应该是 FP_MatchResult ** ppResult。
而且,第一个参数写的很不好。
------解决方案--------------------
晕死,看上去不难,实际写起来繁琐无比!
C# code
        [StructLayout(LayoutKind.Sequential)]
        public struct FP_MatchParam
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public Int32[] nFPosArr;//指位信息,0-9为合法值
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.SysInt, SizeConst = 2)]
            public IntPtr[] probe_finger;//指纹特征数组,NULL表示无指纹特征
            public Int32 nThreshold; //比对相似度阈值,建议设为260
            public Int32 nCandidateN; //最大返回候选数量,通常设为1
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct FP_MatchResult
        {
            public Int32 nCandidateN;//实际返回候选数量
            public IntPtr id_arr;//候选结果ID列表
            public IntPtr SimArr;//候选结果相似度列表
        }

            [DllImport("MyDll.dll")]
            public static extern int FP_Match([In] ref FP_MatchParam pMatchParam, [Out] out FP_MatchResult pResult);

            const int MaxCandidate = 2;

            FP_MatchParam param = new FP_MatchParam();
            param.nFPosArr = new Int32[2] { 9, 9 };

            Byte[] probe_finger1 = new Byte[10];
            Byte[] probe_finger2 = new Byte[10];
            GCHandle gch1 = GCHandle.Alloc(probe_finger1, GCHandleType.Pinned);
            GCHandle gch2 = GCHandle.Alloc(probe_finger2, GCHandleType.Pinned);

            param.probe_finger = new IntPtr[2] { 
                Marshal.UnsafeAddrOfPinnedArrayElement(probe_finger1, 0), 
                Marshal.UnsafeAddrOfPinnedArrayElement(probe_finger2, 0) };

            param.nThreshold = 260;
            param.nCandidateN = MaxCandidate;

            FP_MatchResult result = new FP_MatchResult();
            IntPtr[] arrIdPtr = new IntPtr[MaxCandidate];
            Int32[] arrSim = new Int32[MaxCandidate];
            GCHandle gch3 = GCHandle.Alloc(arrIdPtr, GCHandleType.Pinned);
            GCHandle gch4 = GCHandle.Alloc(arrSim, GCHandleType.Pinned);
            result.id_arr = Marshal.UnsafeAddrOfPinnedArrayElement(arrIdPtr, 0);
            result.SimArr = Marshal.UnsafeAddrOfPinnedArrayElement(arrSim, 0);

            LibWrap.FP_Match(ref param, out result);
            for (int i = 0; i < result.nCandidateN; i++)
            {
                Console.WriteLine("ID:{0}", Marshal.PtrToStringA