// iccard_demo.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <string.h>#define IO_CTL_ICCARD_RD_ISO 0x01
#define IO_CTL_ICCARD_RD_MAIN 0x02
#define IO_CTL_ICCARD_WR_MAIN 0x04
#define IO_CTL_ICCARD_CMP_PSW 0x08
#define IO_CTL_ICCARD_RD_PROTECT 0x10
#define IO_CTL_ICCARD_RD_PSC 0x20static HANDLE hDrv;int ICCard_Init(void)
{
hDrv = CreateFile(
L"ICC0:",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if (INVALID_HANDLE_VALUE == hDrv)
{
printf("Can't open IICard\n\r");
return -1;
}
return 0;
}void ICCardTestMenu(void)
{
char c;
UINT8 cBufIn[257];
UINT8 cBufout[257];
DWORD dwLenIn;
DWORD dwLenOut;
DWORD dwActualOut;
PDWORD pdwStartAddr = (PDWORD)cBufIn;
PDWORD pdwLen = (PDWORD)(cBufIn + 4); PBYTE pucTmp; BOOL status;
int i;
char str[10];
while (1)
{
printf("\r\n##### IC Card Test Menu #####\r\n");
printf("[I] Read ISO number\n\r");
printf("[R] Read Main memory\n\r");
printf("[W] Write Main memory\n\r");
printf("[C] Compare password as ff ff ff\n\r");
printf("[P] Read Protection Memory\n\r");
printf("[S] Read PSC\n\r");
printf("[X] Press X to quit\n\r");
printf("Enter your selection: "); do {
c = getchar();
} while (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')));
switch (c)
{
case 'i':
case 'I':
{
*pdwStartAddr = 0;
*pdwLen = 4;
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_ISO,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("IC Card ISO number, byte read: %d \n\r", dwActualOut);
for (i = 0; i < 4; i++)
printf("0x%02x ", cBufout[i]);
printf("\n\r");
break;
}
case 'r':
case 'R':
{
*pdwStartAddr = 0;
*pdwLen = 32; printf("Reading address 0~31:\n\r");
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
cBufout[32] = '\0';
printf("Value as hex : ");
for (i = 0; i < 32; i++)
printf("%02x ", cBufout[i]);
printf("\r\nValue as char: ");
for (i = 0; i < 32; i++)
printf("%c", cBufout[i]);
printf("\n\r"); *pdwStartAddr = 32;
*pdwLen = 32;
printf("Reading address 32~63:\n\r");
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
cBufout[32] = '\0';
printf("Value as hex : ");
for (i = 0; i < 32; i++)
printf("%02x ", cBufout[i]);
printf("\r\nValue as char: ");
for (i = 0; i < 32; i++)
printf("%c", cBufout[i]);
printf("\n\r"); break;
}
case 'w':
case 'W':
{
pucTmp = cBufIn + 8;
printf("Enter the string to write: ");
scanf("%s", pucTmp);
*pdwStartAddr = 32;
*pdwLen = strlen((const char *)pucTmp);
printf("Write to address 32~%d:\n\r", 32+*pdwLen);
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_WR_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
break;
} case 'c':
case 'C':
{
cBufIn[0] = 0xff;
cBufIn[1] = 0xff;
cBufIn[2] = 0xff;
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_CMP_PSW,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
if (cBufout[0] == 3)
printf("Password compared ok!\r\n");
else
printf("Password compared failed, there is %d times to try.\n\r", cBufout[0]);
break;
}
case 'p':
case 'P':
{
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_PROTECT,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("Protection data: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", cBufout[0], cBufout[1], cBufout[2], cBufout[3]);
break;
}
case 's':
case 'S':
{
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_PSC,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("PSC data: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", cBufout[0], cBufout[1], cBufout[2], cBufout[3]);
break;
}
case 'x':
case 'X':
{
return;
break;
}
}
}
}int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
ICCard_Init();
ICCardTestMenu();
return 0;
}
//#include "stdafx.h"
#include <string.h>#define IO_CTL_ICCARD_RD_ISO 0x01
#define IO_CTL_ICCARD_RD_MAIN 0x02
#define IO_CTL_ICCARD_WR_MAIN 0x04
#define IO_CTL_ICCARD_CMP_PSW 0x08
#define IO_CTL_ICCARD_RD_PROTECT 0x10
#define IO_CTL_ICCARD_RD_PSC 0x20static HANDLE hDrv;int ICCard_Init(void)
{
hDrv = CreateFile(
L"ICC0:",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if (INVALID_HANDLE_VALUE == hDrv)
{
printf("Can't open IICard\n\r");
return -1;
}
return 0;
}void ICCardTestMenu(void)
{
char c;
UINT8 cBufIn[257];
UINT8 cBufout[257];
DWORD dwLenIn;
DWORD dwLenOut;
DWORD dwActualOut;
PDWORD pdwStartAddr = (PDWORD)cBufIn;
PDWORD pdwLen = (PDWORD)(cBufIn + 4); PBYTE pucTmp; BOOL status;
int i;
char str[10];
while (1)
{
printf("\r\n##### IC Card Test Menu #####\r\n");
printf("[I] Read ISO number\n\r");
printf("[R] Read Main memory\n\r");
printf("[W] Write Main memory\n\r");
printf("[C] Compare password as ff ff ff\n\r");
printf("[P] Read Protection Memory\n\r");
printf("[S] Read PSC\n\r");
printf("[X] Press X to quit\n\r");
printf("Enter your selection: "); do {
c = getchar();
} while (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')));
switch (c)
{
case 'i':
case 'I':
{
*pdwStartAddr = 0;
*pdwLen = 4;
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_ISO,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("IC Card ISO number, byte read: %d \n\r", dwActualOut);
for (i = 0; i < 4; i++)
printf("0x%02x ", cBufout[i]);
printf("\n\r");
break;
}
case 'r':
case 'R':
{
*pdwStartAddr = 0;
*pdwLen = 32; printf("Reading address 0~31:\n\r");
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
cBufout[32] = '\0';
printf("Value as hex : ");
for (i = 0; i < 32; i++)
printf("%02x ", cBufout[i]);
printf("\r\nValue as char: ");
for (i = 0; i < 32; i++)
printf("%c", cBufout[i]);
printf("\n\r"); *pdwStartAddr = 32;
*pdwLen = 32;
printf("Reading address 32~63:\n\r");
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
cBufout[32] = '\0';
printf("Value as hex : ");
for (i = 0; i < 32; i++)
printf("%02x ", cBufout[i]);
printf("\r\nValue as char: ");
for (i = 0; i < 32; i++)
printf("%c", cBufout[i]);
printf("\n\r"); break;
}
case 'w':
case 'W':
{
pucTmp = cBufIn + 8;
printf("Enter the string to write: ");
scanf("%s", pucTmp);
*pdwStartAddr = 32;
*pdwLen = strlen((const char *)pucTmp);
printf("Write to address 32~%d:\n\r", 32+*pdwLen);
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_WR_MAIN,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
break;
} case 'c':
case 'C':
{
cBufIn[0] = 0xff;
cBufIn[1] = 0xff;
cBufIn[2] = 0xff;
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_CMP_PSW,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
if (cBufout[0] == 3)
printf("Password compared ok!\r\n");
else
printf("Password compared failed, there is %d times to try.\n\r", cBufout[0]);
break;
}
case 'p':
case 'P':
{
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_PROTECT,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("Protection data: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", cBufout[0], cBufout[1], cBufout[2], cBufout[3]);
break;
}
case 's':
case 'S':
{
status = DeviceIoControl(hDrv,
IO_CTL_ICCARD_RD_PSC,
cBufIn,
dwLenIn,
cBufout,
dwLenOut,
&dwActualOut,
NULL);
printf("PSC data: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", cBufout[0], cBufout[1], cBufout[2], cBufout[3]);
break;
}
case 'x':
case 'X':
{
return;
break;
}
}
}
}int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
ICCard_Init();
ICCardTestMenu();
return 0;
}
解决方案 »
- 这个问题怎么调试?
- 求一正则表达式
- 菜鸟问题 DataView构造函数的参数
- web Service使用Timer组件定时执行的程序不停重入,打乱执行步骤,怎样解决?
- label控件的一个简单问题
- 关于改变图标的问题?好奇怪!!!(在线等回复!)
- 装了虚拟光驱daemon tools后,使用过程中经常出现突然启启的问题
- 2个句柄的问题,高手来看看.
- C#中Navigate2怎么使用,给点例子?
- 求助 insertrow之后后端获取到的行数不变
- 20分请教datagridview动态绑定问题
- 进度条问题(因数据量巨大,所以速度比较慢,为了防止过多占用CPU使用率,我做了个假进度条,只让显示个模式对话框窗体,窗体载入个*.gif的图片)
static const int IO_CTL_ICCARD_RD_ISO = 0x01;
CreateFile就是创建文件,C#里面用File类搞定
Private Const IO_CTL_ICCARD_RD_MAIN As Integer = &H2
Private Const INVALID_HANDLE_VALUE As Integer = -1 Public Declare Function CreateFile Lib "coredll" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByVal lpSecurityAttributes As Int32, _
ByVal dwCreationDisposition As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As Integer) As Integer Private hDrv As Integer Public Declare Function CloseHandle Lib "coredll" _
(ByVal hObject As IntPtr) As Integer Public Declare Function DeviceIoControl Lib "coredll" _
(ByVal hDevice As Integer, _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuffer() As Byte, ByVal _
nInBufferSize As Int32, _
ByVal lpOutBuffer() As Byte, _
ByVal nOutBufferSize As Int32, _
ByVal lpBytesReturned As Int32, _
ByVal lpOverlapped As IntPtr) As Int32 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'初始化
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cBufIn(257) As Byte
Dim cBufout(257) As Byte
Dim dwLenIn As Integer
Dim dwLenOut As Integer
Dim dwActualOut As Integer Dim pdwStartAddr As Integer = cBufIn.Length
Dim pdwLen As Integer = cBufIn.Length + 4
Dim status As Int32
pdwStartAddr = 32
dwLenIn = 32
cBufIn(0) = 32 hDrv = CreateFile("ICC0:", GenericRead Or GenericWrite, 0, Nothing, OpenExisting, FileAttributeNormal, Nothing)
status = DeviceIoControl(hDrv, IO_CTL_ICCARD_RD_MAIN, cBufIn, dwLenIn, cBufout, dwLenOut, dwActualOut, Nothing) Dim tb As String = ""
For i = 0 To 31
tb = tb + Chr(cBufout(i))
Next
TextBox1.Text = tb.Trim
End Sub
如:
[DllImport("kernel32.dll")]
static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);2:看了下你的代码,应该是加载硬件句柄,并进行操作,所以只能API。不能用如File类。3:其它的没有什么难度。