예제 #1
0
/// <summary>
/// Get list of network resources
/// </summary>
/// <param name="resScope"></param>
/// <param name="resType"></param>
/// <param name="resUsage"></param>
/// <param name="o"></param>
/// <param name="netResources"></param>

        static void GetNetResources(
            RESOURCE_SCOPE resScope,
            RESOURCE_TYPE resType,
            RESOURCE_USAGE resUsage,
            Object o,
            List <NETRESOURCE> netResources)
        {
            int    iRet;
            IntPtr ptrHandle = new IntPtr();

            try
            {
                iRet = WNetOpenEnum(
                    resScope, resType, resUsage, o, out ptrHandle);
                if (iRet != 0)
                {
                    return;
                }

                int         entries;
                int         buffer    = 16384;
                IntPtr      ptrBuffer = Marshal.AllocHGlobal(buffer);
                NETRESOURCE nr;
                while (true)
                {
                    entries = -1;
                    buffer  = 16384;
                    iRet    = WNetEnumResource(ptrHandle, ref entries, ptrBuffer, ref buffer);
                    if ((iRet != 0) || (entries < 1))
                    {
                        break;
                    }
                    Int32 ptr = ptrBuffer.ToInt32();
                    for (int i = 0; i < entries; i++)
                    {
                        nr = (NETRESOURCE)Marshal.PtrToStructure(new IntPtr(ptr), typeof(NETRESOURCE));
                        if (RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == ((RESOURCE_USAGE)nr.dwUsage
                                                                       & RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))
                        {
                            //call recursively to get all entries in a container
                            GetNetResources(resScope, resType, resUsage, nr, netResources);
                        }
                        ptr += Marshal.SizeOf(nr);
                        netResources.Add(nr);
                        //Console.WriteLine(" {0} : LocalName='{1}' RemoteName='{2}'",
                        //nr.dwDisplayType.ToString(), nr.LocalName, nr.RemoteName);
                    }
                }
                Marshal.FreeHGlobal(ptrBuffer);
                iRet = WNetCloseEnum(ptrHandle);
            }
            catch (Exception e)
            {
            }
        }
예제 #2
0
 public static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope
                                       , RESOURCE_TYPE dwType
                                       , RESOURCE_USAGE dwUsage
                                       , ref NETRESOURCE lpNetResource
                                       , ref IntPtr lphEnum);
 static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope, RESOURCE_TYPE dwType, RESOURCE_USAGE dwUsage,
                                [MarshalAs(UnmanagedType.AsAny)][In] object lpNetResource, out IntPtr lphEnum);
예제 #4
0
		public static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope, RESOURCE_TYPE dwType, RESOURCE_USAGE dwUsage, [MarshalAs(UnmanagedType.AsAny)][In] Object lpNetResource, out IntPtr lphEnum);