本人使用 TP-LINK 路由器,每次打开都会弹出窗口让输入密码,能不能简单一点呢?下面就是实现了自动登录功能的简易浏览器代码。
用 VS2008 新建一个 windows 应用程序,在默认窗体中放一个 WebBrowser 控件,执行时需带三个参数,分别是路由器IP地址,用户名,密码。
Program.cs 如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AutoLogin
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main(string[] ps)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(ps));
}
}
}
Form1.cs 如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace AutoLogin
{
public partial class Form1 : Form, IOleClientSite, IServiceProvider, IAuthenticate
{
string[] ps;
string url, uid, pwd;
public Form1(string[] ps)
{
this.ps = ps;
InitializeComponent();
if (ps.Length >= 1)
{
url = ps[0];
if (ps.Length >= 3)
{
uid = ps[1];
pwd = ps[2];
}
}
}
public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b");
public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
public const int S_OK = unchecked((int)0x00000000);
private bool haspass = true;
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("about:blank");
object obj = webBrowser1.ActiveXInstance;
IOleObject oc = obj as IOleObject;
oc.SetClientSite(this as IOleClientSite);
webBrowser1.Navigate(url);
}
#region IOleClientSite 成员
public void SaveObject()
{
}
public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk)
{
}
public void GetContainer(object ppContainer)
{
ppContainer = this;
}
public void ShowObject()
{
}
public void OnShowWindow(bool fShow)
{
}
public void RequestNewObjectLayout()
{
}
#endregion
#region IServiceProvider 成员
public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
{
int nRet = guidService.CompareTo(IID_IAuthenticate);
if (nRet == 0)
{
nRet = riid.CompareTo(IID_IAuthenticate);
if (nRet == 0)
{
if (haspass && !string.IsNullOrEmpty(pwd))
{
ppvObject = Marshal.GetComInterfaceForObject(this, typeof(IAuthenticate));
haspass = false;
return S_OK;
}
}
}
ppvObject = new IntPtr();
return INET_E_DEFAULT_ACTION;
}
#endregion
#region IAuthenticate 成员
public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername, ref IntPtr pszPassword)
{
pszUsername = Marshal.StringToCoTaskMemAuto(uid);
pszPassword = Marshal.StringToCoTaskMemAuto(pwd);
return S_OK;
}
#endregion
}
#region COM Interfaces
[ComImport,
Guid("00000112-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleObject
{
void SetClientSite(IOleClientSite pClientSite);
void GetClientSite(IOleClientSite ppClientSite);
void SetHostNames(object szContainerApp, object szContainerObj);
void Close(uint dwSaveOption);
void SetMoniker(uint dwWhichMoniker, object pmk);
void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
void InitFromData(IDataObject pDataObject, bool
fCreation, uint dwReserved);
void GetClipboardData(uint dwReserved, IDataObject ppDataObject);
void DoVerb(uint iVerb, uint lpmsg, object pActiveSite,
uint lindex, uint hwndParent, uint lprcPosRect);
void EnumVerbs(object ppEnumOleVerb);
void Update();
void IsUpToDate();
void GetUserClassID(uint pClsid);
void GetUserType(uint dwFormOfType, uint pszUserType);
void SetExtent(uint dwDrawAspect, uint psizel);
void GetExtent(uint dwDrawAspect, uint psizel);
void Advise(object pAdvSink, uint pdwConnection);
void Unadvise(uint dwConnection);
void EnumAdvise(object ppenumAdvise);
void GetMiscStatus(uint dwAspect, uint pdwStatus);
void SetColorScheme(object pLogpal);
}
[ComImport,
Guid("00000118-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleClientSite
{
void SaveObject();
void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
void GetContainer(object ppContainer);
void ShowObject();
void OnShowWindow(bool fShow);
void RequestNewObjectLayout();
}
[ComImport,
GuidAttribute("6d5140c1-7436-11ce-8034-00aa006009fa"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
ComVisible(false)]
public interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(ref Guid guidService, ref Guid riid, out IntPtr
ppvObject);
}
[ComImport, GuidAttribute("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
ComVisible(false)]
public interface IAuthenticate
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int Authenticate(ref IntPtr phwnd,
ref IntPtr pszUsername,
ref IntPtr pszPassword
);
}
#endregion
}