Establishing GPRS connection in C# (RAS,RasDial)

S

s_alexander04

This is a code I use to establish GPRS connection programmatically in
C# using
RAS. Maybe it'll be useful for somebody. I use it on Windows Mobile
2003 PocketPC.


"Allow unsafe Code Blocks" option must be set to TRUE in project
properties


unsafe public static uint myRasDial(string EntryName,string
UserName,string Password,out IntPtr RasConn) {
/*
EntryName ,UserName,Password - same as in RASDIALPARAMS structure (see

MSDN)
*/
uint r=0;
RasConn=IntPtr.Zero;
byte[] bRASDIALPARAMS =new byte[1464];
fixed (byte* pAddr = bRASDIALPARAMS) {


byte* pCurrent = pAddr;
Marshal.WriteInt32((IntPtr)pCurrent,bRASDIALPARAMS.Length);
pCurrent+=4;
foreach (byte b in Encoding.Unicode.GetBytes(EntryName)) {
Marshal.WriteByte((IntPtr)pCurrent, b);
pCurrent++;
}
pCurrent=pAddr+0x192;//0x192 - offset for RASDIALPARAMS.UserName
foreach (byte b in Encoding.Unicode.GetBytes(UserName)) {
Marshal.WriteByte((IntPtr)pCurrent, b);
pCurrent++;
}
pCurrent=pAddr+0x394;//0x394 - offset for RASDIALPARAMS.Password
foreach (byte b in Encoding.Unicode.GetBytes(Password)) {
Marshal.WriteByte((IntPtr)pCurrent, b);
pCurrent++;
}
r=RasDial(IntPtr.Zero,IntPtr.Zero,(IntPtr)pAddr,0,IntPtr.Zero,ref
RasConn);
}
return r;



}


[DllImport("coredll.dll")]
public static extern uint RasDial(IntPtr dialExtensions,IntPtr
phoneBookPath,IntPtr rasDialParam,uint NotifierType,
IntPtr notifier, ref IntPtr pRasConn);

[DllImport("coredll.dll")]
public static extern uint RasHangUp(IntPtr pRasConn);
//------------------------------------------------------------------------



Example of using myRasDial


IntPtr rc; // connection handle
if(myRasDial("BeelineGPRS","beeline","beeline",out rc)==0) { //success
try {
TcpClient client = new TcpClient("www.someaddress.com", someport);
MessageBox.Show("OK");
//..................................
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
RasHangUp(rc);
}
 
Joined
Feb 10, 2013
Messages
1
Reaction score
0
Hi

There is username and password settings in this code but how can I set dial number ( for example *99# ). thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top