SMS with VB .net

Carty

Active member
Joined
Jan 9, 2006
Messages
29
Programming Experience
1-3
Hey all,

Im a beginner in Pocket PC program and went thru this forum fully but din get a complete guide to send sms & read recieved sms..

For example, i have a form with a text box and a button, on pressing the button i wanna message the textbox contents to a number previously specified in a program.. Pls help me with the code...

Tanx a lottt in advance..
 
MSDN articles and source codes:

Sending SMSs from your Microsoft .NET Compact Framework-based Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfsendsms.asp

Receiving SMS Messages Inside a Managed Application
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/ReceivingSMSMessages.asp

Developing SMS Enabled Line-of-Business Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnppcgen/html/sms_apps2.asp


There is another thread about the issue here at this forum:
http://www.vbdotnetforums.com/showthread.php?t=4977
 
Last edited:
JohnH said:
MSDN articles and source codes:

Sending SMSs from your Microsoft .NET Compact Framework-based Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfsendsms.asp

Receiving SMS Messages Inside a Managed Application
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/ReceivingSMSMessages.asp

Developing SMS Enabled Line-of-Business Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnppcgen/html/sms_apps2.asp


There is another thread about the issue here at this forum:
http://www.vbdotnetforums.com/showthread.php?t=4977

Hey tanx a lott,
Yr links showed me all i expected.. Il go home check tose samples,try buildin my own..
Now im in college' cafe..
Tanx again...
 
Need more!

Hey tose links were usefull.. Tanx a lot.. The first one gave me wot i asked.. Allows me to send a message.. i downloaded the sample and they've used the following class

Imports System.Runtime.InteropServices
Imports interopserv = System.Runtime.InteropServices
Public Enum SMS_ADDRESS_TYPE
SMSAT_UNKNOWN = 0
SMSAT_INTERNATIONAL
SMSAT_NATIONAL
SMSAT_NETWORKSPECIFIC
SMSAT_SUBSCRIBER
SMSAT_ALPHANUMERIC
SMSAT_ABBREVIATED
End Enum 'SMS_ADDRESS_TYPE
Public Structure PhoneAddress
'/ <summary>The address type.</summary>
Public AddressType As SMS_ADDRESS_TYPE
'/ <summary>The phone number in string format.</summary>
Public Address() As Char
End
Structure 'PhoneAddress
Public Class SMS
Private Shared SMS_MSGTYPE_TEXT As String = "Microsoft Text SMS Protocol"
Private Shared SMS_MODE_SEND As Long = &H2
Private Shared SMS_OPTION_DELIVERY_NONE As Long = &H0
Private Shared SMS_OPTION_DELIVERY_NO_RETRY As Long = &H1
Private Shared PS_MESSAGE_OPTION_NONE As Long = &H0
Private Enum SMS_DATA_ENCODING
SMSDE_OPTIMAL = 0
SMSDE_GSM
SMSDE_UCS2
End Enum 'SMS_DATA_ENCODING
Public Enum PROVIDER_SPECIFIC_MESSAGE_CLASS
PS_MESSAGE_CLASS0 = 0
PS_MESSAGE_CLASS1
PS_MESSAGE_CLASS2
PS_MESSAGE_CLASS3
End Enum 'PROVIDER_SPECIFIC_MESSAGE_CLASS
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
End Enum 'PROVIDER_SPECIFIC_REPLACE_OPTION
Private Structure TEXT_PROVIDER_SPECIFIC_DATA
Public dwMessageOptions As Long
Public psMessageClass As PROVIDER_SPECIFIC_MESSAGE_CLASS
Public psReplaceOption As PROVIDER_SPECIFIC_REPLACE_OPTION
End Structure 'TEXT_PROVIDER_SPECIFIC_DATA
<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsOpen(ByVal ptsMessageProtocol As [String], _
ByVal dwMessageModes As Int32, _
ByRef psmshHandle As IntPtr, _
ByVal phMessageAvailableEvent As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsSendMessage(ByVal smshHandle As IntPtr, _
ByVal psmsaSMSCAddress As Int32, _
ByVal psmsaDestinationAddress As IntPtr, _
ByVal pstValidityPeriod As Int32, _
ByVal pbData As IntPtr, _
ByVal dwDataSize As Int32, _
ByVal pbProviderSpecificData() As Byte, _
ByVal dwProviderSpecificDataSize As Int32, _
ByVal smsdeDataEncoding As Int32, _
ByVal dwOptions As Int32, _
ByVal psmsmidMessageID As Int32) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsClose(ByVal smshHandle As IntPtr) As IntPtr
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure MsgSize
Public MsgSz As Int32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure ProviderDataSize
Public ProvDataSize As Int32
End Structure
Public Shared Sub SendMessage(ByVal sPhoneNumber As String, ByVal sMessage As String)
Dim retVal As IntPtr = IntPtr.Zero
Dim smsHandle As IntPtr = IntPtr.Zero
Dim smsProviderData As IntPtr = IntPtr.Zero
Dim smsMessage As IntPtr = IntPtr.Zero
Dim ProvData(12) As Byte
Try
retVal = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_SEND, smsHandle, IntPtr.Zero)
If retVal.ToInt32 <> 0 Then
Throw New Exception("Could not open SMS.")
End If
'Set address structure
Dim smsatAddressType As Byte() = BitConverter.GetBytes(SMS_ADDRESS_TYPE.SMSAT_UNKNOWN)
Dim ptsAddress As Byte() = System.Text.Encoding.Unicode.GetBytes(sPhoneNumber)
Dim smsAddressTag(smsatAddressType.Length + ptsAddress.Length) As Byte
Array.Copy(smsatAddressType, 0, smsAddressTag, 0, smsatAddressType.Length)
Array.Copy(ptsAddress, 0, smsAddressTag, smsatAddressType.Length, ptsAddress.Length)
Dim smsAddress As IntPtr = Marshal.AllocHLocal(smsAddressTag.Length)
System.Runtime.InteropServices.Marshal.Copy(smsAddressTag, 0, smsAddress, smsAddressTag.Length)
'Set message
Dim smsMessageTag As Byte() = System.Text.Encoding.Unicode.GetBytes(sMessage)
smsMessage = Marshal.AllocHLocal(smsMessageTag.Length)
System.Runtime.InteropServices.Marshal.Copy(smsMessageTag, 0, smsMessage, smsMessageTag.Length)
retVal = SmsSendMessage(smsHandle, 0, smsAddress, 0, smsMessage, smsMessageTag.Length, _
ProvData, 12, SMS_DATA_ENCODING.SMSDE_OPTIMAL, SMS_OPTION_DELIVERY_NONE, 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
retVal = SmsClose(smsHandle)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End
Class


By this method, the sms is been sent like a flash message.. In the sense, the sms is displayed in the recipients display directly and wont get saved in their inbox..! tats an advantage in one way but how do i send a normal message??

and also like, is ther any function that v specify the time the sms should be sent?? Like for example i type an sms and specify a time it has to be sent from my Pocket PC?

Yr second link gets me a code been done in VC but i don hav any idea abt VC.. i know only VB and havent even installed VC.. Can u pls convert it to VB as a new solution and send me pls, tat wld be a lot greatfull to me.. Im actually an MBBS IIIyr student.. I had tis intrest of programming from my 10th std but my career has been turned to medicine as my Dad too.. But still i luv to program apps for my personal use.. For my Pocket PC! So can u pls help me..

Tanx in advance!
 
Referencing the first article, there is a structure TEXT_PROVIDER_SPECIFIC_DATA that contains a value of enum PROVIDER_SPECIFIC_MESSAGE_CLASS, the different behaviours of this is described in that article. Basically, this is not set in the example, probably giving a default provider behaviour, and if they have not set defaults for this probably zero values, which for that mess_class is flash... (displayed immediately but not stored).

There is allocated empty byte array the length of the TEXT_PROVIDER_SPECIFIC_DATA in the Shared Sub SendMessage like this:
Dim ProvData(12) AsByte

You have to set the values in a variable of mentioned structure and put the bytes into ProvData, it is the PROVIDER_SPECIFIC_MESSAGE_CLASS enum you have to set to the message 'mode' wanted.

When structure is set with your values, you must copying it to memory like they do with Address and Message to get a bytes array. Since I haven't got the necessary equipment I can't test it. (if my work in blindness isn't doing it, can anyone provide some working code to do this?)

I added the "Set provider structure" section, see if you get this.
VB.NET:
Public Shared Sub SendMessage(.............
 
'.........
 
'Set address structure
'.........
 
'Set provider structure
Dim Tpsd As TEXT_PROVIDER_SPECIFIC_DATA 'our structure
'value is 0: Tpsd.dwMessageOptions = 0
Tpsd.psMessageClass = PROVIDER_SPECIFIC_MESSAGE_CLASS.PS_MESSAGE_CLASS1 'or 2 or 3 or 0(flash)
'value is 0: Tpsd.psReplaceOption = PROVIDER_SPECIFIC_REPLACE_OPTION.PSRO_NONE 
smsProviderData = Marshal.AllocHLocal(Marshal.SizeOf(Tpsd)) 'pointer to memory where to copy Tpsd
'byte array size is preset to 12, normally do this:  ReDim ProvData(Marshal.SizeOf(Tpsd) - 1)
'Dim ProvData(12) As Byte 'according to MSDN, enough for that structure
Marshal.StructureToPtr(Tpsd, smsProviderData, False)
Marshal.Copy(smsProviderData, ProvData, 0, Marshal.SizeOf(Tpsd))
 
'Set message
'.........
 
End Sub

About the other comments, I don't think you can do scheduling with this class, you have to do that by other means. About VC, I haven't got that either.

Edit, I think the copy should be: Marshal.Copy(smsProviderData, ProvData, 0, Marshal.SizeOf(Tpsd))
 
Last edited:
arguement out of exception

John,
tanx a lotttt for the reply


i inserted the following as u said, between set address and set message part in that class

'Set provider structure
Dim Tpsd As TEXT_PROVIDER_SPECIFIC_DATA 'our structure
'value is 0: Tpsd.dwMessageOptions = 0
Tpsd.psMessageClass = PROVIDER_SPECIFIC_MESSAGE_CLASS.PS_MESSAGE_CLASS2 'or 2 or 3 or 0(flash)
'value is 0: Tpsd.psReplaceOption = PROVIDER_SPECIFIC_REPLACE_OPTION.PSRO_NONE
smsProviderData = Marshal.AllocHLocal(System.Runtime.InteropServices.Marshal.SizeOf(Tpsd)) 'pointer to memory where to copy Tpsd
'byte array size is preset to 12, normally do this: ReDim ProvData(Marshal.SizeOf(Tpsd) - 1)
'Dim ProvData(12) As Byte 'according to MSDN, enough for that structure
System.Runtime.InteropServices.Marshal.StructureToPtr(Tpsd, smsProviderData, False)
System.Runtime.InteropServices.Marshal.Copy(smsProviderData, ProvData, 0, System.Runtime.InteropServices.Marshal.SizeOf(Tpsd)

and got the error arguement out of exception...
one more thing, yr code exactly wen inserted there wer debug errors sayin marshal doesnt have the type size of, copy and.... so i edited it to

System.Runtime.InteropServices.Marshal.
and debug errors gone... i build the cab & execute, it wont work :(

i din quite get the edit of yr message.. u edited it wit
Marshal.Copy(smsProviderData, ProvData, 0, System.Runtime.InteropServices.Marshal.SizeOf(Tpsd)
even before editing i ting it was the same or i din c the message before edit and u changed it in the original part also?? ok fine..

Also in the first reply of yrs wit the links, yr second link shows recieving sms sample but tats in C.. can u help me use it in VB pls? Tanx...
 
- I wrote SizeOf(something) first, but changed it to SizeOf(Tpsd).
- Strange with that marshal debug error since you already got Imports System.Runtime.InteropServices..
- I haven't got a C to VB translator.
- I can't get any further than written already since I don't have equipment to test it on.

Still a bit curious, I don't know what you mean by "error arguement out of exception"?

Did you try other combinations for Tpsd.psReplaceOption and Tpsd.psMessageClass ? There could very well be limits to what message modes various providers offer.
In same structure you also got Tpsd.dwMessageOptions
that "specifies some miscellaneous details spelled out in the GSM specification 3.40", I haven't checked up on this.

Keep us posted if you find out more.
 
John,

Check opennetcf.org
They give some extending cabilities to normal compact framework.. but still i couldn use their reference opennetcf.phone.sms..... thers no send message option or maybe i couldn make it out cz i havent done third party controls before..

Tanx..
 
abt marshal importing error...

in the project, thers another class called marshal.vb
so in message class wen marshal.vb is been call and sinz marshal.vb doesnt have sizeof types, it gives the error..

Im not quite sure in my view above as im a begginer..

John, did u open that sample solution?
 
Ok, you can interopserv.Marshal too, they've aliased the import of System.Runtime.InteropServices to 'interopserv'.

No, I can't open the sample, it would not open in my VS, there is no compact framework there.
 
Nope, it still says same error: "Arguementoutofexception"
But still, tanx John for trying to help me...

For some time il try to manage with class0 messaging until some1 find its out for me to post in this forum :)

But now the important ting is, i need that sms recieving sample by microsoft' msdn to be in VB.. (John's second link) Anyone....
John, c to vb converters wont convert 100% without errors, john cld u convert by yr own brain..? Pls dude, cz i need it badly.. :p
Tanx....
 
Like I told you that other thread, I'm out of luck here not being able to test out the code myself.
 
Its ok John... Tanx for trying to help me out..
Hope someone wit intense knowledge with compact framework wld help me out soon..
 
Back
Top