C# 调用动态链接库读取二代身份证信息

开发 后端
一般来说winform应用程序解决这个问题起来时很容易的,web应用程序就麻烦一点了。你必要有联机型居民身份证阅读器一个。

[[420764]]

本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。

概述

一般来说winform应用程序解决这个问题起来时很容易的,web应用程序就麻烦一点了。你必要有联机型居民身份证阅读器一个。

实现方式

1、新建一个winform控件项目ReadCardControl,添加一个主类ReadIDCard

  1. private bool  ReadIDCard() 
  2.      { 
  3.          try 
  4.          { 
  5.              BLL.API.SynIDCardAPI.IDCardData CardMsg = new BLL.API.SynIDCardAPI.IDCardData(); 
  6.              int nRet, nPort; 
  7.              byte[] pucIIN = new byte[4]; 
  8.              byte[] pucSN = new byte[8]; 
  9.              nPort = Convert.ToInt32(1001); 
  10.              if (BLL.API.SynIDCardAPI.Syn_OpenPort(nPort) == 0) 
  11.              { 
  12.                  if (BLL.API.SynIDCardAPI.Syn_SetMaxRFByte(nPort, 80, 0) == 0) 
  13.                  { 
  14.  
  15.                      nRet = BLL.API.SynIDCardAPI.Syn_StartFindIDCard(nPort, ref pucIIN[0], 0); 
  16.                      nRet = BLL.API.SynIDCardAPI.Syn_SelectIDCard(nPort, ref pucSN[0], 0); 
  17.                      nRet = BLL.API.SynIDCardAPI.Syn_ReadMsg(nPort, 0, ref CardMsg); 
  18.                      if (nRet == 0) 
  19.                      { 
  20.                          txtAddress.Text = CardMsg.Address; 
  21.                           
  22.                          txtBirthday.Text = CardMsg.Born; 
  23.                          txtCerticate.Text = CardMsg.IDCardNo; 
  24.                         
  25.                          txtIssuingAuthoriy.Text = CardMsg.GrantDept; 
  26.                          txtNames.Text = CardMsg.Name
  27.                          txtNation.Text = Xw.Common.Helper.NationHelper.GetNationByNO(CardMsg.Nation); 
  28.                          txtSex.Text = CardMsg.Sex == "1" ? "男" : "女"
  29.                          picMan.Image = Image.FromFile(CardMsg.PhotoFileName); 
  30.                          //lblMsg.Text = "读卡成功"
  31.                          return true
  32.                      } 
  33.                      else 
  34.                      { 
  35.                          lblMsg.Text = "请放卡: 读取身份证信息错误"
  36.                          return false
  37.                      } 
  38.                  } 
  39.              } 
  40.              else 
  41.              { 
  42.                  lblMsg.Text = "打开端口失败"
  43.                  return false
  44.              } 
  45.              return true
  46.          } 
  47.          catch (Exception ex) 
  48.          { 
  49.              Xw.Common.Helper.LogHelper.WriteErrorToLog(ex.Message); 
  50.              return false
  51.          } 
  52.      } 

2、声明DllImport,调用动态链接库

  1. public  class SynIDCardAPI 
  2.     { 
  3.         #region IDCardData 
  4.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 
  5.         public struct IDCardData 
  6.         { 
  7.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] //结构中使用的字串,[]类似限定说明 说明此公共结构传输到非托管代码时封装定义 
  8.             public string Name; //姓名    
  9.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)] 
  10.             public string Sex;   //性别 
  11.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] 
  12.             public string Nation; //名族 
  13.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 
  14.             public string Born; //出生日期 
  15.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)] 
  16.             public string Address; //住址 
  17.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)] 
  18.             public string IDCardNo; //身份证号 
  19.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
  20.             public string GrantDept; //发证机关 
  21.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 
  22.             public string UserLifeBegin; // 有效开始日期 
  23.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 
  24.             public string UserLifeEnd;  // 有效截止日期 
  25.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)] 
  26.             public string reserved; // 保留 
  27.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)] 
  28.             public string PhotoFileName; // 照片路径 
  29.         } 
  30.         #endregion 
  31.         #region SynIDCardAPI 
  32.         /************************端口类API *************************/ 
  33.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi)] 
  34.         public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen); 
  35.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi)] 
  36.         public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate); 
  37.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi)] 
  38.         public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud); 
  39.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi)] 
  40.         public static extern int Syn_OpenPort(int iPort); 
  41.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi)] 
  42.         public static extern int Syn_ClosePort(int iPort); 
  43.         /**************************SAM类函数 **************************/ 
  44.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi)] 
  45.         public static extern int Syn_ResetSAM(int iPort, int iIfOpen); 
  46.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi)] 
  47.         public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen); 
  48.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi)] 
  49.         public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen); 
  50.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi)] 
  51.         public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen); 
  52.         /*************************身份证卡类函数 ***************************/ 
  53.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi)] 
  54.         public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen); 
  55.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi)] 
  56.         public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen); 
  57.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)] 
  58.         public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen); 
  59.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDN", CharSet = CharSet.Ansi)] 
  60.         public static extern int Syn_ReadIINSNDN(int iPort, ref byte pucIINSNDN, int iIfOpen); 
  61.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi)] 
  62.         public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen); 
  63.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDNToASCII", CharSet = CharSet.Ansi)] 
  64.         public static extern int Syn_ReadIINSNDNToASCII(int iPort, ref byte pucIINSNDN, int iIfOpen); 
  65.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi)] 
  66.         public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref uint puiAppMsgLen, int iIfOpen); 
  67.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi)] 
  68.         public static extern int Syn_GetBmp(int iPort, ref byte Wlt_File); 
  69.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)] 
  70.         public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData); 
  71.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi)] 
  72.         public static extern int Syn_FindReader(); 
  73.         /***********************设置附加功能函数 ************************/ 
  74.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi)] 
  75.         public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath); 
  76.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi)] 
  77.         public static extern int Syn_SetPhotoType(int iType); 
  78.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi)] 
  79.         public static extern int Syn_SetPhotoName(int iType); 
  80.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi)] 
  81.         public static extern int Syn_SetSexType(int iType); 
  82.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi)] 
  83.         public static extern int Syn_SetNationType(int iType); 
  84.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi)] 
  85.         public static extern int Syn_SetBornType(int iType); 
  86.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi)] 
  87.         public static extern int Syn_SetUserLifeBType(int iType); 
  88.         [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi)] 
  89.         public static extern int Syn_SetUserLifeEType(int iType, int iOption); 
  90.  
  91.         #endregion 
  92.     } 

3、读取显示相关的信息

  1. txtAddress.Text = CardMsg.Address; 
  2.                              
  3.                             txtBirthday.Text = CardMsg.Born; 
  4.                             txtCerticate.Text = CardMsg.IDCardNo; 
  5.                            
  6.                             txtIssuingAuthoriy.Text = CardMsg.GrantDept; 
  7.                             txtNames.Text = CardMsg.Name
  8.                             txtNation.Text = Xw.Common.Helper.NationHelper.GetNationByNO(CardMsg.Nation); 
  9.                             txtSex.Text = CardMsg.Sex == "1" ? "男" : "女"
  10.                             picMan.Image = Image.FromFile(CardMsg.PhotoFileName); 
  11.                             //lblMsg.Text = "读卡成功"
  12.                             return true

 

责任编辑:武晓燕 来源: 后端Q
相关推荐

2009-08-05 16:29:18

C#调用C++动态链接

2011-04-29 09:40:59

复印二代身份证多功能一体机

2024-03-01 20:59:11

C#DLL开发

2009-08-28 16:19:30

C#实现修改动态链接库

2009-04-26 14:19:38

2011-05-18 17:15:45

2010-02-01 17:37:35

C++调用C链接库

2021-07-04 12:44:04

PythonExcel身份证

2023-05-09 08:24:11

JNA链接库代码

2012-01-06 10:25:50

JavaDLLC++

2023-11-29 08:31:20

PythonRust

2020-02-05 15:29:56

计算机网络设备

2018-01-04 11:28:23

2011-06-21 18:02:14

Qt 动态 链接库

2011-08-02 14:15:05

XCode 静态 链接库

2012-05-04 08:24:14

LinuxUnix

2011-05-23 14:03:42

扫描仪评测

2017-01-03 09:25:35

全实名电话诈骗

2022-06-09 09:54:45

编译软件开发

2012-05-08 14:48:23

LinuxUnix动态链接库
点赞
收藏

51CTO技术栈公众号