[英]How do I send SMS from Windows CE 5.0?
我一直在尝试从Windows CE 5.0设备发送SMS。 我在网上有一些样本,全部使用“ sms.dll”,但它们似乎无法正常工作。 我开始认为它们仅适用于6.0。 有可以从5.0发送的API吗?
不能说我理解我在下面所做的事情,就像我说的那样-我从来没有为我工作。
就是说,这是我自上而下使用的课程,并附有一些解释。
首先,名称空间包括:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.Win32;
using Microsoft.WindowsMobile.PocketOutlook;
using System.Runtime.InteropServices;
我将其放在自己的名称空间中,以使其不会干扰我的其他东西,然后声明一些常量:
namespace MobileSMS {
class SmsClass {
private const Int32 FILE_DEVICE_HAL = 0x00000101;
private const Int32 FILE_ANY_ACCESS = 0x0;
private const Int32 METHOD_BUFFERED = 0x0;
private static readonly Int32 IOCTL_HAL_GET_DEVICEID =
((FILE_DEVICE_HAL) << 16) |
((FILE_ANY_ACCESS) << 14) |
((21) << 2) | (METHOD_BUFFERED);
private const string NO_NAME = "[Unnamed]";
private const string COREDLL = "coredll.dll";
[DllImport(COREDLL)]
private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
}
}
这些是我上面所有的常数。
private string m_name;
private MessageInterceptor m_sms;
public SmsClass() {
m_name = null;
Exception error = null;
try {
m_sms = new MessageInterceptor(DeviceName, false);
m_sms.InterceptionAction = InterceptionAction.NotifyAndDelete;
m_sms.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, DeviceName);
m_sms.MessageReceived += new MessageInterceptorEventHandler(Intercept_MessageReceived);
m_sms.EnableApplicationLauncher(DeviceName);
} catch (Exception err) {
error = err; // just because there was an error doesn't mean it might not have been enabled.
}
if (!MessageInterceptor.IsApplicationLauncherEnabled(DeviceName)) {
Console.WriteLine("Unable to load SMS Tool: " + error.Message);
}
}
我的总是在构造函数中失败。 不要在构造函数中抛出错误(如果您不知道的话),否则该类会产生奇怪的行为。
我的课堂可能因为我对IOCTL_HAL_GET_DEVICEID
定义而IOCTL_HAL_GET_DEVICEID
-可以说我不明白所有这些。 我只是抄下来。
DeviceName
必须是唯一的,这样一台设备在发送消息时就可以与另一台设备区分开(我听到了)。 这是我获得DeviceName
:它首先在注册表中搜索条目,如果在注册表中找不到任何内容,我将使用在Microsoft上找到的东西来获得唯一的序列号(但实际上回来时看起来更像是GUI)。
public string DeviceName {
get {
if (String.IsNullOrEmpty(m_name)) {
m_name = getName();
}
return m_name;
}
set {
if (m_name != value) {
m_name = value;
}
}
}
这是我尝试从注册表中读取我的值:
private static string getName() {
string name = null;
using (var key = Registry.LocalMachine.OpenSubKey("Ident", true)) {
name = key.GetValue("Name", NO_NAME) as string;
}
if (String.IsNullOrEmpty(name)) {
name = getDeviceID();
}
return name;
}
如果没有任何帮助,我将使用在MSDN上找到的以下代码获取设备ID:
private static string getDeviceID() {
// Reference: http://msdn.microsoft.com/en-us/library/aa446562.aspx
byte[] data = new byte[256];
Int32 OutputBufferSize = data.Length;
Int32 BytesReturned = 0;
// Call KernelIoControl passing the previously defined IOCTL_HAL_GET_DEVICEID parameter
// We don’t need to pass any input buffers to this call
// so InputBuffer and InputBufferSize are set to their null values
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, data, OutputBufferSize, ref BytesReturned);
// If the request failed, exit the method now
if (retVal) {
// Examine the OutputBuffer byte array to find the start of the
// Preset ID and Platform ID, as well as the size of the PlatformID.
// PresetIDOffset – The number of bytes the preset ID is offset from the beginning of the structure
// PlatformIDOffset - The number of bytes the platform ID is offset from the beginning of the structure
// PlatformIDSize - The number of bytes used to store the platform ID
// Use BitConverter.ToInt32() to convert from byte[] to int
Int32 PresetIDOffset = BitConverter.ToInt32(data, 4);
Int32 PlatformIDOffset = BitConverter.ToInt32(data, 0xc);
Int32 PlatformIDSize = BitConverter.ToInt32(data, 0x10);
// Convert the Preset ID segments into a string so they can be
// displayed easily.
StringBuilder sb = new StringBuilder();
sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
BitConverter.ToInt32(data, PresetIDOffset),
BitConverter.ToInt16(data, PresetIDOffset + 4),
BitConverter.ToInt16(data, PresetIDOffset + 6),
BitConverter.ToInt16(data, PresetIDOffset + 8)));
// Break the Platform ID down into 2-digit hexadecimal numbers
// and append them to the Preset ID. This will result in a
// string-formatted Device ID
for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++) {
sb.Append(String.Format("{0:X2}", data[i]));
}
// return the Device ID string
return sb.ToString();
}
return null;
}
如果曾经收到过SMS消息,则此拦截应将其“捕获”。 我认为您应该在command
放置一些唯一的内容,以便可以将其标识为来自其他设备之一的消息,而不是随机尝试将其发送给正在收听的任何内容的消息。 不过,我从没走过那么远,因为我的例程在构造函数中总是失败。
private static void Intercept_MessageReceived(object sender, MessageInterceptorEventArgs e) {
var newMessage = (SmsMessage)e.Message;
if (newMessage != null) {
Console.WriteLine("From: {0}", newMessage.From.Address);
Console.WriteLine("Body: {0}", newMessage.Body);
string[] command = newMessage.Body.Split(new char[] { '.' });
string line = command[1];
if (line == "helo") {
/*do some Stuff*/
}
}
}
好吧,这就是全部! 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.