如何封送 C# 结构体数组?

发布于 2024-12-08 03:40:25 字数 2313 浏览 1 评论 0原文

我正在为人力资源管理系统开发指纹登录。 SDK 提供的示例代码允许使用一个模板进行验证。这是代码段。

 
    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);

根据SDK文档(Link) ABSVerify方法允许输入模板数组作为参数之一。 我发现整理 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.
        }

这是我带来的解决方案。 我写了一个名为 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;
            }
        }

    }

请帮助我。谢谢。

I’m developing a fingerprint login for a HRM system. Sample code given with the SDK allows verifying with one template. 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.
I’m finding difficulties marshaling ABS_BIR struct array. Here is the struct for 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()

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2024-12-15 03:40:25

如果我错了,请检查我,但我相信我在某处读到可以通过执行以下操作来将结构编组到数组:

[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.
}

然后简单地将结构传递到数组的位置。

祝你好运

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文