using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Microsoft.Wireless
{
/// <summary>
/// Identifies the phone number type specified.
/// </summary>
public enum AddressType
{
/// <summary>Unknown phone number type.</summary>
Unknown,
/// <summary>International phone number.</summary>
International,
/// <summary>National phone number.</summary>
National,
/// <summary>Network-specific phone number.</summary>
NetworkSpecific,
/// <summary>Subscriber phone number.</summary>
Subscriber,
/// <summary>Alphanumeric phone number.</summary>
Alphanumeric,
/// <summary>Abbreviated phone number.</summary>
Abbreviated
}
/// <summary>
/// Information about the phone number.
/// </summary>
public struct PhoneAddress
{
/// <summary>The address type.</summary>
public AddressType AddressType;
/// <summary>The phone number in string format.</summary>
public String Address;
}
/// <summary>
/// Short Message Service.
/// </summary>
public class SMS
{
private static string SMS_MSGTYPE_TEXT = "Microsoft Text SMS Protocol";
private static long SMS_MODE_SEND = 0x00000002;
private static long SMS_OPTION_DELIVERY_NONE = 0x00000000;
private static long SMS_OPTION_DELIVERY_NO_RETRY = 0x00000001;
private static long PS_MESSAGE_OPTION_NONE = 0x00000000;
private enum SMS_DATA_ENCODING
{
SMSDE_OPTIMAL = 0,
SMSDE_GSM,
SMSDE_UCS2,
}
private enum PROVIDER_SPECIFIC_MESSAGE_CLASS
{
PS_MESSAGE_CLASS0 = 0,
PS_MESSAGE_CLASS1,
PS_MESSAGE_CLASS2,
PS_MESSAGE_CLASS3,
}
private enum PROVIDER_SPECIFIC_REPLACE_OPTION
{
PSRO_NONE = 0,
PSRO_REPLACE_TYPE1,
PSRO_REPLACE_TYPE2,
PSRO_REPLACE_TYPE3,
PSRO_REPLACE_TYPE4,
PSRO_REPLACE_TYPE5,
PSRO_REPLACE_TYPE6,
PSRO_REPLACE_TYPE7,
PSRO_RETURN_CALL,
PSRO_DEPERSONALIZATION,
}
private struct TEXT_PROVIDER_SPECIFIC_DATA
{
public IntPtr dwMessageOptions;
public PROVIDER_SPECIFIC_MESSAGE_CLASS psMessageClass;
public PROVIDER_SPECIFIC_REPLACE_OPTION psReplaceOption;
}
[DllImport("sms.dll")]
private static extern IntPtr SmsOpen(String ptsMessageProtocol, IntPtr dwMessageModes, ref IntPtr psmshHandle, IntPtr phMessageAvailableEvent);
[DllImport("sms.dll")]
private static extern IntPtr SmsSendMessage(IntPtr smshHandle, IntPtr psmsaSMSCAddress, IntPtr psmsaDestinationAddress, IntPtr pstValidityPeriod, byte[] pbData, IntPtr dwDataSize,
byte[] pbProviderSpecificData, IntPtr dwProviderSpecificDataSize, SMS_DATA_ENCODING smsdeDataEncoding,
IntPtr dwOptions, IntPtr psmsmidMessageID);
[DllImport("sms.dll")]
private static extern IntPtr SmsClose(IntPtr smshHandle);
/// <summary>
/// Sends a SMS message to the phone number specified.
/// </summary>
/// <param name="sPhoneNumber">The phone number of the recipent</param>
/// <param name="sMessage">The message to send.</param>
unsafe public static void SendMessage(string sPhoneNumber, string sMessage)
{
IntPtr hSms = IntPtr.Zero;
try
{
IntPtr res = SmsOpen(SMS_MSGTYPE_TEXT, (IntPtr)SMS_MODE_SEND, ref hSms, IntPtr.Zero);
if (res != IntPtr.Zero)
throw new Exception("Could not open SMS.");
Byte[] bDest = new Byte[516];
fixed (byte* pAddr = bDest)
{
byte* pCurrent = pAddr;
Marshal.WriteInt32((IntPtr)pCurrent, (int)AddressType.Unknown);
pCurrent += 4;
foreach (byte b in Encoding.Unicode.GetBytes(sPhoneNumber))
{
Marshal.WriteByte((IntPtr)pCurrent, b);
pCurrent++;
}
// The data for the TEXT_PROVIDER_SPECIFIC_DATA
Byte[] ProvData = new Byte[12];
byte[] bMessage = Encoding.Unicode.GetBytes(sMessage);
int nMsgSize = bMessage.Length;
res = SmsSendMessage(hSms, IntPtr.Zero, (IntPtr)pAddr, IntPtr.Zero, bMessage, (IntPtr)nMsgSize,
ProvData, (IntPtr)ProvData.Length, SMS_DATA_ENCODING.SMSDE_OPTIMAL, (IntPtr)SMS_OPTION_DELIVERY_NONE, IntPtr.Zero);
if (res != IntPtr.Zero)
throw new Exception("SMS send failed.");
}
}
finally
{
if (hSms != IntPtr.Zero)
SmsClose(hSms);
}
}
}
}