RAS.Dial + WebRequest -> Error?

T

Trollpower

Dear NG,

i need to make a RAS.Dial in my App. But due to some circumstances i
also need to make a WebRequest to a page. If i call Ras.Dial and then
make the WebRequest, the System tries to make another connection on
its own and gives me an Error, saying that it cant establish a
connection.

If i put the Timeout to 5 Seconds the WebRequest also throws a
timeout-exception.

I wrote a small app which reproduces this issue and which i put at the
end of this post. Please remember to fill in your custom connection,
user and passwort for the RAS.Dial.

If anyone knows the problem and can tell me how to solve it, i will
appreciate it.

Greetz

Jens

Code:

using System;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.Collections;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace WeichenTest
{
/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Button button4;
int con = 0;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code

private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.Text = "RAS.Dial";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(80, 64);
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.Text = "Make Request";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(80, 112);
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.Text = "RAS.Hangup";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(80, 160);
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.Text = "EXIT";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string connection = "T-Mobile GPRS2";
string user = "t-d1";
string pw = "t-d1";
Cursor.Current = Cursors.WaitCursor;
RAS.Dial(connection, user,pw, ref con);
Cursor.Current = Cursors.Default;
}

private void button2_Click(object sender, System.EventArgs e)
{
HttpWebRequest hwrRequest;
HttpWebResponse hwrResponse;
string strUrl = @"http://www.microsoft.com/";
bool bConnected = false;
try
{
hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl);
hwrRequest.Timeout = 5000;
hwrResponse = (HttpWebResponse)hwrRequest.GetResponse();

if(hwrResponse.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("WebAccess() -> true");
bConnected = true;
}
}
catch(WebException we)
{
MessageBox.Show("WebAccess Error: "+we.ToString());
bConnected = false;
}
catch(Exception ex)
{
MessageBox.Show("WebAccess Error: "+ex.ToString());
bConnected = false;
}
finally
{
hwrRequest = null;
hwrResponse = null;
}
MessageBox.Show("WebAccess -> "+bConnected.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
RAS.HangUp(con);
}

private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}

