namespace Microshaoft
{
using System;
using RAS;
public class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.WriteLine(Environment.Version.ToString());
Console.WriteLine("Pls Input EntryName (网络连接名称):");
string EntryName = Console.ReadLine();
Console.WriteLine("Pls Input UserName (用户名):");
string UserName = Console.ReadLine();
Console.WriteLine("Pls Input Password (密码):");
string Password = ConsoleReadMaskLine('*',true);
RasManager rm = new RasManager();
rm.EntryName = EntryName; // entry name in phonebook
rm.UserName = UserName;
rm.Password = Password;
int r = -1;
r = rm.Connect();
string s = "失败";
if (r == 0)
{
s = "成功";
}
Console.WriteLine("\n网络连接: \"{0}\" 拨号上网{1},Result: {2}", EntryName, s, r);
Console.ReadLine();
}
public static string ConsoleReadMaskLine
(
char PasswordChar
, bool WithMask
)
{
string password = "";
ConsoleKey ck;
string s = @"~!@#$%&*()_+`1234567890-="; //可输入字符
s += @"QWERTYUIOP{}|qwertyuiop[]\";
s += "ASDFGHJKL:\"asdfghjkl;'";
s += "ZXCVBNM<>?zxcvbnm,./ ";
do
{
ConsoleKeyInfo cki = Console.ReadKey(true);
char c = cki.KeyChar;
ck = cki.Key;
int p = Console.CursorLeft;
if (ck == ConsoleKey.Backspace)
{
string left = "";
if (p > 0)
{
left = password.Substring(0, p - 1);
}
string right = password.Substring(p);
password = left + right;
Console.Write(c);
string output = right;
if (WithMask)
{
output = GetPasswordChars(right, PasswordChar);
}
output += "\0";
Console.Write(output);
if (p > 0)
{
p --;
}
}
else if (ck == ConsoleKey.Delete)
{
string left = "";
if (p > 0)
{
left = password.Substring(0, p);
}
string right = "";
if (p < password.Length)
{
right = password.Substring(p + 1);
}
password = left + right;
//Console.Write(right + " ");
string output = right;
if (WithMask)
{
output = GetPasswordChars(right, PasswordChar);
}
output += "\0";
Console.Write(output);
}
else
{
if (s.IndexOf(c) >= 0)
{
string left = password.Substring(0, p);
string right = password.Substring(p);
password = left + c + right;
string output = c + right;
if (WithMask)
{
output = GetPasswordChars(c + right, PasswordChar);
}
Console.Write(output);
p ++;
}
else
{
switch (ck)
{
case ConsoleKey.LeftArrow :
if (p > 0)
{
p --;
}
break;
case ConsoleKey.RightArrow :
if (p < password.Length)
{
p ++;
}
break;
case ConsoleKey.Home :
p = 0;
break;
case ConsoleKey.End :
p = password.Length;
break;
default :
Console.Beep();
break;
}
}
}
Console.CursorLeft = p;
} while (ck != ConsoleKey.Enter);
return password;
}
private static string GetPasswordChars(string s, char c)
{
string passwordChars = "";
for (int i = 0; i < s.Length; i++)
{
passwordChars += c;
}
return passwordChars;
}
}
}
namespace RAS
{
using System;
using System.Runtime.InteropServices;
public class RasManager
{
public const int RAS_MaxEntryName = 256;
public const int RAS_MaxPhoneNumber = 128;
public const int UNLEN = 256;
public const int PWLEN = 256;
public const int DNLEN = 15;
public const int MAX_PATH = 260;
public const int RAS_MaxDeviceType = 16;
public const int RAS_MaxCallbackNumber = RAS_MaxPhoneNumber;
public delegate void Callback(uint unMsg, int rasconnstate, int dwError);
[StructLayout(LayoutKind.Sequential, Pack = 4,CharSet = CharSet.Auto)]
public struct RASDIALPARAMS
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
public string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= RAS_MaxCallbackNumber + 1)]
public string szCallbackNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= UNLEN + 1)]
public string szUserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= PWLEN + 1)]
public string szPassword;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= DNLEN + 1)]
public string szDomain;
public int dwSubEntry;
public int dwCallbackId;
}
[DllImport("rasapi32.dll", CharSet=CharSet.Auto)]
public static extern int RasDial
(
int lpRasDialExtensions
, string lpszPhonebook
, ref RASDIALPARAMS lprasdialparams
, int dwNotifierType
, Callback lpvNotifier
, ref int lphRasConn
);
private RASDIALPARAMS RasDialParams;
private int Connection;
public RasManager()
{
Connection = 0;
RasDialParams = new RASDIALPARAMS();
RasDialParams.dwSize = Marshal.SizeOf(RasDialParams);
}
#region Properties
public string UserName
{
get
{
return RasDialParams.szUserName;
}
set
{
RasDialParams.szUserName = value;
}
}
public string Password
{
get
{
return RasDialParams.szPassword;
}
set
{
RasDialParams.szPassword = value;
}
}
public string EntryName
{
get
{
return RasDialParams.szEntryName;
}
set
{
RasDialParams.szEntryName = value;
}
}
#endregion
public int Connect()
{
Callback rasDialFunc = new Callback(RasManager.RasDialFunc);
RasDialParams.szEntryName += "\0";
RasDialParams.szUserName += "\0";
RasDialParams.szPassword += "\0";
int result = RasDial (0, null, ref RasDialParams, 0, rasDialFunc, ref Connection);
return result;
}
public static void RasDialFunc
(
uint unMsg
, int rasconnstate
, int dwError
)
{
Console.WriteLine
(
"CallBack: unMsg={0},rasconnstate={1},dwError={2}"
, unMsg
, rasconnstate
, dwError
);
}
}
}