using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // 为了使用DllImport
using System.Text;
namespace WindowsApplication2
{
static class Program
{
///
/// 应用程序的主入口点。
///
///
[STAThread]
[DllImport("BM32.dll")]
internal static extern IntPtr bm_init(int comPortNumber, int baudrate);
[DllImport("BM32.dll")]
internal static extern Int16 bm_type_set(IntPtr dev, int cardtype);
[DllImport("BM32.dll")]
internal static extern Int16 bm_page(IntPtr dev, int pageno);
static void Main()
{
IntPtr dev;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
dev = bm_init(1, 4800);
if (dev != null)
{
MessageBox.Show("dd"); //连上读卡器后可以弹出这个对话框。这个函数在这个应该是成功了吧。
}
else
{
MessageBox.Show("ee");
}
short ret;
ret = bm_type_set(dev, 6);
if (ret == 0)
{
MessageBox.Show("type set ok!");//连上读卡器后可以弹出这个对话框。这个函数在这个应该是成功了吧。
}
else
{
MessageBox.Show("type set nook!");
}
unsafe
{
Int16 tt;
//fixed char name[30];
byte[] cc = new byte[1024];
int i;
for (i = 0; i < 1024; i++)
cc[i] = 0;
// string d = "";
//MessageBox.Show(cc[0].ToString());
fixed (Byte * p = cc)
{
tt = bm_page(dev, 0);
tt = UnmanagedWrapper.bm_read(dev, 0, 16, p);
if (tt == 0)
{
MessageBox.Show(cc[0].ToString());//连上读卡器后可以弹出这个对话框。但读到的字符不正确。
}
else
{
MessageBox.Show("read faild!");
}
//byte[] ByteArray2 = { 0x30, 0xe8, 108, 108, 111, 32, 87, 111, 114, 108, 100 };
string dte = System.Text.Encoding.Default.GetString(cc);
MessageBox.Show(dte);
/*
ASCIIEncoding AE2 = new ASCIIEncoding();
char[] CharArray = AE2.GetChars(cc);
String str = "";
for (int x = 0; x <= 16; x++)
{
//Console.Write(CharArray[x]);
str += (char)(cc[x]);
}
MessageBox.Show(str);*/
}
}
}
unsafe internal static class UnmanagedWrapper
{
[DllImport("BM32.dll", CharSet = CharSet.Unicode)]
internal static extern Int16 bm_read(IntPtr dev_id, int add, int bytes, byte* p);
[DllImport("BM32.dll", CharSet = CharSet.Unicode)]
internal static extern Int16 bm_read_hex(IntPtr dev_id, int add, int bytes, byte* p);
}
}
}
阅读(823) | 评论(0) | 转发(0) |