Пример #1
0
        private static ICollection <string> EnumerateResources(NetResource pRsrc, ResourceScope scope, ResourceType type,
                                                               ResourceUsage usage, ResourceDisplayType displayType)
        {
            List <string> result     = new List <string>();
            uint          bufferSize = 16384;
            IntPtr        buffer     = Marshal.AllocHGlobal((int)bufferSize);

            try
            {
                IntPtr handle;
                uint   cEntries = 1;

                ErrorCodes res = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

                if (res == ErrorCodes.NoError)
                {
                    try
                    {
                        do
                        {
                            res = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

                            if (res == ErrorCodes.NoError)
                            {
                                Marshal.PtrToStructure(buffer, pRsrc);

                                if (pRsrc.dwDisplayType == displayType)
                                {
                                    result.Add(pRsrc.lpRemoteName);
                                }

                                // If the current NetworkResource is a container, we call EnumerateResources recursively.
                                // In some situations, the lpRemoteName in the NetworkResource is null or empty. In this case
                                // we do not call EnumerateResources recursively as this leads to an infinite loop of
                                // recursive calls. For details see Jira MP2-356
                                if ((pRsrc.dwUsage & ResourceUsage.Container) == ResourceUsage.Container && !String.IsNullOrEmpty(pRsrc.lpRemoteName))
                                {
                                    result.AddRange(EnumerateResources(pRsrc, scope, type, usage, displayType));
                                }
                            }
                            else if (res != ErrorCodes.ErrorNoMoreItems)
                            {
                                break;
                            }
                        } while (res != ErrorCodes.ErrorNoMoreItems);
                    }
                    finally
                    {
                        WNetCloseEnum(handle);
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
            return(result);
        }
Пример #2
0
        private static ICollection <string> EnumerateResources(NetResource pRsrc, ResourceScope scope, ResourceType type,
                                                               ResourceUsage usage, ResourceDisplayType displayType)
        {
            List <string> result     = new List <string>();
            uint          bufferSize = 16384;
            IntPtr        buffer     = Marshal.AllocHGlobal((int)bufferSize);

            try
            {
                IntPtr handle;
                uint   cEntries = 1;

                ErrorCodes res = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

                if (res == ErrorCodes.NoError)
                {
                    try
                    {
                        do
                        {
                            res = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

                            if (res == ErrorCodes.NoError)
                            {
                                Marshal.PtrToStructure(buffer, pRsrc);

                                if (pRsrc.dwDisplayType == displayType)
                                {
                                    result.Add(pRsrc.lpRemoteName);
                                }

                                if ((pRsrc.dwUsage & ResourceUsage.Container) == ResourceUsage.Container)
                                {
                                    result.AddRange(EnumerateResources(pRsrc, scope, type, usage, displayType));
                                }
                            }
                            else if (res != ErrorCodes.ErrorNoMoreItems)
                            {
                                break;
                            }
                        } while (res != ErrorCodes.ErrorNoMoreItems);
                    }
                    finally
                    {
                        WNetCloseEnum(handle);
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
            return(result);
        }
Пример #3
0
        public ServerEnum(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
        {
            NETRESOURCE pRsrc = new NETRESOURCE();

            EnumerateServers(pRsrc, scope, type, usage, displayType, "");
        }
Пример #4
0
        private void EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType, string strDomain)
        {
            uint       bufferSize = 16384;
            IntPtr     buffer     = Marshal.AllocHGlobal((int)bufferSize);
            IntPtr     handle     = IntPtr.Zero;
            ErrorCodes result;
            uint       cEntries = 1;

            result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

            if (result == ErrorCodes.NO_ERROR)
            {
                do
                {
                    result = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

                    if (result == ErrorCodes.NO_ERROR)
                    {
                        Marshal.PtrToStructure(buffer, pRsrc);

                        // If the discovered item is of the correct type then add a server entry to our array
                        if (pRsrc.dwDisplayType == displayType)
                        {
                            string strName;
                            strName = pRsrc.lpRemoteName;

                            // Remove any "\\" prefix
                            if (strName.StartsWith("\\"))
                            {
                                strName = strName.Remove(0, 2);
                            }
                            Server server = new Server(strName, strDomain);
                            aData.Add(server);
                        }

                        // If this is a domain or NDS container then store the name as the parent
                        if ((pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN) ||
                            (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_NDSCONTAINER))
                        {
                            strDomain = pRsrc.lpRemoteName;
                        }

                        // If this is a container then call ourselves recursively to expand it
                        if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER) == ResourceUsage.RESOURCEUSAGE_CONTAINER)
                        {
                            EnumerateServers(pRsrc, scope, type, usage, displayType, strDomain);
                        }
                    }
                    else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                    {
                        break;
                    }
                }while   (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

                WNetCloseEnum(handle);
            }

            Marshal.FreeHGlobal((IntPtr)buffer);
        }
Пример #5
0
		public ServerEnum(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType, string kPath)
		{
			var netRoot = new NetResource();
			EnumerateServers(netRoot, scope, type, usage, displayType, kPath);
		}
Пример #6
0
		private void EnumerateServers(NetResource pRsrc, ResourceScope scope, ResourceType type,
			ResourceUsage usage, ResourceDisplayType displayType, string kPath)
		{
			uint bufferSize = 16384;
			var buffer = Marshal.AllocHGlobal((int)bufferSize);
			IntPtr handle;
			uint cEntries = 1;
			var serverenum = false;

			var result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

			if (result == ErrorCodes.NO_ERROR)
			{
				do
				{
					result = WNetEnumResource(handle, ref cEntries, buffer, ref	bufferSize);

					if ((result == ErrorCodes.NO_ERROR))
					{
						Marshal.PtrToStructure(buffer, pRsrc);

						if (kPath == "")
						{
							if ((pRsrc.dwDisplayType == displayType) || (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN))
								_aData.Add(pRsrc.lpRemoteName + "|" + pRsrc.dwDisplayType);

							if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER) == ResourceUsage.RESOURCEUSAGE_CONTAINER)
								if ((pRsrc.dwDisplayType == displayType))
									EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
						}
						else
						{
							if (pRsrc.dwDisplayType == displayType)
							{
								_aData.Add(pRsrc.lpRemoteName);
								EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
								serverenum = true;
							}
							if (!serverenum)
							{
								if (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE)
									_aData.Add(pRsrc.lpRemoteName + "-share");
							}
							else
								serverenum = false;

							if ((kPath.IndexOf(pRsrc.lpRemoteName) >= 0) || (String.Compare(pRsrc.lpRemoteName, "Microsoft Windows Network") == 0))
								EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
						}
					}
					else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
						break;
				} while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

				WNetCloseEnum(handle);
			}
			Marshal.FreeHGlobal(buffer);
		}
Пример #7
0
        private static IEnumerable<string> EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
        {
            uint bufferSize = 16384;
            IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize);
            IntPtr handle = IntPtr.Zero;
            ErrorCodes result;
            uint cEntries = 1;

            result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

            if (result == ErrorCodes.NO_ERROR)
            {
                do
                {
                    result = WNetEnumResource(handle, ref cEntries, buffer, ref	bufferSize);

                    if (result == ErrorCodes.NO_ERROR)
                    {
                        Marshal.PtrToStructure(buffer, pRsrc);

                        if (pRsrc.dwDisplayType == displayType)
                            yield return pRsrc.lpRemoteName;

                        if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER) == ResourceUsage.RESOURCEUSAGE_CONTAINER)
                        {
                            foreach( string share in EnumerateServers(pRsrc, scope, type, usage, displayType))
                                yield return share;
                        }
                    }
                    else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                        break;
                } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

                WNetCloseEnum(handle);
            }

            Marshal.FreeHGlobal((IntPtr)buffer);
        }
Пример #8
0
            public ServerEnum(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType, string kPath)
            {
                NETRESOURCE netRoot = new NETRESOURCE();

                EnumerateServers(netRoot, scope, type, usage, displayType, kPath);
            }
Пример #9
0
            private void EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType, string kPath)
            {
                uint       bufferSize = 16384;
                IntPtr     buffer     = Marshal.AllocHGlobal((IntPtr)bufferSize);
                IntPtr     Handle     = IntPtr.Zero;
                ErrorCodes result;
                uint       cEntries   = 1;
                bool       ServerEnum = false;

                result = WNetOpenEnum(scope, type, usage, pRsrc, ref Handle);                 // out

                if (result == ErrorCodes.NO_ERROR)
                {
                    do
                    {
                        result = WNetEnumResource(Handle, ref cEntries, buffer, ref bufferSize);                         // ref ref

                        if (result == ErrorCodes.NO_ERROR)
                        {
                            Marshal.PtrToStructure(buffer, pRsrc);

                            if (string.Compare(kPath, "") == 0)
                            {
                                if (pRsrc.dwDisplayType == displayType || pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN)
                                {
                                    aData.Add(pRsrc.lpRemoteName + "|" + pRsrc.dwDisplayType);
                                    if ((pRsrc.dwUsage | ResourceUsage.RESOURCEUSAGE_CONTAINER) == ResourceUsage.RESOURCEUSAGE_CONTAINER)
                                    {
                                        if (pRsrc.dwDisplayType == displayType)
                                        {
                                            EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                                        }
                                    }
                                }
                                else
                                {
                                    if (pRsrc.dwDisplayType == displayType)
                                    {
                                        aData.Add(pRsrc.lpRemoteName);
                                        EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                                        //return
                                        ServerEnum = true;
                                    }
                                    if (!ServerEnum)
                                    {
                                        if (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE)
                                        {
                                            aData.Add(pRsrc.lpRemoteName + "-share");
                                        }
                                    }
                                    else
                                    {
                                        ServerEnum = false;
                                    }
                                    if (kPath.IndexOf(pRsrc.lpRemoteName) >= 0 || string.Compare(pRsrc.lpRemoteName, "Microsoft Windows Network") == 0)
                                    {
                                        EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                                    }
                                }
                            }
                            else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                            {
                                break;
                            }
                        }
                    } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

                    WNetCloseEnum(Handle);
                }

                Marshal.FreeHGlobal(buffer);
            }
    private static ICollection<string> EnumerateResources(NetResource pRsrc, ResourceScope scope, ResourceType type,
        ResourceUsage usage, ResourceDisplayType displayType)
    {
      List<string> result = new List<string>();
      uint bufferSize = 16384;
      IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize);
      try
      {
        IntPtr handle;
        uint cEntries = 1;

        ErrorCodes res = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

        if (res == ErrorCodes.NoError)
          try
          {
            do
            {
              res = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

              if (res == ErrorCodes.NoError)
              {
                Marshal.PtrToStructure(buffer, pRsrc);

                if (pRsrc.dwDisplayType == displayType)
                  result.Add(pRsrc.lpRemoteName);

                // If the current NetworkResource is a container, we call EnumerateResources recursively.
                // In some situations, the lpRemoteName in the NetworkResource is null or empty. In this case
                // we do not call EnumerateResources recursively as this leads to an infinite loop of
                // recursive calls. For details see Jira MP2-356
                if ((pRsrc.dwUsage & ResourceUsage.Container) == ResourceUsage.Container && !String.IsNullOrEmpty(pRsrc.lpRemoteName))
                  result.AddRange(EnumerateResources(pRsrc, scope, type, usage, displayType));
              }
              else if (res != ErrorCodes.ErrorNoMoreItems)
                break;
            } while (res != ErrorCodes.ErrorNoMoreItems);
          }
          finally
          {
            WNetCloseEnum(handle);
          }
      }
      finally
      {
        Marshal.FreeHGlobal(buffer);
      }
      return result;
    }
 public static ICollection<string> EnumerateResources(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
 {
   NetResource pRsrc = new NetResource();
   return EnumerateResources(pRsrc, scope, type, usage, displayType);
 }
Пример #12
0
        private void EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
        {
            uint       bufferSize = 16384;
            IntPtr     buffer     = Marshal.AllocHGlobal((int)bufferSize);
            IntPtr     handle     = IntPtr.Zero;
            ErrorCodes result;
            uint       cEntries = 1;

            result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

            if (result == ErrorCodes.NO_ERROR)
            {
                do
                {
                    result = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

                    if (result == ErrorCodes.NO_ERROR)
                    {
                        Marshal.PtrToStructure(buffer, pRsrc);

                        if (pRsrc.dwDisplayType == displayType)
                        {
                            aData.Add(pRsrc.lpRemoteName);
                        }

                        if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER) == ResourceUsage.RESOURCEUSAGE_CONTAINER)
                        {
                            EnumerateServers(pRsrc, scope, type, usage, displayType);
                        }
                    }
                    else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                    {
                        break;
                    }
                } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

                WNetCloseEnum(handle);
            }

            Marshal.FreeHGlobal((IntPtr)buffer);
        }
Пример #13
0
        public static ICollection <string> EnumerateResources(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
        {
            NetResource pRsrc = new NetResource();

            return(EnumerateResources(pRsrc, scope, type, usage, displayType));
        }
Пример #14
0
        private void EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage,
                                      ResourceDisplayType displayType, string kPath)
        {
            uint   bufferSize = 16384;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);
            IntPtr handle;
            uint   cEntries   = 1;
            bool   serverenum = false;

            ErrorCodes result = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

            if (result == ErrorCodes.NO_ERROR)
            {
                do
                {
                    result = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

                    if ((result == ErrorCodes.NO_ERROR))
                    {
                        Marshal.PtrToStructure(buffer, pRsrc);

                        if (kPath == "")
                        {
                            if ((pRsrc.dwDisplayType == displayType) ||
                                (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN))
                            {
                                aData.Add(pRsrc.lpRemoteName + "|" + pRsrc.dwDisplayType);
                            }

                            if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER) ==
                                ResourceUsage.RESOURCEUSAGE_CONTAINER)
                            {
                                if ((pRsrc.dwDisplayType == displayType))
                                {
                                    EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                                }
                            }
                        }
                        else
                        {
                            if (pRsrc.dwDisplayType == displayType)
                            {
                                aData.Add(pRsrc.lpRemoteName);
                                EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                                //return;
                                serverenum = true;
                            }
                            if (!serverenum)
                            {
                                if (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE)
                                {
                                    aData.Add(pRsrc.lpRemoteName + "_share");
                                }
                            }
                            else
                            {
                                serverenum = false;
                            }
                            if ((kPath.IndexOf(pRsrc.lpRemoteName) >= 0) ||
                                (pRsrc.lpRemoteName == "Microsoft Windows Network"))
                            {
                                EnumerateServers(pRsrc, scope, type, usage, displayType, kPath);
                            }
                        }
                    }
                    else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                    {
                        break;
                    }
                } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);

                WNetCloseEnum(handle);
            }

            Marshal.FreeHGlobal(buffer);
        }
    private static ICollection<string> EnumerateResources(NetResource pRsrc, ResourceScope scope, ResourceType type,
        ResourceUsage usage, ResourceDisplayType displayType)
    {
      List<string> result = new List<string>();
      uint bufferSize = 16384;
      IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize);
      try
      {
        IntPtr handle;
        uint cEntries = 1;

        ErrorCodes res = WNetOpenEnum(scope, type, usage, pRsrc, out handle);

        if (res == ErrorCodes.NoError)
          try
          {
            do
            {
              res = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);

              if (res == ErrorCodes.NoError)
              {
                Marshal.PtrToStructure(buffer, pRsrc);

                if (pRsrc.dwDisplayType == displayType)
                  result.Add(pRsrc.lpRemoteName);

                if ((pRsrc.dwUsage & ResourceUsage.Container) == ResourceUsage.Container)
                  result.AddRange(EnumerateResources(pRsrc, scope, type, usage, displayType));
              }
              else if (res != ErrorCodes.ErrorNoMoreItems)
                break;
            } while (res != ErrorCodes.ErrorNoMoreItems);
          }
          finally
          {
            WNetCloseEnum(handle);
          }
      }
      finally
      {
        Marshal.FreeHGlobal(buffer);
      }
      return result;
    }
        public ServerEnum(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType)
        {
            NETRESOURCE pRsrc = new NETRESOURCE();

            EnumerateServers(pRsrc, scope, type, usage, displayType);
        }