public class RAS
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasDial (int dialExtensions,
int phoneBookPath,
IntPtr rasDialParam,
int NotifierType,
int notifier,
ref int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasHangUp (int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasGetConnectStatus (int hRasConn,
ref Object lpRasConnStatus);


private struct rasDialParams
{
public int dwSize;
}

public struct VBRasDialParams
{
public string EntryName;
public string PhoneNumber;
public string CallbackNumber;
public string UserName;
public string Password;
public string Domain;
public long SubEntryIndex;
public long RasDialFunc2CallbackId;
}


public static bool Dial(string szRasConnection, string szUsername,
string szPassword, ref int hRasConnection)
{
rasDialParams p = new rasDialParams();
int dwSize;
IntPtr strPointer;
char[] bRasConnection = new char[szRasConnection.Length];
//char[szRasConnection.Length] bRasConnection;
IntPtr offset;
int hrasconn = 0;

char[] bUsername = new char[szUsername.Length];
char[] bPassword = new char[szPassword.Length];
int ret;

dwSize = Marshal.SizeOf(p);
dwSize += Marshal.SystemDefaultCharSize * 21;
dwSize += Marshal.SystemDefaultCharSize * 129;
dwSize += Marshal.SystemDefaultCharSize * 49;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 16;

bRasConnection = szRasConnection.ToCharArray();
bUsername = szUsername.ToCharArray();
bPassword = szPassword.ToCharArray();
strPointer = GPRSConnect.AllocHLocal(dwSize);
offset = new IntPtr(strPointer.ToInt32() + Marshal.SizeOf(p));
Marshal.Copy(bRasConnection, 0, offset, szRasConnection.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 21);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 129);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 49);
Marshal.Copy(bUsername, 0, offset, szUsername.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy(bPassword, 0, offset, szPassword.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);

try
{
ret = RasDial(0, 0, strPointer, 0, 0, ref hrasconn);

if (ret == 0)
{
hRasConnection = hrasconn;
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}

public static int GetConnectStatus(int hrasconn,
ref object lprasconnstatus)
{
return RasGetConnectStatus(hrasconn, ref lprasconnstatus);
}


public static bool HangUp(int hrasconn)
{
int ret;
try
{
ret = RasHangUp(hrasconn);
if (ret == 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
}

public class GPRSConnect
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalAlloc (int uFlags,
int uBytes);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalFree (IntPtr hMem);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalReAlloc (int hMem,
int uBytes, int fuFlags);

private const int LMEM_FIXED = 0;
private const int LMEM_MOVEABLE = 2;
private const int LMEM_ZEROINIT = 64;
private const int LPTR = LMEM_ZEROINIT; //(LMEM_FIXED ||
LMEM_ZEROINIT);


// Allocates a block of memory using LocalAlloc
public static IntPtr AllocHLocal(int cb)
{
return LocalAlloc(LPTR, cb);
}

// Frees memory allocated by AllocHLocal

public static void FreeHLocal(IntPtr hlocal)
{
if (!hlocal.Equals(IntPtr.Zero))
{
if (!IntPtr.Zero.Equals(LocalFree(hlocal)))
{

throw new Exception("win32 error");

}

hlocal = IntPtr.Zero;

}

}

// Resizes a block of memory previously allocated with AllocHLocal
public static IntPtr ReAllocHLocal(int pv, int cb)
{

IntPtr newMem = LocalReAlloc(pv, cb, LMEM_MOVEABLE);

if (newMem.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}

return newMem;
}

// Copies the contents of a managed string to unmanaged memory

public static IntPtr StringToHLocalUni(string s)
{
if (s == null)
{
return IntPtr.Zero;

}
else
{

int nc = s.Length;
int len = 2 * (1 + nc);

IntPtr hLocal = AllocHLocal(len);

if (hLocal.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}
else
{
System.Runtime.InteropServices.Marshal.Copy(s.ToCharArray(), 0,
hLocal, s.Length);
return hLocal;
}

}

}

}
}
 
D

Darren Beckley

I have not used WebRequest myself, but perhaps it is using the Connection
Manager to create an dial-up connection? Unfortunately, Connection Manager
will not re-use an existing dial-up connection made by RasDial(), even if it
is the same dial-up connection that it would use itself! Try using
ConnMgrEstablishConnection instead of RasDial. There is a managed code
interface to Connection Manager in the OpenNETCF classes
(www.opennetcf.org).

Hope that helps,
Darren


Trollpower said:
Dear NG,

i need to make a RAS.Dial in my App. But due to some circumstances i
also need to make a WebRequest to a page. If i call Ras.Dial and then
make the WebRequest, the System tries to make another connection on
its own and gives me an Error, saying that it cant establish a
connection.

If i put the Timeout to 5 Seconds the WebRequest also throws a
timeout-exception.

I wrote a small app which reproduces this issue and which i put at the
end of this post. Please remember to fill in your custom connection,
user and passwort for the RAS.Dial.

If anyone knows the problem and can tell me how to solve it, i will
appreciate it.

Greetz

Jens

Code:

using System;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.Collections;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace WeichenTest
{
/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Button button4;
int con = 0;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code

private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.Text = "RAS.Dial";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(80, 64);
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.Text = "Make Request";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(80, 112);
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.Text = "RAS.Hangup";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(80, 160);
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.Text = "EXIT";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string connection = "T-Mobile GPRS2";
string user = "t-d1";
string pw = "t-d1";
Cursor.Current = Cursors.WaitCursor;
RAS.Dial(connection, user,pw, ref con);
Cursor.Current = Cursors.Default;
}

private void button2_Click(object sender, System.EventArgs e)
{
HttpWebRequest hwrRequest;
HttpWebResponse hwrResponse;
string strUrl = @"http://www.microsoft.com/";
bool bConnected = false;
try
{
hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl);
hwrRequest.Timeout = 5000;
hwrResponse = (HttpWebResponse)hwrRequest.GetResponse();

if(hwrResponse.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("WebAccess() -> true");
bConnected = true;
}
}
catch(WebException we)
{
MessageBox.Show("WebAccess Error: "+we.ToString());
bConnected = false;
}
catch(Exception ex)
{
MessageBox.Show("WebAccess Error: "+ex.ToString());
bConnected = false;
}
finally
{
hwrRequest = null;
hwrResponse = null;
}
MessageBox.Show("WebAccess -> "+bConnected.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
RAS.HangUp(con);
}

private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}

public class RAS
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasDial (int dialExtensions,
int phoneBookPath,
IntPtr rasDialParam,
int NotifierType,
int notifier,
ref int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasHangUp (int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasGetConnectStatus (int hRasConn,
ref Object lpRasConnStatus);


private struct rasDialParams
{
public int dwSize;
}

public struct VBRasDialParams
{
public string EntryName;
public string PhoneNumber;
public string CallbackNumber;
public string UserName;
public string Password;
public string Domain;
public long SubEntryIndex;
public long RasDialFunc2CallbackId;
}


public static bool Dial(string szRasConnection, string szUsername,
string szPassword, ref int hRasConnection)
{
rasDialParams p = new rasDialParams();
int dwSize;
IntPtr strPointer;
char[] bRasConnection = new char[szRasConnection.Length];
//char[szRasConnection.Length] bRasConnection;
IntPtr offset;
int hrasconn = 0;

char[] bUsername = new char[szUsername.Length];
char[] bPassword = new char[szPassword.Length];
int ret;

dwSize = Marshal.SizeOf(p);
dwSize += Marshal.SystemDefaultCharSize * 21;
dwSize += Marshal.SystemDefaultCharSize * 129;
dwSize += Marshal.SystemDefaultCharSize * 49;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 16;

bRasConnection = szRasConnection.ToCharArray();
bUsername = szUsername.ToCharArray();
bPassword = szPassword.ToCharArray();
strPointer = GPRSConnect.AllocHLocal(dwSize);
offset = new IntPtr(strPointer.ToInt32() + Marshal.SizeOf(p));
Marshal.Copy(bRasConnection, 0, offset, szRasConnection.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 21);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 129);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 49);
Marshal.Copy(bUsername, 0, offset, szUsername.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy(bPassword, 0, offset, szPassword.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);

try
{
ret = RasDial(0, 0, strPointer, 0, 0, ref hrasconn);

if (ret == 0)
{
hRasConnection = hrasconn;
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}

public static int GetConnectStatus(int hrasconn,
ref object lprasconnstatus)
{
return RasGetConnectStatus(hrasconn, ref lprasconnstatus);
}


public static bool HangUp(int hrasconn)
{
int ret;
try
{
ret = RasHangUp(hrasconn);
if (ret == 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
}

public class GPRSConnect
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalAlloc (int uFlags,
int uBytes);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalFree (IntPtr hMem);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalReAlloc (int hMem,
int uBytes, int fuFlags);

private const int LMEM_FIXED = 0;
private const int LMEM_MOVEABLE = 2;
private const int LMEM_ZEROINIT = 64;
private const int LPTR = LMEM_ZEROINIT; //(LMEM_FIXED ||
LMEM_ZEROINIT);


// Allocates a block of memory using LocalAlloc
public static IntPtr AllocHLocal(int cb)
{
return LocalAlloc(LPTR, cb);
}

// Frees memory allocated by AllocHLocal

public static void FreeHLocal(IntPtr hlocal)
{
if (!hlocal.Equals(IntPtr.Zero))
{
if (!IntPtr.Zero.Equals(LocalFree(hlocal)))
{

throw new Exception("win32 error");

}

hlocal = IntPtr.Zero;

}

}

// Resizes a block of memory previously allocated with AllocHLocal
public static IntPtr ReAllocHLocal(int pv, int cb)
{

IntPtr newMem = LocalReAlloc(pv, cb, LMEM_MOVEABLE);

if (newMem.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}

return newMem;
}

// Copies the contents of a managed string to unmanaged memory

public static IntPtr StringToHLocalUni(string s)
{
if (s == null)
{
return IntPtr.Zero;

}
else
{

int nc = s.Length;
int len = 2 * (1 + nc);

IntPtr hLocal = AllocHLocal(len);

if (hLocal.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}
else
{
System.Runtime.InteropServices.Marshal.Copy(s.ToCharArray(), 0,
hLocal, s.Length);
return hLocal;
}

}

}

}
}
 
A

Alex Feinman [MVP]

This is correct AFAIK. The WebRequest class internally uses Connection
Manager to initiate connection. ConnMgr will not reuse the existing RAS
connection unless ithas this connection associated with it. I suspect that
if your RAS connectoid is listed among Connects to Internet in network
connections, COnnMgr will be happy to use it if it is already active. To
register a RAS connectoid with COnnMgr you will need to either edit
registry, or use DMProcessConfigXml function

--
Alex Feinman
---
Visit http://www.opennetcf.org
Darren Beckley said:
I have not used WebRequest myself, but perhaps it is using the Connection
Manager to create an dial-up connection? Unfortunately, Connection Manager
will not re-use an existing dial-up connection made by RasDial(), even if it
is the same dial-up connection that it would use itself! Try using
ConnMgrEstablishConnection instead of RasDial. There is a managed code
interface to Connection Manager in the OpenNETCF classes
(www.opennetcf.org).

Hope that helps,
Darren


Trollpower said:
Dear NG,

i need to make a RAS.Dial in my App. But due to some circumstances i
also need to make a WebRequest to a page. If i call Ras.Dial and then
make the WebRequest, the System tries to make another connection on
its own and gives me an Error, saying that it cant establish a
connection.

If i put the Timeout to 5 Seconds the WebRequest also throws a
timeout-exception.

I wrote a small app which reproduces this issue and which i put at the
end of this post. Please remember to fill in your custom connection,
user and passwort for the RAS.Dial.

If anyone knows the problem and can tell me how to solve it, i will
appreciate it.

Greetz

Jens

Code:

using System;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.Collections;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace WeichenTest
{
/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Button button4;
int con = 0;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code

private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.Text = "RAS.Dial";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(80, 64);
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.Text = "Make Request";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(80, 112);
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.Text = "RAS.Hangup";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(80, 160);
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.Text = "EXIT";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string connection = "T-Mobile GPRS2";
string user = "t-d1";
string pw = "t-d1";
Cursor.Current = Cursors.WaitCursor;
RAS.Dial(connection, user,pw, ref con);
Cursor.Current = Cursors.Default;
}

private void button2_Click(object sender, System.EventArgs e)
{
HttpWebRequest hwrRequest;
HttpWebResponse hwrResponse;
string strUrl = @"http://www.microsoft.com/";
bool bConnected = false;
try
{
hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl);
hwrRequest.Timeout = 5000;
hwrResponse = (HttpWebResponse)hwrRequest.GetResponse();

if(hwrResponse.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("WebAccess() -> true");
bConnected = true;
}
}
catch(WebException we)
{
MessageBox.Show("WebAccess Error: "+we.ToString());
bConnected = false;
}
catch(Exception ex)
{
MessageBox.Show("WebAccess Error: "+ex.ToString());
bConnected = false;
}
finally
{
hwrRequest = null;
hwrResponse = null;
}
MessageBox.Show("WebAccess -> "+bConnected.ToString());
}

private void button3_Click(object sender, System.EventArgs e)
{
RAS.HangUp(con);
}

private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}

public class RAS
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasDial (int dialExtensions,
int phoneBookPath,
IntPtr rasDialParam,
int NotifierType,
int notifier,
ref int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasHangUp (int hRasConn);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern int RasGetConnectStatus (int hRasConn,
ref Object lpRasConnStatus);


private struct rasDialParams
{
public int dwSize;
}

public struct VBRasDialParams
{
public string EntryName;
public string PhoneNumber;
public string CallbackNumber;
public string UserName;
public string Password;
public string Domain;
public long SubEntryIndex;
public long RasDialFunc2CallbackId;
}


public static bool Dial(string szRasConnection, string szUsername,
string szPassword, ref int hRasConnection)
{
rasDialParams p = new rasDialParams();
int dwSize;
IntPtr strPointer;
char[] bRasConnection = new char[szRasConnection.Length];
//char[szRasConnection.Length] bRasConnection;
IntPtr offset;
int hrasconn = 0;

char[] bUsername = new char[szUsername.Length];
char[] bPassword = new char[szPassword.Length];
int ret;

dwSize = Marshal.SizeOf(p);
dwSize += Marshal.SystemDefaultCharSize * 21;
dwSize += Marshal.SystemDefaultCharSize * 129;
dwSize += Marshal.SystemDefaultCharSize * 49;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 257;
dwSize += Marshal.SystemDefaultCharSize * 16;

bRasConnection = szRasConnection.ToCharArray();
bUsername = szUsername.ToCharArray();
bPassword = szPassword.ToCharArray();
strPointer = GPRSConnect.AllocHLocal(dwSize);
offset = new IntPtr(strPointer.ToInt32() + Marshal.SizeOf(p));
Marshal.Copy(bRasConnection, 0, offset, szRasConnection.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 21);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 129);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 49);
Marshal.Copy(bUsername, 0, offset, szUsername.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy(bPassword, 0, offset, szPassword.Length);
offset = new IntPtr(offset.ToInt32() +
Marshal.SystemDefaultCharSize * 257);
Marshal.Copy("".ToCharArray(), 0, offset, "".Length);

try
{
ret = RasDial(0, 0, strPointer, 0, 0, ref hrasconn);

if (ret == 0)
{
hRasConnection = hrasconn;
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}

public static int GetConnectStatus(int hrasconn,
ref object lprasconnstatus)
{
return RasGetConnectStatus(hrasconn, ref lprasconnstatus);
}


public static bool HangUp(int hrasconn)
{
int ret;
try
{
ret = RasHangUp(hrasconn);
if (ret == 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
}

public class GPRSConnect
{

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalAlloc (int uFlags,
int uBytes);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalFree (IntPtr hMem);

[DllImport("coredll.dll", CharSet=CharSet.Unicode)]
private static extern IntPtr LocalReAlloc (int hMem,
int uBytes, int fuFlags);

private const int LMEM_FIXED = 0;
private const int LMEM_MOVEABLE = 2;
private const int LMEM_ZEROINIT = 64;
private const int LPTR = LMEM_ZEROINIT; //(LMEM_FIXED ||
LMEM_ZEROINIT);


// Allocates a block of memory using LocalAlloc
public static IntPtr AllocHLocal(int cb)
{
return LocalAlloc(LPTR, cb);
}

// Frees memory allocated by AllocHLocal

public static void FreeHLocal(IntPtr hlocal)
{
if (!hlocal.Equals(IntPtr.Zero))
{
if (!IntPtr.Zero.Equals(LocalFree(hlocal)))
{

throw new Exception("win32 error");

}

hlocal = IntPtr.Zero;

}

}

// Resizes a block of memory previously allocated with AllocHLocal
public static IntPtr ReAllocHLocal(int pv, int cb)
{

IntPtr newMem = LocalReAlloc(pv, cb, LMEM_MOVEABLE);

if (newMem.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}

return newMem;
}

// Copies the contents of a managed string to unmanaged memory

public static IntPtr StringToHLocalUni(string s)
{
if (s == null)
{
return IntPtr.Zero;

}
else
{

int nc = s.Length;
int len = 2 * (1 + nc);

IntPtr hLocal = AllocHLocal(len);

if (hLocal.Equals(IntPtr.Zero))
{
throw new Exception("out of memory");
}
else
{
System.Runtime.InteropServices.Marshal.Copy(s.ToCharArray(), 0,
hLocal, s.Length);
return hLocal;
}

}

}

}
}
 

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