/// <summary> /// Remove entry from phone-book, then break connection established early. /// </summary> public void Disconnect() { if (this._handle == IntPtr.Zero) { return; } Ras.RASCONNSTATUS status = new Ras.RASCONNSTATUS(); // force hangup try { Ras.RasCheck(Ras.RasHangUp(this._handle)); } catch (RasException ex) { if (ex.ErrorCode != (uint)RasError.ERROR_NO_CONNECTION) { throw ex; } } while (6 != Ras.RasGetConnectStatus(this._handle, status)) { Thread.Sleep(0); } this.DeleteEntry(); this._handle = IntPtr.Zero; }
// get properties of existent phone-book entry and fill _ent structure of this class private void GetEntry() { int entrySize = 0; IntPtr buff = IntPtr.Zero; Ras.RasGetEntryProperties(null, this._params.szEntryName, buff, ref entrySize, 0, 0); buff = Marshal.AllocHGlobal(entrySize); Marshal.WriteInt32(buff, this._ent.dwSize); Ras.RasCheck(Ras.RasGetEntryProperties(null, this._params.szEntryName, buff, ref entrySize, 0, 0)); this._ent = (Ras.RASENTRY)Marshal.PtrToStructure(buff, typeof(Ras.RASENTRY)); Marshal.FreeHGlobal(buff); }
/// <summary> /// Create new entry in phone-book, then establish connection. <see cref="Disconnect()"/> method must be called in any case to balance call of this method. /// </summary> public void Connect() { if (this._handle != IntPtr.Zero) { Ras.RASCONNSTATUS status = new Ras.RASCONNSTATUS(); uint res = Ras.RasGetConnectStatus(this._handle, status); if (res == 6) // i.e. ERROR_INVALID_HANDLE { this._handle = IntPtr.Zero; } else { return; // already established } } uint validateName = Ras.RasValidateEntryName(null, this._params.szEntryName); if (validateName != (uint)RasError.SUCCESS) { throw new RasException("Entry name " + ((this._params.szEntryName == null || this._params.szEntryName.Length <= 0) ? "<Empty Name>" : this._params.szEntryName) + " not valid or already presented in phone-book."); } // 1) Create new entry this._ent.dwDialMode = 1; this._ent.dwDialExtraPercent = 75; this._ent.dwDialExtraSampleSeconds = 120; this._ent.dwHangUpExtraPercent = 10; this._ent.dwHangUpExtraSampleSeconds = 120; this.SetEntry(); // just test that entry was written successfully this.GetEntry(); // 2) Use created entry to establish connection try { Ras.RasCheck(Ras.RasDial(null, (this._phonebook != null && this._phonebook.Length > 0) ? this._phonebook : null, this._params, 0, null, ref this._handle)); } catch (Exception ex) { this.Disconnect(); throw ex; } this.OnConnected(); this.StartWatch(); Ras.RasConnectionNotification(this._handle, this._disconnEvent.Handle, Ras.RASNOTIFICATION.RASCN_Disconnection); }
// create new entry and set it's properties from _ent structure of this class private void SetEntry() { Ras.RasCheck(Ras.RasSetEntryProperties(null, this._params.szEntryName, ref this._ent, this._ent.dwSize, 0, 0)); }
// delete entry that was created before connection private void DeleteEntry() { Ras.RasCheck(Ras.RasDeleteEntry(null, this._params.szEntryName)); }