2013-06-26 55 views
0

我一直在尝试从Windows CE 5.0设备发送短信。我在网上获得了一些样本,全部使用“sms.dll”,但他们似乎没有工作。我开始认为他们只能在6.0下工作。有没有我可以用来从5.0发送的API?如何从Windows CE 5.0发送短信?

+0

我已经为我的C#代码提供了一个SMS例程,但是我从来没有能够得到它的工作。我所得到的是我的自定义例外,说“短信失败”。如果你愿意,我可以发布我是如何做到的,但就像我说的 - 我的版本也失败了。 – jp2code

+0

请发布。在这一点上值得一看。谢谢。 – Yves

回答

0

不能说我明白我在下面做了什么,就像我说的 - 我从来没有得到这个为我工作。

这就是说,这是我使用的类,从上到下分解了一些解释。

首先,命名空间包括:

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的定义,我的班级有可能失败 - 我可以诚实地说我不理解所有这些。我只是把它复制下来。

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 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; 
    } 

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

如果收到短信,它应该被这个拦截“拦截”。我认为你应该在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*/ 
     } 
     } 
    } 

好吧,就是这样!我希望它有帮助。