c#自动ADSL拨号代码
鉴于网上搜索的c#拨号代码总是会弹出拨号连接的界面,基本等于无用,现在给出一段代码,经过本人的调试,完全可以实现自动ADSL拨号,昨夜测试完成后,不敢独享。具体用途,各位自己决定吧
public uint Connect(string EntryName, string UserName, string Password, out IntPtr RasConn) { uint retVal; 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++; } retVal = RAS.RasDial(IntPtr.Zero, IntPtr.Zero, (IntPtr)pAddr, 0, IntPtr.Zero, ref RasConn); } return retVal; }
代码中使用了指针,因此,需要在编译的时候添加/unsafe编译选项,RasDial可以在rasapi32.dll中找到,需要做一下封送和接口定义
[DllImport("rasapi32.dll", CharSet = CharSet.Auto)] public extern static uint RasDial( IntPtr dialExtensions, IntPtr phoneBookPath, IntPtr rasDialParam, uint NotifierType, IntPtr notifier, ref IntPtr pRasConn );
兄弟,要是断开连接呢?
另外当前拨号状态能取得吗?
比如已连接,处于断开等等
详情参照“rasapi32.dll”中的函数