简体   繁体   English

如何编组C#结构数组?

[英]How to Marshal C# struct array?

I'm developing a fingerprint login for a HRM system. 我正在为HRM系统开发指纹登录。 Sample code given with the SDK allows verifying with one template. SDK提供的示例代码允许使用一个模板进行验证。 Here is the segment of the code. 这是代码段。

 
    BSTypes.ABS_BIR ppEnrolledTemplate; //Load the template array to ppEnrolledTemplate

    IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ppEnrolledTemplateArray[i]));

    Marshal.StructureToPtr(ppEnrolledTemplateArray[i], ptr, false);

    res = BSApi.ABSVerify(conn, ref op, 1, ref ptr, ref matching_slot, 0);

According to the SDK documentation( Link ) ABSVerify method allows to input template array as one of the parameters. 根据SDK文档( Link ),ABSVerify方法允许输入模板数组作为参数之一。 I'm finding difficulties marshaling ABS_BIR struct array. 我在整理ABS_BIR结构数组时遇到困难。 Here is the struct for ABS_BIR. 这是ABS_BIR的结构。


[StructLayout(LayoutKind.Sequential)]      
        public struct ABS_BIR
        {
            public ABS_BIR_HEADER Header;   // BIR header
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2560)]
            public byte[] Data;     // The data composing the fingerprint template.
        }

This is the solution I came with. 这是我附带的解决方案。 I wrote a method called ReadyTemplate() 我写了一个叫ReadyTemplate()的方法

private void readTemplate() {
        //read template from binary file
        for (int i = 0; i < 4; i++)
        {
            FileStream objFileStream;
            BinaryReader objBinaryReader;
            try
            {
                // ========== Updated by bubz ============
                byte[] data;
                objFileStream = new FileStream(i + ".bin", FileMode.Open);
                objBinaryReader = new BinaryReader(objFileStream);
                data = objBinaryReader.ReadBytes((int)objFileStream.Length);

                GCHandle pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned);
                ppEnrolledTemplateArray[i] = (BSTypes.ABS_BIR)Marshal.PtrToStructure(pinnedData.AddrOfPinnedObject(), typeof(BSTypes.ABS_BIR));

                displayOut(0, 0, "Template retrieved from PC.");
                displayOut(0, 0, i.ToString());
                objBinaryReader.Close();

                // ============ end ====================


            }

            catch (FileNotFoundException FileEx)
            {
                displayOut(2, 0, FileEx.Message);
                return;
            }

            catch (Exception Ex)
            {
                displayOut(2, 0, Ex.Message);
                return;
            }
        }

    }

Please help me.Thanks. 请帮助我。谢谢。

Check me if I'm wrong, but I believe I read somewhere that marshaling a struct to an array can be performed by doing the following: 检查我是否错,但是我相信我读到某个地方可以通过执行以下操作来将结构封送到数组中:

[StructLayout(LayoutKind.Sequential)]
[MarshalAs(UnmanagedType.ByValArray)]      
public struct ABS_BIR
{
    public ABS_BIR_HEADER Header;   // BIR header

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2560)]
    public byte[] Data;     // The data composing the fingerprint template.
}

And then simply passing the struct in the place of the array. 然后只需将结构传递给数组即可。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM