Esempio n. 1
0
        /// <summary>
        /// Updates an address in the AutoDial mapping database.
        /// </summary>
        /// <param name="address">The address to update.</param>
        /// <param name="entries">A collection of <see cref="DotRas.RasAutoDialEntry"/> objects containing the entries for the <paramref name="address"/> specified.</param>
        /// <returns><b>true</b> if the update was successful, otherwise <b>false</b>.</returns>
        public bool SetAutoDialAddress(string address, Collection<RasAutoDialEntry> entries)
        {
            bool retval = false;

            IntPtr pEntries = IntPtr.Zero;
            try
            {
                int count = 0;
                int totalSize = 0;

                if (entries != null && entries.Count > 0)
                {
                    // Reset the existing item so the new object being passed in isn't simply appended to any existing entries.
                    RasHelper.Instance.SetAutoDialAddress(address, null);

                    count = entries.Count;
                    int size = Marshal.SizeOf(typeof(NativeMethods.RASAUTODIALENTRY));

                    // Copy the entries into the struct array that will be used.
                    NativeMethods.RASAUTODIALENTRY[] autoDialEntries = new NativeMethods.RASAUTODIALENTRY[entries.Count];
                    for (int index = 0; index < autoDialEntries.Length; index++)
                    {
                        RasAutoDialEntry current = entries[index];
                        if (current != null)
                        {
                            NativeMethods.RASAUTODIALENTRY item = new NativeMethods.RASAUTODIALENTRY();
                            item.size = size;
                            item.dialingLocation = current.DialingLocation;
                            item.entryName = current.EntryName;

                            autoDialEntries[index] = item;
                        }
                    }

                    pEntries = Utilities.CopyObjectsToNewPtr<NativeMethods.RASAUTODIALENTRY>(autoDialEntries, ref size, out totalSize);
                }

                int ret = UnsafeNativeMethods.Instance.SetAutodialAddress(address, 0, pEntries, totalSize, count);
                if (ret == NativeMethods.SUCCESS)
                {
                    retval = true;
                }
                else if (ret == NativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    retval = false;
                }
                else
                {
                    ThrowHelper.ThrowRasException(ret);
                }
            }
            catch (EntryPointNotFoundException)
            {
                ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
            }
            finally
            {
                if (pEntries != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pEntries);
                }
            }
                      
            return retval;
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves information about the entries associated with a network address in the AutoDial mapping database.
        /// </summary>
        /// <param name="address">The address to retrieve.</param>
        /// <returns>A new <see cref="DotRas.RasAutoDialAddress"/> object.</returns>
        public RasAutoDialAddress GetAutoDialAddress(string address)
        {
            RasAutoDialAddress retval = null;

            int size = Marshal.SizeOf(typeof(NativeMethods.RASAUTODIALENTRY));

            RasGetAutodialAddressParams info = new RasGetAutodialAddressParams();
            info.BufferSize = new IntPtr(size);
            info.Count = IntPtr.Zero;

            bool retry = false;

            do
            {
                NativeMethods.RASAUTODIALENTRY entry = new NativeMethods.RASAUTODIALENTRY();
                entry.size = size;

                try
                {
                    info.Address = Marshal.AllocHGlobal(info.BufferSize);
                    Marshal.StructureToPtr(entry, info.Address, true);

                    int ret = UnsafeNativeMethods.Instance.GetAutodialAddress(info);
                    if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL)
                    {
                        retry = true;
                    }
                    else if (ret == NativeMethods.SUCCESS)
                    {
                        retry = false;

                        NativeMethods.RASAUTODIALENTRY[] entries = Utilities.CreateArrayOfType<NativeMethods.RASAUTODIALENTRY>(info.Address, size, info.Count.ToInt32());
                        retval = new RasAutoDialAddress(address);

                        if (entries != null || entries.Length > 0)
                        {
                            for (int index = 0; index < entries.Length; index++)
                            {
                                NativeMethods.RASAUTODIALENTRY current = entries[index];
                                retval.Entries.Add(new RasAutoDialEntry(current.dialingLocation, current.entryName));
                            }
                        }
                    }
                    else if (ret == NativeMethods.ERROR_FILE_NOT_FOUND)
                    {
                        retry = false;
                    }
                    else
                    {
                        ThrowHelper.ThrowRasException(ret);
                    }
                }
                catch (EntryPointNotFoundException)
                {
                    ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform);
                }
                finally
                {
                    if (info.Address != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(info.Address);
                    }
                }
            }
            while (retry);

            return retval;
        }
Esempio n. 3
0
        public void GetAutoDialAddressTest()
        {
            NativeMethods.RASAUTODIALENTRY[] expected = new NativeMethods.RASAUTODIALENTRY[]
            {
                new NativeMethods.RASAUTODIALENTRY()
                {
                    dialingLocation = 0,
                    entryName = "Test Entry 1",
                    options = 0
                },
                new NativeMethods.RASAUTODIALENTRY()
                {
                    dialingLocation = 1,
                    entryName = "Test Entry 2",
                    options = 1
                }
            };

            int expectedSize = Marshal.SizeOf(typeof(NativeMethods.RASAUTODIALENTRY)) * expected.Length;

            StructBufferedPInvokeMock<RasGetAutodialAddressParams, NativeMethods.RASAUTODIALENTRY> target = new StructBufferedPInvokeMock<RasGetAutodialAddressParams, NativeMethods.RASAUTODIALENTRY>();
            target.Result = expected;

            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetAutodialAddress(It.Is<RasGetAutodialAddressParams>(i => i.BufferSize.ToInt32() != expectedSize))).Callback((RasGetAutodialAddressParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.ERROR_BUFFER_TOO_SMALL);
            mock.Setup(o => o.GetAutodialAddress(It.Is<RasGetAutodialAddressParams>(i => i.BufferSize.ToInt32() == expectedSize))).Callback((RasGetAutodialAddressParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.SUCCESS);

            string address = "Test";
            RasAutoDialAddress actual = RasHelper.Instance.GetAutoDialAddress(address);

            Assert.AreEqual(address, actual.Address);
            Assert.IsNotNull(actual.Entries);
            Assert.AreEqual(expected.Length, actual.Entries.Count);

            for (int index = 0; index < expected.Length; index++)
            {
                Assert.AreEqual(expected[index].dialingLocation, actual.Entries[index].DialingLocation);
                Assert.AreEqual(expected[index].entryName, actual.Entries[index].EntryName);
            }
        }