.NET Compact Framework applications target devices like Pocket PCs, Smart Phones, PDAs. It is based on Windows CE. The core set of functions for smart-device systems and applications are available in COREDLL.DLL. It also has the following dlls:
AYGShell.dll – Pocket PC shell functions
CommCtrl.dll – Common control lib
WinSock.dll – Windows Sockets
Phone.dll – High level phone control
SMS.dll – SMS messaging API
Using the P/Invoke support available in .NET CF for calling Win32 API functions in unmanaged dlls, we can send SMS from a C# or VB.NET CF application.
SmsOpen function opens the SMS messaging component.
HRESULT SmsOpen (
const LPCTSTR ptsMessageProtocol,
const DWORD dwMessageModes,
SMS_HANDLE* const psmshHandle,
HANDLE* const phMessageAvailableEvent);
ptsMessageProtocol is a string denoting that SMS protocol to use. dwMessageModes specifies whether we want to be in send or receive mode.
psmshHandle is a pointer to the handle of the SMS session and is valid only if the function returns properly. phMessageAvailableEvent is the handle to a Win32 event handle that can be used to determine when the next message is available to be read.
Using the DllImport attribute in the System.Runtime.InteropServices namespace, we can declare the functions in our code.
[DllImport("sms.dll")]
private static extern IntPtr SmsOpen(String ptsMessageProtocol,
IntPtr dwMessageModes, ref IntPtr psmshHandle, IntPtr
phMessageAvailableEvent);
SmsSendMessage function sends the message to a mobile number.
HRESULT SmsSendMessage (
const SMS_HANDLE smshHandle,
const SMS_ADDRESS * const psmsaSMSCAddress,
const SMS_ADDRESS * const psmsaDestinationAddress,
const SYSTEMTIME * const pstValidityPeriod,
const BYTE * const pbData,
const DWORD dwDataSize,
const BYTE * const pbProviderSpecificData,
const DWORD dwProviderSpecificDataSize,
const SMS_DATA_ENCODING smsdeDataEncoding,
const DWORD dwOptions,
SMS_MESSAGE_ID * psmsmidMessageID);
smshHandle is the handle returned in psmshHandle by SmsOpen. psmsaSMSCAddress is an optional parameter specifying which SMS Message Center is to be used. If NULL is used, the user's default SMSMC will be used.
psmsaDestinationAddress is where the message is to be delivered. pstValidityPeriod breaks from the standard SYSTEMTIME structure in that it is the amount of time past sending of an SMS during which the message still is considered valid.
pbData is the byte representation of the message's data portion. This can be NULL.
dwDataSize is the size in bytes of the message's data portion. pbProviderSpecificData is additional information required by some providers to allow an SMS to transmit correctly. dwProviderSpecificDataSize is the size in bytes of the previously mentioned field.
smsdeDataEncoding is an option found within the SMS_DATA_ENCODING enumeration detailed above. dwOptions are (currently) two flags that will fail an SMS after one attempt or will allow it to be redelivered until the router gives up. psmsmidMessageID will be non-null if this function returns successfully.
In our managed code, we use it as
[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);
SmsClose function closes an SMS message service request.
HRESULT SmsClose (
const SMS_HANDLE oCommandBarPopup);
In our code we declare it as
[DllImport("sms.dll")]
private static extern IntPtr SmsClose(IntPtr smshHandle);