public static List <SHARE_INFO_2> List() { int entriesread = 0; int totalentries = 0; int resume_handle = 0; int nStructSize = Marshal.SizeOf(typeof(SHARE_INFO_2)); IntPtr bufptr = IntPtr.Zero; int result = NetShareEnum(null, 2, ref bufptr, PREFMAXLEN, ref entriesread, ref totalentries, ref resume_handle); if (result == 0) { List <SHARE_INFO_2> ShareInfos = new List <SHARE_INFO_2>(); IntPtr shi2ptr = bufptr; for (int i = 0; i < entriesread; i++) { SHARE_INFO_2 shi2 = (SHARE_INFO_2)Marshal.PtrToStructure(shi2ptr, typeof(SHARE_INFO_2)); ShareInfos.Add(shi2); shi2ptr = new IntPtr(shi2ptr.ToInt32() + nStructSize); } NetApiBufferFree(bufptr); return(ShareInfos); } else { throw new System.ComponentModel.Win32Exception(result); } }
public static SHARE_INFO_2 GetShareInfo_2(string server_name, string share_name) { IntPtr buffer = IntPtr.Zero; SHARE_INFO_2 ret = new SHARE_INFO_2(); try { int res = WinApiNET.NetShareGetInfo(server_name, share_name, NetShareInfoLevel.INFO_2, ref buffer); if (res != WinApiNET.NERR_Success) { throw new Win32Exception(res); } ret = (SHARE_INFO_2)Marshal.PtrToStructure(buffer, typeof(SHARE_INFO_2)); } finally { if (buffer != IntPtr.Zero) { WinApiNET.NetApiBufferFree(buffer); } } return(ret); }
private static List <ShareInfo> GetShareInfoNT(string serverName, IList <ShareType> shareTypes) { int level = 2; int entriesRead, totalEntries, nRet, hResume = 0; IntPtr pBuffer = IntPtr.Zero; List <ShareInfo> shareInfoList = new List <ShareInfo>(); try { nRet = NetShareEnum(serverName, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (nRet == ERROR_ACCESS_DENIED) { //Need admin for level 2, drop to level 1 level = 1; nRet = NetShareEnum(serverName, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (nRet == NO_ERROR && entriesRead > 0) { Type t = (level == 2) ? typeof(SHARE_INFO_2) : typeof(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 (level == 1) { SHARE_INFO_1 shareInfo = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); if (shareTypes.Contains(shareInfo.ShareType)) { shareInfoList.Add(new ShareInfo(serverName, shareInfo.NetName, string.Empty, shareInfo.ShareType, shareInfo.Remark)); } } else { SHARE_INFO_2 shareInfo = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); if (shareTypes.Contains(shareInfo.ShareType)) { shareInfoList.Add(new ShareInfo(serverName, shareInfo.NetName, shareInfo.Path, shareInfo.ShareType, shareInfo.Remark)); } } } } } finally { // Clean up buffer allocated by system if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } return(shareInfoList); }
/// <summary> /// Enumerates the shares on Windows NT /// </summary> /// <param name="server">The server name</param> /// <param name="shares">The ShareCollection</param> protected static void EnumerateSharesNT(string server, ShareCollection shares) { int level = 2; int entriesRead, totalEntries, nRet, hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (ERROR_ACCESS_DENIED == nRet) { //Need admin for level 2, drop to level 1 level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (NO_ERROR == nRet && entriesRead > 0) { Type t = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1); int offset = Marshal.SizeOf(t); IntPtr pItem = pBuffer; for (long i = 0; i < entriesRead; i++) { if (1 == level) { SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); } else { SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); } pItem = IntPtr.Add(pItem, offset); } } } finally { // Clean up buffer allocated by system if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }
public static string GetShareLocalPath(string server, string shareName) { string path = string.Empty; IntPtr ptr = IntPtr.Zero; int result = NetShareGetInfo(server, shareName, 2, ref ptr); if (result == (int)GetShareInfoResultType.NerrSuccess) { SHARE_INFO_2 shareInfo = (SHARE_INFO_2) Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_2)); path = shareInfo.shi2_path; NetApiBufferFree(ptr); } return(path); }
protected static void EnumerateSharesNT(string server, ShareCollection shares) { int level = 2; int entriesRead, totalEntries, nRet, hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (ERROR_ACCESS_DENIED == nRet) { level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (NO_ERROR == nRet && entriesRead > 0) { Type t = (2 == level)?typeof(SHARE_INFO_2):typeof(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) { SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); } else { SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); } } } } finally { if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }
private void fill_share_page() { try { string[] share_splitted = resource_intern.lpRemoteName.Split (new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); SHARE_INFO_2 share_info = WinApiNETwrapper.GetShareInfo_2(share_splitted[0], share_splitted[1]); textBoxShareType.Text = share_info.shi2_type.ToString(); textBoxShareComment.Text = share_info.shi2_remark; textBoxSharePermissions.Text = share_info.shi2_permissions.ToString(); textBoxShareMaxuses.Text = share_info.shi2_max_uses.ToString(); textBoxShareCurrentUses.Text = share_info.shi2_current_uses.ToString(); textBoxSharePath.Text = share_info.shi2_path; } catch (Exception ex) { errorProvider1.SetIconAlignment(textBoxShareType, ErrorIconAlignment.MiddleLeft); errorProvider1.SetError(textBoxShareType, ex.Message); } }
public static string[] GetShareInfo(IntPtr handle_b, string sharename, string sHostname) { //// establish null session //if(!Session.EnsureNullSession(sHostname, ce)) //{ // return null; //} try { string[] sShareInfo = null; IntPtr pBuf = IntPtr.Zero; int nret = -1; int param_err = 0; Logger.Log(String.Format("NetShareGetInfo(handle_b={0:x},sHostname={1}, sharename={2}) called", handle_b.ToInt32(), sHostname, sharename), Logger.FileShareManagerLogLevel); if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { nret = NetShareGetInfo( sHostname, sharename, 2, out pBuf ); } else { nret = NetShareGetInfo( handle_b, sHostname, sharename, 2, ref pBuf, ref param_err ); } Logger.Log(String.Format( "NetShareGetInfo(); result={0}, pBuf={1}, param_err={2}", nret, pBuf, param_err)); // now, iterate through the data in pBuf IntPtr pCur = pBuf; // marshal the entry into SHARE_INFO_2 si2 = (SHARE_INFO_2)Marshal.PtrToStructure(pCur, typeof(SHARE_INFO_2)); // only allow regular diskshares // TODO: Review this. Should we not display admin shares? if (si2.shi2_type == STYPE_DISKTREE) { sShareInfo = new string[] { "share", si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString(), si2.shi2_max_uses.ToString() }; } else { sShareInfo = new string[] { "adminshare", si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString(), si2.shi2_max_uses.ToString() }; } if (pBuf != IntPtr.Zero) { // free the data if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { NetApiBufferFree(pBuf); } else { SrvSvcFreeMemory(pBuf); } } // all done return(sShareInfo); } catch (Win32Exception we) { throw new LikewiseAPIException(we); } }
public static Dictionary <int, string[]> EnumShares(IntPtr handle_b, CredentialEntry ce, string sHostname) { //// establish null session //if(!Session.EnsureNullSession(sHostname, ce)) //{ // return null; //} try { Dictionary <int, string[]> ShareList = new Dictionary <int, string[]>(); IntPtr pBuf = IntPtr.Zero; int cRead = 0; int cTotal = 0; int hResume = 0; int maxlen = -1; int nret = -1; if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { Logger.Log(String.Format("NetShareEnum(sHostname={0}) called", sHostname), Logger.FileShareManagerLogLevel); nret = NetShareEnum(sHostname, 2, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } else { Logger.Log(String.Format("NetShareEnum(handle_b={0:x},sHostname={1}) called", handle_b.ToInt32(), sHostname), Logger.FileShareManagerLogLevel); maxlen = 20; nret = NetShareEnum(handle_b, sHostname, 2, ref pBuf, maxlen, ref cRead, ref cTotal, ref hResume); } Logger.Log(String.Format( "NetShareEnum(); result={0}, pBuf={1}, cRead={2}, cTotal={3}, hResume={4}", nret, pBuf, cRead, cTotal, hResume)); // now, iterate through the data in pBuf IntPtr pCur = pBuf; for (int i = 0; i < cRead; i++) { // marshal the entry into SHARE_INFO_2 si2 = (SHARE_INFO_2)Marshal.PtrToStructure(pCur, typeof(SHARE_INFO_2)); // only allow regular diskshares // TODO: Review this. Should we not display admin shares? if (si2.shi2_type == STYPE_DISKTREE) { string[] sShareInfo = { si2.shi2_netname, si2.shi2_path, si2.shi2_remark, si2.shi2_current_uses.ToString() }; ShareList.Add(i, sShareInfo); } // advance to the next entry pCur = (IntPtr)((int)pCur + Marshal.SizeOf(si2)); } if (pBuf != IntPtr.Zero) { // free the data if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows) { NetApiBufferFree(pBuf); } else { SrvSvcFreeMemory(pBuf); } } // all done return(ShareList); } catch (Win32Exception we) { throw new LikewiseAPIException(we); } }
public static SHARE_INFO_2[] GetShareInfos_2(string server_name) { List <SHARE_INFO_2> ret_list = new List <SHARE_INFO_2>(); IntPtr net_buffer = IntPtr.Zero; int entries_readed = 0; int entries_total = 0; uint resume_handle = 0; int res = 0; int res_free = 0; do { if ((server_name == null) || (server_name == string.Empty)) { res = WinApiNET.NetShareEnum (IntPtr.Zero, NET_INFO_LEVEL.LEVEL_2, ref net_buffer, WinApiNET.MAX_PREFERRED_LENGTH, ref entries_readed, ref entries_total, ref resume_handle); } else { res = WinApiNET.NetShareEnum (server_name, NET_INFO_LEVEL.LEVEL_2, ref net_buffer, WinApiNET.MAX_PREFERRED_LENGTH, ref entries_readed, ref entries_total, ref resume_handle); } //check result if (res == WinApiNET.NERR_Success) { //success, add to result list ret_list.AddRange(SHARE_INFO_2.FromBuffer(net_buffer, entries_readed)); //free buffer res_free = WinApiNET.NetApiBufferFree(net_buffer); if (res_free != WinApiNET.NERR_Success) { throw new Win32Exception(res_free); } //break cycle break; } if (res == WinApiNET.ERROR_MORE_DATA) { //success, but more data available ret_list.AddRange(SHARE_INFO_2.FromBuffer(net_buffer, entries_readed)); //free buffer res_free = WinApiNET.NetApiBufferFree(net_buffer); if (res_free != WinApiNET.NERR_Success) { throw new Win32Exception(res_free); } //continue cycle continue; } //now res is error code Win32Exception win_ex = new Win32Exception(res); throw win_ex; } while (true); return(ret_list.ToArray()); }
/// <summary> /// Enumerates the shares on Windows NT /// </summary> /// <param name="server">The server name</param> /// <param name="shares">The ShareCollection</param> protected static void EnumerateSharesNT(string server, ShareCollection shares) { int level = 2; int entriesRead, totalEntries, nRet, hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (ERROR_ACCESS_DENIED == nRet) { //Need admin for level 2, drop to level 1 level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (NO_ERROR == nRet && entriesRead > 0) { Type t = (2 == level) ? typeof(SHARE_INFO_2) : typeof(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) { SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); if (si.ShareType == ShareType.Special && si.NetName.Length == 2) { } else { shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); //Console.WriteLine(trim(si.NetName) + " no path " + si.ShareType + "\t" + si.Remark); } } else { SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); if (si.ShareType == ShareType.Special && si.NetName.Length == 2 && si.Path.Length == 3) { } else { shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); //Console.Write(trim(si.NetName) + " " + trim(si.Path) + " " + si.ShareType + "\t" + si.Remark + "\n"); } } } } } finally { // Clean up buffer allocated by system if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }