如何实现C#中在系统收件箱中创建短信?
jufow 2008-07-26 07:31:08 我看了Appleseeker的博客后,使用vs2005在wm5下建立了项目文件,在真机上读短信正常,但是不能写短信,错误SIM_E_SIMDOWNLOADERROR,请大家提示看看是什么问题?cellcore.dll是在网上找的
namespace DeviceApplication28
{
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();
}
[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 listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click_1(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_SMSSTORAGE_BROADCAST, 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;
for (int j = 1; j <= used; j++)
{
res = SimReadMessage(hSim, (int)SIM_SMSSTORAGE_BROADCAST, j, ref message);
if (res == 0)
{//显示短信
}
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_SMSSTORAGE_BROADCAST, 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 listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
[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
}