在5.0以前的版本,虽然现在使用量比较小了,但是在2年前,在其上面发送短息可以说很是费劲,当时确实还让我头疼了一段时间,需要用户自己封装底层的API,然后进行调用,我这里有一个,贴出来,以供大家参考
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SmartSMS
{
/// <summary>
/// 短信发送程序
/// </summary>
public class SMS
{
public SMS())
{
// TODO: 在此处添加构造函数逻辑
//
}
private const string SMS_MSGTYPE_TEXT = "Microsoft Text SMS Protocol";
private const int SMS_MODE_SEND = 2;
// Open in send mode
private const int SMSDE_GSM = 2;
// Use standard GSM encoding;
private const int SMSAT_INTERNATIONAL = 1;
// International number format
private const int PS_MESSAGE_OPTION_NONE = 0;
// No message options
private const int PS_MESSAGE_CLASS0 = 0;
// Show but do not store
private const int PS_MESSAGE_CLASS1 = 1;
// Store and show
private const int PSRO_NONE = 0;
// No replacements
private const int SMS_OPTION_DELIVERY_NONE = 0;
// No delivery options
private const int SMS_MAX_ADDRESS_LENGTH = 256;
// Max length of address
[DllImport("sms.dll")]
private static extern int SmsOpen(
string ptsMessageProtocol,
int dwMessageModes,
ref IntPtr psmshHandle,
int phMessageAvailableEvent);
[DllImport("sms.dll")]
private static extern int SmsSendMessage(
IntPtr smshHandle,
int psmsaSMSCAddress,
IntPtr psmsaDestinationAddress,
int pstValidityPeriod,
IntPtr pbData,
int dwDataSize,
IntPtr pbProviderSpecificData,
int dwProviderSpecificDataSize,
int smsdeDataEncoding,
int dwOptions,
int psmsmidMessageID);
[DllImport("sms.dll")]
private static extern int SmsClose(IntPtr smshHandle);
[DllImport("coredll.dll")]
private static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);
[DllImport("coredll.dll")]
internal static extern IntPtr LocalFree(IntPtr hMem);
private static IntPtr AllocHGlobal(IntPtr cb)
{
IntPtr hmem;
hmem = LocalAlloc(0x40, (uint) cb);
return hmem;
}
private static void FreeHGlobal(IntPtr hglobal)
{
LocalFree(hglobal);
public static int SendSMS(string Number, string Message)
{
int returnValue = 0;
IntPtr smsHandle = new IntPtr(0);
// Set address structure
byte[] smsatAddressType = BitConverter.GetBytes(SMSAT_INTERNATIONAL);
byte[] ptsAddress = Encoding.Unicode.GetBytes(Number);
byte[] smsAddressTag = new byte[smsatAddressType.Length + ptsAddress.Length];
Array.Copy(smsatAddressType, 0, smsAddressTag, 0, smsatAddressType.Length);
Array.Copy(ptsAddress, 0, smsAddressTag, smsatAddressType.Length, ptsAddress.Length);
IntPtr smsAddress = AllocHGlobal((IntPtr)smsAddressTag.Length);
Marshal.Copy(smsAddressTag, 0, smsAddress, smsAddressTag.Length);
// Set provider data structure
byte[] dwMessageOptions = BitConverter.GetBytes(PS_MESSAGE_OPTION_NONE);
byte[] psMessageClass = BitConverter.GetBytes(PS_MESSAGE_CLASS1);
byte[] psReplaceOption = BitConverter.GetBytes(PSRO_NONE);
byte[] smsProviderDataTag = new byte[dwMessageOptions.Length +
psMessageClass.Length + psReplaceOption.Length];
Array.Copy(dwMessageOptions, 0, smsProviderDataTag, 0, dwMessageOptions.Length);
Array.Copy(psMessageClass, 0, smsProviderDataTag, dwMessageOptions.Length,
psMessageClass.Length);
Array.Copy(psReplaceOption, 0, smsProviderDataTag, dwMessageOptions.Length +
psMessageClass.Length, psReplaceOption.Length);
IntPtr smsProviderData = AllocHGlobal((IntPtr) smsProviderDataTag.Length);
Marshal.Copy(smsProviderDataTag, 0, smsProviderData, smsProviderDataTag.Length);
// Set message
// Encoding unicode = Encoding.Unicode;
// Encoding utf8 = Encoding.ASCII;
// byte[] smsMessageTag1 = Encoding.ASCII.GetBytes(Message);
// byte[] smsMessageTag = Encoding.Convert(utf8,unicode,smsMessageTag1);
byte[] smsMessageTag = Encoding.Unicode .GetBytes(Message);
IntPtr smsMessage = AllocHGlobal((IntPtr)smsMessageTag.Length);
Marshal.Copy(smsMessageTag, 0, smsMessage, smsMessageTag.Length);
// Get handle
if (0 != SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_SEND, ref smsHandle, 0))
returnValue = -1; // Could not open
)
// Send message
if (0 != SmsSendMessage(smsHandle, 0, smsAddress, 0,
smsMessage, smsMessageTag.Length,
smsProviderData, smsProviderDataTag.Length,
SMSDE_GSM, SMS_OPTION_DELIVERY_NONE, 0))
returnValue = -2;
FreeHGlobal(smsMessage);
FreeHGlobal(smsProviderData);
FreeHGlobal(smsAddress);
// Release handle
if (0 != SmsClose(smsHandle))
returnValue = -3;
// Could not close
return returnValue;
}
}
}