小弟在弄一个“第二代居民身份证阅读器”的实例,希望大家帮帮忙,小弟对控制台项目不是很擅长
参考资料:
一. termb.dll说明
1. int __stdcall InitComm(int iPort)//初始化设备(串口:1-16,USB:1001-1016)
2. int __stdcall Authenticate() //证卡验证,返回值不需要判断
3. int __stdcall Read_Content(int iActive)//读基本信息,iActive=1;读卡成功后照片信息存放在zp.bmp文件中
4. int __stdcall Read_Content(int iActive)//读追加地址,iActive=3;
5.读卡成功后调用以下方法获取相应的身份证信息:
int __stdcall GetPeopleAddress(char* pData, UINT iDataLen)//地址
int __stdcall GetPeopleIDCode(char* pData, UINT iDataLen) //身份证号码
int __stdcall GetPeopleBirthday(char* pData, UINT iDataLen) //出生日期
int __stdcall GetPeopleNation(char* pData, UINT iDataLen) //民族
int __stdcall GetPeopleSex(char* pData, UINT iDataLen)//性别
int __stdcall GetPeopleName(char* pData, UINT iDataLen)//姓名
int __stdcall GetDepartment(char* pData, UINT iDataLen)//发证机关
int __stdcall GetStartDate(char* pData, UINT iDataLen) //证件开始日期
int __stdcall GetEndDate(char* pData, UINT iDataLen)//证件结束日期
int __stdcall GetPhotoBMP(char* pData, UINT iDataLen)//照片
int __stdcall GetAppAddress(UINT index, char* pData, UINT iDataLen)//追加地址
6. int __stdcall CloseComm()读卡完毕后,关闭设备const int ERR_SUCCESS = 1;//成功
const int ERR_FAIL = 0;//失败
const int ERR_SAVESERIALNO = -1;//存序列号失败 未授权机具
const int ERR_CANCELSERIALNO = -1;//序列号取消 未授权机具const int ERR_OPENECOMM = -2;//打开串口
const int ERR_CLOSECOMM = -3;//关闭串口const int ERR_SAMSTATUS = -4;//取sam状态失败
const int ERR_SAMID = -5;//取samID失败
const int ERR_FINDCARD = -6;//找卡错误
const int ERR_SELECTCARD = -7;//选卡错误
const int ERR_BASEINFO = -8;//读取基本信息错误
const int ERR_APPINFO = -9;//读取附加信息错误
const int ERR_MNGINFO = -10;//读取MNG信息错误实例代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int InitComm(int iPort); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int CloseComm(); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int Authenticate(); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int Read_Content(int iActive); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleName(Byte[] buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleSex(Byte[] buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleNation(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleBirthday(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleAddress(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetStartDate(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetEndDate(StringBuilder buf, int iLen); static string GetName()
{
Byte[] asciiBytes = null;
asciiBytes = new Byte[100];
int dwRet = GetPeopleName(asciiBytes, 100);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
return new string(asciiChars);
} static string GetSex()
{
Byte[] asciiBytes = null;
asciiBytes = new Byte[100];
int dwRet = GetPeopleSex(asciiBytes, 100);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
return new string(asciiChars);
} static void Main(string[] args)
{
int i = InitComm(1001);
i = Authenticate();
i = Read_Content(1); string szValue = null; szValue = GetName();
}
}
}小弟,对“控制台项目”不是很了解
现在想:敲入“Enter”,在控制台上显示出“GetName()”和“GetSex”信息,并把照片放在zp.bmp文件中,接下来该怎样写???小弟感激不尽
参考资料:
一. termb.dll说明
1. int __stdcall InitComm(int iPort)//初始化设备(串口:1-16,USB:1001-1016)
2. int __stdcall Authenticate() //证卡验证,返回值不需要判断
3. int __stdcall Read_Content(int iActive)//读基本信息,iActive=1;读卡成功后照片信息存放在zp.bmp文件中
4. int __stdcall Read_Content(int iActive)//读追加地址,iActive=3;
5.读卡成功后调用以下方法获取相应的身份证信息:
int __stdcall GetPeopleAddress(char* pData, UINT iDataLen)//地址
int __stdcall GetPeopleIDCode(char* pData, UINT iDataLen) //身份证号码
int __stdcall GetPeopleBirthday(char* pData, UINT iDataLen) //出生日期
int __stdcall GetPeopleNation(char* pData, UINT iDataLen) //民族
int __stdcall GetPeopleSex(char* pData, UINT iDataLen)//性别
int __stdcall GetPeopleName(char* pData, UINT iDataLen)//姓名
int __stdcall GetDepartment(char* pData, UINT iDataLen)//发证机关
int __stdcall GetStartDate(char* pData, UINT iDataLen) //证件开始日期
int __stdcall GetEndDate(char* pData, UINT iDataLen)//证件结束日期
int __stdcall GetPhotoBMP(char* pData, UINT iDataLen)//照片
int __stdcall GetAppAddress(UINT index, char* pData, UINT iDataLen)//追加地址
6. int __stdcall CloseComm()读卡完毕后,关闭设备const int ERR_SUCCESS = 1;//成功
const int ERR_FAIL = 0;//失败
const int ERR_SAVESERIALNO = -1;//存序列号失败 未授权机具
const int ERR_CANCELSERIALNO = -1;//序列号取消 未授权机具const int ERR_OPENECOMM = -2;//打开串口
const int ERR_CLOSECOMM = -3;//关闭串口const int ERR_SAMSTATUS = -4;//取sam状态失败
const int ERR_SAMID = -5;//取samID失败
const int ERR_FINDCARD = -6;//找卡错误
const int ERR_SELECTCARD = -7;//选卡错误
const int ERR_BASEINFO = -8;//读取基本信息错误
const int ERR_APPINFO = -9;//读取附加信息错误
const int ERR_MNGINFO = -10;//读取MNG信息错误实例代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int InitComm(int iPort); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int CloseComm(); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int Authenticate(); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int Read_Content(int iActive); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleName(Byte[] buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleSex(Byte[] buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleNation(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleBirthday(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleAddress(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetStartDate(StringBuilder buf, int iLen); [DllImport("termb.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern int GetEndDate(StringBuilder buf, int iLen); static string GetName()
{
Byte[] asciiBytes = null;
asciiBytes = new Byte[100];
int dwRet = GetPeopleName(asciiBytes, 100);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
return new string(asciiChars);
} static string GetSex()
{
Byte[] asciiBytes = null;
asciiBytes = new Byte[100];
int dwRet = GetPeopleSex(asciiBytes, 100);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
return new string(asciiChars);
} static void Main(string[] args)
{
int i = InitComm(1001);
i = Authenticate();
i = Read_Content(1); string szValue = null; szValue = GetName();
}
}
}小弟,对“控制台项目”不是很了解
现在想:敲入“Enter”,在控制台上显示出“GetName()”和“GetSex”信息,并把照片放在zp.bmp文件中,接下来该怎样写???小弟感激不尽
{
if(e.KeyChar == (char)13)
{
//.....
}
}
平台调用服务,即P/Invoke是受控代码与非受控代码进行交互,使托管代码能够调用 DLL 中实现的非托管函数。调用系统的 API 或与 COM 对象打交道,通过 System.Runtime.InteropServices 命名空间为了从托管代码中调用非托管的DLL中函数,你要创建一个P/Invoke包装(Wrapper)。一个P/Invoke包装是一个.net兼 容的方法声明,用来创建P/Invoke包装的语法与创建托管
方法的声明语法本质上是一样的。唯一不同是P/Invoke包装不包含函数体,而只有方法 名、返回值类型和参数信息。并且,P/Invoke包装使用了DllImport属性。这个属性是用来
定位包含有目标函数的非托管的DLL。
{
//输出文件
Console.WriteLine("name");
Console.WriteLine("sex");
char ch = (char)Console.Read(); // get a char
}输出文件再看看
{
//输出文件 byte[] bytelist=new byte[]{1,0,1};//从读卡器读到的byte
using (FileStream fs = new FileStream("zp.bmp", FileMode.Create))
{
fs.Write(bytelist, 0, bytelist.Length);
}
Console.WriteLine("name");
Console.WriteLine("sex");
char ch = (char)Console.Read(); // get a char
}