/// <summary> /// Obte els recursos compartits d'un servidor CIFS NTWindows 9x /// </summary> /// <param name="server">Nom del servidor</param> /// <param name="shares">Colecció de recursos</param> protected static Shares EnumerateShares9x(string server) { Shares shares = new Shares(); Share s; int level = 50; int nRet = 0; ushort entriesRead, totalEntries; Type t = typeof(WrapperWin32Api.SHARE_INFO_2_9x); int size = Marshal.SizeOf(t); ushort cbBuffer = (ushort)(WrapperWin32Api.MAX_SI50_ENTRIES * size); //A Win9x, s'ha de reserva la memòria del buffer abans de cridar l'API IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer); try { nRet = WrapperWin32Api.NetShareEnum(server, level, pBuffer, cbBuffer, out entriesRead, out totalEntries); if (WrapperWin32Api.ERROR_WRONG_LEVEL == nRet) { level = 1; //el matix problema del permisos i les estructures de retorn de dades t = typeof(WrapperWin32Api.SHARE_INFO_1_9x); size = Marshal.SizeOf(t); nRet = WrapperWin32Api.NetShareEnum(server, level, pBuffer, cbBuffer, out entriesRead, out totalEntries); } if (WrapperWin32Api.NO_ERROR == nRet || WrapperWin32Api.ERROR_MORE_DATA == nRet) { for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size) { IntPtr pItem = new IntPtr(lpItem); if (1 == level) { WrapperWin32Api.SHARE_INFO_1_9x si = (WrapperWin32Api.SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t); s = new Share(); s.comment = si.Remark; s.name = si.NetName; s.type = CommonShareTypes(si.ShareType); shares.addShare(s); } else { WrapperWin32Api.SHARE_INFO_2_9x si = (WrapperWin32Api.SHARE_INFO_2_9x)Marshal.PtrToStructure(pItem, t); System.Console.WriteLine(si.NetName); s = new Share(); s.comment = si.Remark; s.name = si.NetName; s.type = CommonShareTypes(si.ShareType); shares.addShare(s); } } } else { Console.WriteLine(nRet); } return(shares); } finally { //Netejem el buffe Marshal.FreeHGlobal(pBuffer); } }
/// <summary> /// Obte els recursos compartits d'un servidor CIFS NT /// </summary> /// <param name="server">Nom del servidor</param> /// <param name="shares">Colecció de recursos</param> protected static Shares EnumerateSharesNT(string server) { Shares shares = new Shares(); Share s; int level = 2; int entriesRead, totalEntries, nRet, hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { nRet = WrapperWin32Api.NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (WrapperWin32Api.ERROR_ACCESS_DENIED == nRet) { //depenent del tipus de nivell d'acces que tenim la consulta obtindrà un tipus de dades // o un altres, això afectarà a l'estructura on es carrega el resultat level = 1; nRet = WrapperWin32Api.NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (WrapperWin32Api.NO_ERROR == nRet && entriesRead > 0) { Type t = (2 == level) ? typeof(WrapperWin32Api.SHARE_INFO_2) : typeof(WrapperWin32Api.SHARE_INFO_1); int offset = Marshal.SizeOf(t); for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset) { IntPtr pItem = new IntPtr(lpItem); if (1 == level) //estructura basica { WrapperWin32Api.SHARE_INFO_1 si = (WrapperWin32Api.SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); s = new Share(); s.comment = si.Remark; s.name = si.NetName; s.type = CommonShareTypes(si.ShareType); shares.addShare(s); } else //estructura amb més informació, però seguirem agafant la mateixa { WrapperWin32Api.SHARE_INFO_2 si = (WrapperWin32Api.SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); s = new Share(); s.comment = si.Remark; s.name = si.NetName; s.type = CommonShareTypes(si.ShareType); shares.addShare(s); } } } return(shares); } finally { // Netejem el buffer if (IntPtr.Zero != pBuffer) { WrapperWin32Api.NetApiBufferFree(pBuffer); } } }