/// <summary> /// Retrieves operating statistics for a service. Currently, only the workstation and server services are supported. /// </summary> /// <param name="serverName"> /// Pointer to a string that specifies the DNS or NetBIOS name of the server on which the function /// is to execute. If this parameter is NULL, the local computer is used. /// </param> /// <param name="netName"> /// Pointer to a string that specifies the name of the service about which to get the statistics. /// Only the values SERVICE_SERVER and SERVICE_WORKSTATION are currently allowed. /// </param> /// <returns> /// If the function succeeds, the return value is NERR_Success. If the function fails, the return value is a /// system error code. For a list of error codes, see System Error Codes. /// </returns> public static Statworkstation0 GetStatistics(string serverName, string netName) { var returnValue = new Statworkstation0(); IntPtr pBuffer; var status = DllImports.NetShareGetInfo(serverName, netName, 503, out pBuffer); if (status == 0) { var shareinfoType = typeof(Structs.StatWorkstation0); var pItem = new IntPtr(pBuffer.ToInt32()); var statworkstation0 = (Structs.StatWorkstation0)Marshal.PtrToStructure(pItem, shareinfoType); returnValue = Statworkstation0.MapToStatworkstation0(statworkstation0); ApiBuffer.FreeBuffer(pBuffer); } return(returnValue); }
/// <summary> /// Retrieves information about a session established between a particular server and workstation. Descriptions taken /// from https://msdn.microsoft.com/en-us/library/windows/desktop/bb525383(v=vs.85).aspx (NetSessionGetInfo function). /// </summary> /// <param name="servername"> /// Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the /// function is to execute. If this parameter is NULL, the local computer is used. /// </param> /// <param name="clientame"> /// Pointer to a string that specifies the name of the computer session for which information is to /// be returned. This parameter is required and cannot be NULL. For more information, see NetSessionEnum. /// </param> /// <param name="username"> /// Pointer to a string that specifies the name of the user whose session information is to be /// returned. This parameter is required and cannot be NULL. /// </param> /// <returns></returns> public static SessionInfo2 GetSession(string servername, string clientame, string username) { var returnValue = new SessionInfo2(); IntPtr pBuffer; var status = DllImports.NetSessionGetInfo(servername, clientame, username, 3, out pBuffer); if (status == 0) { var shareinfoType = typeof(Structs.SessionInfo2); var pItem = new IntPtr(pBuffer.ToInt32()); var sessionInfo2 = (Structs.SessionInfo2)Marshal.PtrToStructure(pItem, shareinfoType); returnValue = SessionInfo2.MapToSessionInfo2(sessionInfo2); ApiBuffer.FreeBuffer(pBuffer); } return(returnValue); }
/// <summary> /// Retrieves information about a particular shared resource on a server. /// </summary> /// <param name="serverName"> /// Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the /// function is to execute. If this parameter is NULL, the local computer is used. /// </param> /// <param name="netName">Pointer to a string that specifies the name of the share for which to return information.</param> /// <returns>A managed ShareInfo503 Object.</returns> public static ShareInfo503 GetInfo(string serverName, string netName) { var returnValue = new ShareInfo503(); IntPtr pBuffer; var status = NetworkShareManagementFunctions.NetShareGetInfo.DllImports.NetShareGetInfo(serverName, netName, 503, out pBuffer); if (status == 0) { var shareinfoType = typeof(Structs.FileInfo3); var pItem = new IntPtr(pBuffer.ToInt32()); var shareInfo503 = (Structs.ShareInfo503)Marshal.PtrToStructure(pItem, shareinfoType); returnValue = ShareInfo503.MapToShareInfo503(shareInfo503); ApiBuffer.FreeBuffer(pBuffer); } return(returnValue); }
/// <summary> /// Retrieves information about a particular opening of a server resource. Descriptions taken from /// https://msdn.microsoft.com/en-us/library/windows/desktop/bb525379(v=vs.85).aspx (NetFileGetInfo function). /// </summary> /// <param name="servername"> /// Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the /// function is to execute. If this parameter is NULL, the local computer is used. This string is Unicode if /// _WIN32_WINNT or FORCE_UNICODE is defined. /// </param> /// <param name="fileid"> /// Specifies the file identifier of the open resource for which to return information. The value of /// this parameter must have been returned in a previous enumeration call. For more information, see the following /// Remarks section. /// </param> /// <returns></returns> public static FileInfo3 GetFileInfo(string servername, int fileid) { var returnValue = new FileInfo3(); IntPtr pBuffer; var status = Internal.Native.NetworkShareManagementFunctions.NetFileGetInfo.DllImports.NetFileGetInfo(servername, fileid, 3, out pBuffer); if (status == 0) { var shareinfoType = typeof(Structs.FileInfo3); var pItem = new IntPtr(pBuffer.ToInt32()); var fileInfo3 = (Structs.FileInfo3)Marshal.PtrToStructure(pItem, shareinfoType); returnValue = FileInfo3.MapToFileInfo3(fileInfo3); ApiBuffer.FreeBuffer(pBuffer); } return(returnValue); }
/// <summary> /// Provides information about sessions established on a server. /// </summary> /// <param name="server"> /// Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the /// function is to execute. If this parameter is NULL, the local computer is used. /// </param> /// <param name="clientName"> /// Pointer to a string that specifies the name of the computer session for which information is /// to be returned. If this parameter is NULL, NetSessionEnum returns information for all computer sessions on the /// server. /// </param> /// <param name="userName"> /// Pointer to a string that specifies the name of the user for which information is to be returned. /// If this parameter is NULL, NetSessionEnum returns information for all users. /// </param> /// <returns>A Ienumerable of managed SessionInfo502 Objects.</returns> public static IEnumerable <SessionInfo502> GetSessions(string server, string clientName, string userName) { var list = new List <SessionInfo502>(); int entriesRead; int totalEntries; var resumeHandle = 0; IntPtr pBuffer; var status = Internal.Native.NetworkShareManagementFunctions.NetSessionEnum.DllImports.NetSessionEnum(server, null, null, 502, out pBuffer, -1, out entriesRead, out totalEntries, ref resumeHandle); if (status == 0 & entriesRead > 0) { var shareinfoType = typeof(Structs.SessionInfo502); var offset = Marshal.SizeOf(shareinfoType); for (int i = 0, item = pBuffer.ToInt32(); i < entriesRead; i++, item += offset) { var pItem = new IntPtr(item); var sessionInfo502 = (Structs.SessionInfo502)Marshal.PtrToStructure(pItem, shareinfoType); var netSessionEnumResult = SessionInfo502.MapToSessionInfo502(sessionInfo502); list.Add(netSessionEnumResult); } } ApiBuffer.FreeBuffer(pBuffer); return(list); }
/// <summary> /// Returns information about some or all open files on a server, depending on the parameters specified. /// </summary> /// <param name="server"> /// Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the /// function is to execute. If this parameter is NULL, the local computer is used. This string is Unicode if /// _WIN32_WINNT or FORCE_UNICODE is defined. /// </param> /// <returns>A IEnumerable of Managed FileInfo3 Objects.</returns> public static IEnumerable <FileInfo3> GetNetFileList(string server) { var list = new List <FileInfo3>(); int entriesRead; int totalEntries; var resumeHandle = 0; var pBuffer = IntPtr.Zero; var status = DllImports.NetFileEnum(server, null, null, 3, ref pBuffer, -1, out entriesRead, out totalEntries, ref resumeHandle); if (status == 0 & entriesRead > 0) { var shareinfoType = typeof(Structs.FileInfo3); var offset = Marshal.SizeOf(shareinfoType); for (int i = 0, item = pBuffer.ToInt32(); i < entriesRead; i++, item += offset) { var pItem = new IntPtr(item); var fileInfo3 = (Structs.FileInfo3)Marshal.PtrToStructure(pItem, shareinfoType); var nEtFileEnumResult = FileInfo3.MapToFileInfo3(fileInfo3); list.Add(nEtFileEnumResult); } ApiBuffer.FreeBuffer(pBuffer); } return(list); }