DLL程序代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataLogic
{
public partial class Form1 : Form
{
public static IList<string> strlist =new List<string>();
public static string va = "";
public Form1()
{
InitializeComponent();
}
public void StartLink()
{
for (int i = 1; i < 10; i++)
{
axRFNCCTL1.Devices.Add(i);
}
axRFNCCTL1.CommPort = "COM4";
axRFNCCTL1.BaudRate = "38400";
axRFNCCTL1.OpenLine();
axRFNCCTL1.SetLostFrameBehaviour(true, 1, 2);
axRFNCCTL1.Satellites.Add(2);
axRFNCCTL1.Satellites.get_Satellite(2).Alive();
TBStarLink.Text = Convert.ToDateTime(DateTime.Now).ToShortTimeString();
axRFNCCTL1.OpenNetwork();
}
public void OpenLink()
{
axRFNCCTL1.CloseNetwork();
}
/// <summary>
/// 扫描枪发送接收事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axRFNCCTL1_DataEvent(object sender, AxNRFCCTLCONTROLLib._IRFNCCTLEvents_DataEventEvent e)
{
string a = axRFNCCTL1.Devices.get_Device(e.devID).DeviceData.ToString();
setArray(a);
axRFNCCTL1.Devices.get_Device(e.devID).DataWrite(a + result, (short)(a.Length + a.Length));
}
/// <summary>
/// 扫描枪连接事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axRFNCCTL1_AliveEvent(object sender, AxNRFCCTLCONTROLLib._IRFNCCTLEvents_AliveEventEvent e)
{
TBStarLink.Text = "连接AP成功!";
}
public bool setArray(string str)
{
strlist.Add(str);
return true;
}
public static string getArray()
{
foreach (string str in strlist)
{
va += str + "*";
}
strlist.Clear();
return va;
}
}
主程序代码(动态调用DLL)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Test
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private object Invoke(string lpFileName, string Namespace, string ClassName, string lpProcName)
{
try
{ // 载入程序集
Assembly MyAssembly = Assembly.LoadFrom(lpFileName);
Type[] type = MyAssembly.GetTypes();
foreach (Type t in type)
{// 查找要调用的命名空间及类
if (t.Namespace == Namespace && t.Name == ClassName)
{// 查找要调用的方法并进行调用
MethodInfo m = t.GetMethod(lpProcName);
if (m != null)
{
object o = Activator.CreateInstance(t);
//string s = (string)m.Invoke(o, null);
return m.Invoke(o,null);
}
else MessageBox.Show(" 装载出错 !");
}
}
}//try
catch (System.NullReferenceException e)
{
MessageBox.Show(e.Message);
}//catch
return (object)0;
}
/// <summary>
/// 取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Invoke("DataLogic.exe", "DataLogic", "Form1", "getArray").ToString());
}
/// <summary>
/// 启动扫描枪
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Invoke("DataLogic.exe", "DataLogic", "Form1", "StartLink");
}
/// <summary>
/// 关闭扫描枪
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Invoke("DataLogic.exe", "DataLogic", "Form1", "OpenLink");
}
}
}
首先我点击button2_Click启动扫描枪,然后使用扫描枪扫条码,这是DLL的axRFNCCTL1_DataEvent事件会触发接收数据并调用setArray()方法存储数据,这是strlist已有数据,我点击button3_Click取数据,取到扫的条码数据。但这时我再扫条码axRFNCCTL1_DataEvent事件就不触发了,我不知道为什么,一但我点击button3_Click事件就再不能扫描,axRFNCCTL1_DataEvent事件也就不会触发了,我郁闷了!~不知道为什么
如果我不button3_Click,就一直可以扫描,axRFNCCTL1_DataEvent事件也一直触发。
大家帮我想想为什么,好奇怪啊!!!!!!!
if (s == false)
{
o = Activator.CreateInstance(t);
s = true;
}
调用一次