如何封送 C# 结构体数组?
我正在为人力资源管理系统开发指纹登录。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我错了,请检查我,但我相信我在某处读到可以通过执行以下操作来将结构编组到数组:
然后简单地将结构传递到数组的位置。
祝你好运
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:
And then simply passing the struct in the place of the array.
Good luck