读书人

C#接收自定义消息有关问题

发布时间: 2012-08-02 11:35:26 作者: rapoo

C#接收自定义消息问题!
最近做一个接口,将酒店门锁卡集成到我们的酒馆系统中,客人开一间房就直接用我们的程序制卡实现开门。
门锁厂家提供的是delphi的dll接口,我的是c# winform程序。我用[DllImport("delphi.dll")]方式将其导入到我的程序中,按照dll接口说明可以实现制卡,但是每次制卡成功后我的酒馆程序就关闭了。原因排查后大概就是由我的自定义消息引起的。
当我把我自定义消息注释掉后,是卡制成功后程序退出,不注释自定义消息时,卡刚放到刷卡机上就退出(都没制成功)。
我在网上搜了自定义消息 ,重载了windows 的DefWndProc()方法。代码如下:
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
int i = 0;
IntPtr iaddr = m.LParam;
string readCardStr = ""; //读卡返回的信息
switch (m.Msg)
{
//检测到卡片(卡片有效)
case PWM_CARDVAILD:
i = YDD.ReadCard(iaddr, ref readCardStr);

listBox1.Items.Clear();
listBox1.Items.Add(readCardStr);
break;
//未检测到卡片(卡片无效)
case PWM_CARDNOVAILD:
listBox1.Items.Add("卡片无效,这是自定义消息");
break;
//写卡失败
case PWM_WRITEFAIL:
//MessageBox.Show("写卡失败,这是自定义消息");
listBox1.Items.Add("写卡失败,这是自定义消息");
break;
//系统授权成功
case PWM_GETSYSCODE:

listBox1.Items.Add("系统授权成功,这是自定义消息");
break;
case PWM_GETREADERDATA:
i = YDD.ReadCard(iaddr, ref readCardStr);
listBox1.Items.Add("获得开锁记录,这是自定义消息");
break;
default:
base.DefWndProc(ref m);
break;
}
}
到这里后,我不清楚程序为什么会自动退出,具体是什么造成的,而且也不能读卡,求高人帮我解决,实在头疼!



[解决办法]
base.DefWndProc(ref m); 放到switch外看看,不要拦截消息
[解决办法]
学习,再帮顶。
[解决办法]
估计是异常退出了,检查dll调用部分,
[解决办法]
估计遇到了没有捕获的异常。
你在 Main 方法中的Application.Run之前写一句

C# code
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);//捕获应用程序线程异常,这样会把没有捕获的异常抓取到。
[解决办法]
先确定是调用哪个函数出错的;
[解决办法]
你能打断点测试一下么?我估计是程序有异常你没捕获所以自动释放资源了。。。
我原先做过一个读卡制卡的功能,代码给你参考下:
//c++函数强制声明
[DllImport("mi.dll", EntryPoint = "API_OpenComm")]
public static extern IntPtr API_OpenComm(int nCom, int nBaudrate);

[DllImport("mi.dll", EntryPoint = "API_CloseComm")]
public static extern bool API_CloseComm(IntPtr commHandle);

[DllImport("mi.dll", EntryPoint = "API_ControlBuzzer")]
public static extern int API_ControlBuzzer(IntPtr commHandle, int DeviceAddress, byte freq, byte duration, ref byte buffer);

[DllImport("mi.dll", EntryPoint = "API_MF_Read")]
public static extern int API_MF_Read(IntPtr commHandle, int DeviceAddress, byte mode, byte blk_add, byte num_blk, ref byte snr, ref byte buffer);

//制卡
private void makeCard(object sender, DataGridViewCellEventArgs e)


{
DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1.Rows[e.RowIndex].Cells["Buttons"];

if (buttonCell.Enabled)
{
if (MessageBox.Show("您确定要置卡?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//连接读卡器
dataGridView1.CurrentRow.Cells[0].Value = true;
Program.Port = API_OpenComm(4, 9600);
if (Program.Port.ToInt32() == 0)
{
MessageBox.Show("读卡器连接失败!");
return;
}
byte pBuf = new byte();
Program.G_start = API_ControlBuzzer(Program.Port, 0, 3, 1, ref pBuf);
if (Program.G_start == 0)
{
//MessageBox.Show("读卡器连接成功!");
}

byte[] hex = new byte[16];
long ret = long.Parse("281474976710655");
hex = BitConverter.GetBytes(ret);
byte[] hex1 = new byte[16];
byte[] hex2 = new byte[16];
byte[] data = new byte[16];

int Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ColumnId"].Value.ToString());

//写卡信息方法
hex1 = BitConverter.GetBytes(Convert.ToInt64(Id.ToString()));
for (int i = 0; i < 8; i++)
{
hex2[i] = hex1[i];
}
int idata = API_MF_Write(Program.Port, 0, 0, 10, 1, ref hex[0], ref hex2[0]);
API_CloseComm(Program.Port);
//无卡接触时
if (idata != 0)
{
MessageBoxEx.Show("写卡有误,请确认卡放在指定位置!");
return;
}
Program.G_start = API_ControlBuzzer(Program.Port, 0, 3, 1, ref pBuf);

MessageBoxEx.Show("保存成功,请取卡!", "提示");
if (API_CloseComm(Program.Port))
{
Program.G_start = 1;// 串口打开标识
}

//数据库修改其置卡状态
member.Fabrication = "是";
member.MemberId = Id;
member.UpdateCard();
Bind();
//为了不再次查数据库 直接在dataGridView1中改其状态
//dataGridView1.Rows[e.RowIndex].Cells["ColumnFabrication"].Value = "是";
// buttonCell.UseColumnTextForButtonValue = false;
buttonCell.Enabled = false;
}

}
}

读书人网 >C#

热点推荐