// 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;
}

解决方案 »

  1.   

    跟C#差不多的主要就是调用DeviceIoControl这个API,C#里面怎么调用API上网搜索一下就知道了#define后面的内容你就当成一个数字,在C#里面可以这样
    static const int IO_CTL_ICCARD_RD_ISO = 0x01;
    CreateFile就是创建文件,C#里面用File类搞定
      

  2.   

    CreateFile就是创建文件,C#里面用File类搞定
      

  3.   

    就是创建文件,查MSDN中的File类,声明的常量,用const
      

  4.   

    我已转为vb.net 但CreateFile 得到设备不对,读设备也不对,请各位看看问题所在    Private Const ERROR_INSUFFICIENT_BUFFER As Integer = 122    Private Const ERROR_INVALID_NAME As Integer = 123    Private Const GenericRead As Integer = &H80000000    Private Const GenericWrite As Integer = &H40000000    Private Const FileAttributeNormal As Integer = &H80    Private Const FileShareRead As Integer = 1    Private Const FileShareWrite As Integer = 2    Private Const IOCTL_DISK_GET_STORAGEID As UInt32 = &H71C24    Private Const OpenExisting As Integer = 3
        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
      

  5.   

    你这是Windows程序还是Windows Mobile程序?你的VB代码用core.dll是Windows Mobile的类库。在pc机上执行显然会失败。
      

  6.   

    1:非托管代码调用,可到www.pinvoke.net查阅。
    如:
    [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:其它的没有什么难度。
      

  7.   

    这个是Windows ce 5.0 要用的程序
      

  8.   

    前面的c代码调试通过,转换为vb.net 现在调不过去