使用SDK开发短信收发平台,收短信使用回调,经过一番修改,终于可正常接收,但是发现一个问题:如果有2条短信需要接收,显示接收到的数据一样,包括号码、内容、时间都是一样的,具体代码如下:
//引用DLL
[DllImport("EUCPComm.dll", EntryPoint = "ReceiveSMSEx")] // 接收短信
public static extern int ReceiveSMSEx(string sn, deleSQF mySmsContent);
//回调(接收短信)
static void getSMSContent(string mobile, string senderaddi, string recvaddi, string ct, string sd, ref int flag)
{
string mob = mobile;
string content = ct;
int myflag = flag;
//在此处将短信内容存入datatable
DataRow RecRow = TempDT.NewRow();
RecRow[0] = mobile;
RecRow[1] = ct;
RecRow[2] = sd;
TempDT.Rows.Add(RecRow);
}
public delegate void deleSQF(string mobile, string senderaddi, string recvaddi, string ct, string sd, ref int flag);
deleSQF mySmsContent = new deleSQF(getSMSContent);
//声明静态的DataTable
public static DataTable TempDT = new DataTable();
//Form1_Load中初始化DataTable
private void Form1_Load(object sender, EventArgs e)
{
TempDT.Columns.Add("mob");
TempDT.Columns.Add("con");
TempDT.Columns.Add("rtime");
}
//button5单击事件收取短信
private void button5_Click(object sender, EventArgs e)
{
deleSQF mySmsContent = new deleSQF(getSMSContent);
//接收短信 序列号 函数指针
int result = 2;
while (result == 2) //当result = 2 时,说明还有下一批短信等待接收,这时需重新再调用一次ReceiveSMS方法
{
result = ReceiveSMSEx(textBox1.Text, mySmsContent);
if (result == 1)
{
textBox3.Text += "接收短信成功:"+DateTime.Now.ToString()+"\r\n";
}
else if (result == 101)
{
textBox3.Text += "网络故障:" + DateTime.Now.ToString() + "\r\n";
}
else if (result == 102)
{
textBox3.Text += "其它故障:" + DateTime.Now.ToString() + "\r\n";
}
else if (result == 105)
{
textBox3.Text += "参数指针为空:" + DateTime.Now.ToString() + "'\r\n";
}
else if (result == 0)
{
textBox3.Text += "失败:" + DateTime.Now.ToString() + "\r\n";
}
else if (result == -1)
{
textBox3.Text += "未知故障:" + DateTime.Now.ToString() + "\r\n";
}
}
//如果TempDT有记录
if (TempDT.Rows.Count > 0)
{
foreach (DataRow row in TempDT.Rows)
{
//显示短信号码、内容、时间。就这里出错,如果有2条未接受短信,点接收时,textBox3中显示接受到的两条记录是一样的
textBox3.Text += row[0].ToString() + "/" + row[1].ToString() + "/" + row[2].ToString() + "\r\n";
}
清空TempDT
TempDT.Clear();
}
}
从程序本身看没有什么问题,主要要看你对 EUCPComm.dll 这个动态库中相关API函数的调用方法是否正确。
你看看这个方法是怎么处理的
ReceiveSMSEx