예제 #1
0
        /// <summary>
        /// Creates a new RasEntry (phone-book entry).
        /// </summary>
        /// <param name="newEntry">RasEntry that contains all phone-book entry information.</param>
        /// <param name="dialParams">RasDialParams that contains dial parameters.</param>
        public static RasEntry CreateEntry(RasEntry newEntry, RasDialParams dialParams)
        {
            Native.Error ret = Native.Error.Success;
            int cbEntry = 0, cbDevCfg = 0;

            if (newEntry.DeviceType == "" || newEntry.DeviceName == "")
            {
                throw new Exception("DeviceName and DeviceType must be populated in the RasEntry.");
            }
            //validate entry first
            ret = Native.RasValidateEntryName(null, newEntry.Name);
            if (ret != Native.Error.AlreadyExists)
            {
                if (ret != 0)
                {
                    throw new Exception("Invalid EntryName format.");

                }
            }

            //get the size for a RASENTRY
            ret = Native.RasGetEntryProperties(null, string.Empty, null, ref cbEntry, null, ref cbDevCfg);
            byte[] buffer = new byte[cbEntry];

            // get a default set of properties
            ret = Native.RasGetEntryProperties(null, string.Empty, buffer, ref cbEntry, null, ref cbDevCfg);

            RasEntry entry = new RasEntry(buffer);

            entry.CountryCode = newEntry.CountryCode;
            entry.Name = newEntry.Name;
            entry.Options = newEntry.Options;
            entry.DeviceType = newEntry.DeviceType;
            entry.DeviceName = newEntry.DeviceName;
            entry.AreaCode = newEntry.AreaCode;
            entry.PhoneNumber = newEntry.PhoneNumber;
            entry.IPAddress = newEntry.IPAddress;
            entry.IPAddressDns = newEntry.IPAddressDns;
            entry.IPAddressDnsAlt = newEntry.IPAddressDnsAlt;
            entry.IPAddressWins = newEntry.IPAddressWins;
            entry.IPAddressWinsAlt = newEntry.IPAddressWinsAlt;

            ret = Native.RasSetEntryProperties(null, entry.Name, entry.Data, cbEntry, null, 0);
            if (ret == Native.Error.Success)
            {
                if (dialParams != null)
                {
                    entry.SetDialParams(dialParams);
                }
                return newEntry;
            }
            else
            {
                return null;
            }
        }