📄 form1.cs
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using System.Runtime.InteropServices;
namespace SmartDeviceOutlookDemo
{
public partial class Form1 : Form
{
public static long SIM_SMSSTORAGE_SIM = 0x00000002;
public static long SIM_SMSSTORAGE_BROADCAST = 0x00000001;
public static long SIM_NUMSMSSTORAGES = 2;
public static long SIM_PARAM_MSG_ALL = 0x0000007f;
public static long SIM_INIT_NONE =0x00000000; // @constdefine Do not send any notifications
public static long SIM_INIT_SIMCARD_NOTIFICATIONS = 0x00000001; // @constdefine Send SIM card related notifications
public Form1()
{
InitializeComponent();
}
private void menuItem3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void menuItem4_Click(object sender, EventArgs e)
{
MessageInterceptor msgInterceptor = new MessageInterceptor();
msgInterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
MessageCondition msgCondition = new MessageCondition();
msgCondition.ComparisonType = MessagePropertyComparisonType.Contains;
msgCondition.Property = MessageProperty.Sender;
msgCondition.ComparisonValue = "Test Man";
msgInterceptor.MessageCondition = msgCondition;
msgInterceptor.MessageReceived += new MessageInterceptorEventHandler(msgInterceptor_MessageReceived);
}
void msgInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
this.listBox1.Items.Add("Type Name: " + e.Message.GetType().Name);
if (e.Message.GetType() == typeof(SmsMessage))
{
SmsMessage sms = (SmsMessage)e.Message;
this.listBox1.Items.Add("From: " + sms.From.Name);
this.listBox1.Items.Add("Body: " + sms.Body);
this.listBox1.Items.Add("Received Tiem: " + sms.Received.ToString("yyyy-MM-dd"));
}
if (e.Message.GetType() == typeof(EmailMessage))
{
EmailMessage mail = (EmailMessage)e.Message;
this.listBox1.Items.Add("ItemId: " + mail.ItemId.ToString());
}
}
[StructLayout(LayoutKind.Sequential)]
private struct SimRecord
{
public IntPtr cbSize;
public IntPtr dwParams;
public IntPtr dwRecordType;
public IntPtr dwItemCount;
public IntPtr dwSize;
}
[DllImport("cellcore.dll", SetLastError=true)]
private static extern int SimInitialize(int dwFlags, int lpfnCallBack, int dwParam, out int lphSim);
[DllImport("cellcore.dll", SetLastError = true)]
private static extern int SimDeinitialize(int hSim);
[DllImport("cellcore.dll", SetLastError = true)]
public static extern int SimGetSmsStorageStatus(int hSim, int dwStorage, ref int lpdwUsed, ref int lpdwTotal);
[DllImport("cellcore.dll", SetLastError = true)]
private static extern int SimWriteMessage(int hSim, int dwStorage, ref int lpdwIndex, ref SimMessageTag SmsStructType);
[DllImport("cellcore.dll", SetLastError = true)]
private static extern int SimReadMessage(int hSim, int dwStorage, int lpdwIndex, ref SimMessageTag SmsStructType);
[DllImport("cellcore.dll", SetLastError = true)]
private static extern int SimDeleteMessage(int hSim, int dwStorage, ref int lpdwIndex);
private void menuItem6_Click(object sender, EventArgs e)
{
int hSim = 0, res = 0;
try
{
res = SimInitialize((int)SIM_INIT_NONE, 0, (int)SIM_PARAM_MSG_ALL, out hSim);
if (res != 0)
throw new Exception("Could not initialize SIM");
int used = 0, total = 0;
res = SimGetSmsStorageStatus(hSim, (int)SIM_NUMSMSSTORAGES, ref used, ref total);
if (res == 0)
{
this.listBox1.Items.Add("Used: " + used.ToString());
this.listBox1.Items.Add("Total: " + total.ToString());
}
else
{
this.listBox1.Items.Add("Last Error: " + Marshal.GetLastWin32Error().ToString());
}
SimMessageTag message = new SimMessageTag();
int index = 1;
//res = SimDeleteMessage(hSim, (int)SIM_SMSSTORAGE_BROADCAST, ref index);
//if (res != 0)
//{
// this.listBox1.Items.Add("Last Error: " + Marshal.GetLastWin32Error().ToString());
//}
//this.listBox1.Items.Add("Index: " + index.ToString());
for (int j = 1; j <= used; j++)
{
res = SimReadMessage(hSim, (int)SIM_NUMSMSSTORAGES, j, ref message);
if (res == 0)
{
this.listBox1.Items.Add("From: " + message.lpszAddress);
this.listBox1.Items.Add("CBSize: " + message.cbSize.ToString());
this.listBox1.Items.Add("Address Type: " + message.dwAddressType.ToString());
this.listBox1.Items.Add("NumPlan: " + message.dwNumPlan.ToString());
this.listBox1.Items.Add("Params: " + message.dwParams.ToString());
char[] header = new char[message.rgbHeader.Length];
string msg = "";
for (int i = 0; i < message.rgbHeader.Length; i++)
{
header[i] = (char)message.rgbHeader[i];
msg += header[i].ToString();
}
this.listBox1.Items.Add("Header: " + msg);
this.listBox1.Items.Add("Receive Time: " + message.stReceiveTime.ToString());
this.listBox1.Items.Add("Message: " + message.lpszMessage);
this.listBox1.Items.Add("");
}
else
{
this.listBox1.Items.Add("Last Error: " + Marshal.GetLastWin32Error().ToString());
}
}
SimMessageTag msg1 = new SimMessageTag();
msg1.cbSize = message.cbSize;
msg1.dwAddressType =1;
msg1.dwNumPlan = 1;
msg1.dwParams = 111;
msg1.lpszAddress = "123456789";
msg1.stReceiveTime = new global::SystemTime(System.DateTime.Now);
msg1.lpszMessage = "It is a test mail!";
msg1.cbHdrLength = 0;
msg1.rgbHeader = new byte[256];
index = used + 1;
res = SimWriteMessage(hSim, (int)SIM_NUMSMSSTORAGES, ref index, ref msg1);
if (res != 0)
{
this.listBox1.Items.Add("Last Error: " + Marshal.GetLastWin32Error().ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.listBox1.Items.Add("Result: " + res.ToString());
SimDeinitialize(hSim);
}
}
private void menuItem7_Click(object sender, EventArgs e)
{
OutlookSession session = new OutlookSession();
MessagingApplication.Synchronize(session.SmsAccount);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
public SystemTime(System.DateTime now)
{
wYear = (short)now.Year;
wMonth = (short)now.Month;
wDayOfWeek = (short)now.DayOfWeek;
wDay = (short)now.Day;
wHour = (short)now.Hour;
wMinute = (short)now.Minute;
wSecond = (short)now.Second;
wMilliseconds = (short)now.Millisecond;
}
public override string ToString()
{
return string.Format("{0}/{1}/{2}", wYear, wMonth, wDay);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SimMessageTag
{
public int cbSize; // Size of the structure in bytes
public int dwParams; //Indicates valid parameter values
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszAddress; //An array that contains the actual phone number
public int dwAddressType; //A SIM_ADDRTYPE constant
/*
SIM_ADDRTYPE_UNKNOWN = Unknown.
SIM_ADDRTYPE_INTERNATIONAL = International number.
SIM_ADDRTYPE_NATIONAL 0ne National = number.
SIM_ADDRTYPE_NETWKSPECIFIC Network = specific number.
SIM_ADDRTYPE_SUBSCRIBER Subscriber = number
(protocol-specific).
SIM_ADDRTYPE_ALPHANUM Alphanumeric = address.
SIM_ADDRTYPE_ABBREV Abbreviated = number.
*/
public int dwNumPlan; //A SIM_NUMPLAN constant
/*
SIM_NUMPLAN_UNKNOWN = Unknown.
SIM_NUMPLAN_TELEPHONE = ISDN/telephone numbering plan
(E.164/E.163).
SIM_NUMPLAN_DATA = Data numbering plan (X.121).
SIM_NUMPLAN_TELEX = Telex numbering plan.
SIM_NUMPLAN_NATIONAL = National numbering plan.
SIM_NUMPLAN_PRIVATE = Private numbering plan.
SIM_NUMPLAN_ERMES ERMES = numbering plan (ETSI DE/PS 3 01-3).
*/
public SystemTime stReceiveTime; //Timestamp for the incoming message
public int cbHdrLength; //Header length in bytes
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] rgbHeader; //An array containing the actual header data
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszMessage; //An array containing the actual message data
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -