private static IEnumerable <Win32MethodsNetwork.SHARE_INFO_1> GetNetShares(string computer) { IntPtr pInfo; int entriesread = 0; int totalentries = 0; Win32MethodsNetwork.NERR err = Win32MethodsNetwork.NetShareEnum(computer, 1, out pInfo, Win32MethodsNetwork.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, 0); if ((err == Win32MethodsNetwork.NERR.NERR_SUCCESS || err == Win32MethodsNetwork.NERR.ERROR_MORE_DATA) && pInfo != IntPtr.Zero) { try { const uint stypeIPC = (uint)Win32MethodsNetwork.SHARE_TYPE.STYPE_IPC; int ptr = pInfo.ToInt32(); for (int i = 0; i < entriesread; i++) { var shi1 = (Win32MethodsNetwork.SHARE_INFO_1)Marshal.PtrToStructure(new IntPtr(ptr), typeof(Win32MethodsNetwork.SHARE_INFO_1)); if ((shi1.shi1_type & stypeIPC) != stypeIPC) { yield return(shi1); } ptr += Marshal.SizeOf(typeof(Win32MethodsNetwork.SHARE_INFO_1)); } } finally { if (pInfo != IntPtr.Zero) { Win32MethodsNetwork.NetApiBufferFree(pInfo); } } } }
private static IEnumerable <Win32MethodsNetwork.SERVER_INFO_101> GetNetServers(string domain, Win32MethodsNetwork.SV_101_TYPES types) { IntPtr pInfo; uint entriesread = 0; uint totalentries = 0; Win32MethodsNetwork.NERR err; unchecked { err = Win32MethodsNetwork.NetServerEnum(null, 101, out pInfo, (uint)-1, ref entriesread, ref totalentries, types, domain, 0); } if ((err == Win32MethodsNetwork.NERR.NERR_SUCCESS || err == Win32MethodsNetwork.NERR.ERROR_MORE_DATA) && pInfo != IntPtr.Zero) { try { int ptr = pInfo.ToInt32(); for (int i = 0; i < entriesread; i++) { yield return((Win32MethodsNetwork.SERVER_INFO_101)Marshal.PtrToStructure(new IntPtr(ptr), typeof(Win32MethodsNetwork.SERVER_INFO_101))); ptr += Marshal.SizeOf(typeof(Win32MethodsNetwork.SERVER_INFO_101)); } } finally { if (pInfo != IntPtr.Zero) { Win32MethodsNetwork.NetApiBufferFree(pInfo); } } } }