Home > 2. Windows Programming, Managed Code, Windows Mobile > How to programmatically establish GPRS connection in .NET CF

How to programmatically establish GPRS connection in .NET CF

This is a simple way to establish GPRS connection in .NET CF using RAPI. Remember to check “Allow unsafe Code Blocks” option in project properties page!

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 System.Text.Encoding.Unicode.GetBytes(EntryName))
{
   Marshal.WriteByte((IntPtr)pCurrent, b);
   pCurrent++;
  }
  pCurrent = pAddr + 0x192;   //0x192 - offset for RASDIALPARAMS.UserName
  foreach (byte b in System.Text.Encoding.Unicode.GetBytes(UserName))
  {
Marshal.WriteByte((IntPtr)pCurrent, b);
   pCurrent++;
  }
  pCurrent = pAddr + 0x394; //0x394 - offset for RASDIALPARAMS.Password
  foreach (byte b in System.Text.Encoding.Unicode.GetBytes(Password))
  {
   Marshal.WriteByte((IntPtr)pCurrent, b);
   pCurrent++;
  }
  r = RasDial(IntPtr.Zero, IntPtr.Zero, (IntPtr)pAddr, 0, IntPtr.Zero, 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")] extern uint RasHangUp(IntPtr pRasConn);
//------------------------------------------------------------------------
void Connect()
{
  /* Example of using myRasDial */
 IntPtr rc;   // connection handle
 if (myRasDial("BeelineGPRS", "beeline", "beeline", out rc) == 0)
 {
  //success
  try
  {
   System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("www.someaddress.com", someport);
   MessageBox.Show("OK");
  }
  catch (Exception ex)
  {
   MessageBox.Show(ex.Message);
  }
  RasHangUp(rc);
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: