Exemplo n.º 1
0
        /// <summary>
        ///     Lists all connections made to a shared resource on the server or all connections established from a particular
        ///     computer. If there is more than one user using this connection, then it is possible to get more than one structure
        ///     for the same connection, but with a different user name.
        /// </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>
        /// <param name="shareNetName">
        ///     Pointer to a string that specifies a share name or computer name for the connections of
        ///     interest. If it is a share name, then all the connections made to that share name are listed. If it is a computer
        ///     name (for example, it starts with two backslash characters), then NetConnectionEnum lists all connections made from
        ///     that computer to the server specified. This string is Unicode if _WIN32_WINNT or FORCE_UNICODE is defined.
        /// </param>
        /// <returns>A IEnumerable of Managed ConnectionInfo1 Objects.</returns>
        public static IEnumerable <ConnectionInfo1> GetConnections(string server, string shareNetName)
        {
            var  list         = new List <ConnectionInfo1>();
            uint entriesRead  = 0;
            uint totalEntries = 0;
            uint resumeHandle = 0;
            var  pBuffer      = IntPtr.Zero;

            var status = Internal.Native.NetworkShareManagementFunctions.NetConnectionEnum.DllImports.NetConnectionEnum(Marshal.StringToCoTaskMemAuto(server), Marshal.StringToCoTaskMemAuto(shareNetName), 1, ref pBuffer, 0xFFFFFFFF, ref entriesRead, ref totalEntries, ref resumeHandle);

            if (status == 0 & entriesRead > 0)
            {
                var connectionInfo1Type = typeof(Structs.ConnectionInfo1);
                var offset = Marshal.SizeOf(connectionInfo1Type);
                for (int i = 0, item = pBuffer.ToInt32(); i < entriesRead; i++, item += offset)
                {
                    var pItem           = new IntPtr(item);
                    var connectionInfo1 = ConnectionInfo1.MapToConnectionInfo1((Structs.ConnectionInfo1)Marshal.PtrToStructure(pItem, connectionInfo1Type));
                    list.Add(connectionInfo1);
                }
            }
            NetApiBufferFree.FreeBuffer(pBuffer);
            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Retrieves information about each shared resource on a server.You can also use the WNetEnumResource function to
        ///     retrieve resource information. However, WNetEnumResource does not enumerate hidden shares or users connected to a
        ///     share.
        /// </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>
        /// <returns>A IEnumerable of managed ShareInfo2 Objects.</returns>
        public static IEnumerable <ShareInfo2> GetShares(string server)
        {
            var    list = new List <ShareInfo2>();
            int    entriesRead;
            int    totalEntries;
            var    resumeHandle = 0;
            IntPtr pBuffer;
            var    status = Internal.Native.NetworkShareManagementFunctions.NetShareEnum.DllImports.NetShareEnum(server, 2, out pBuffer, -1, out entriesRead, out totalEntries, ref resumeHandle);

            if (status == 0 & entriesRead > 0)
            {
                var shareinfoType = typeof(Structs.ShareInfo2);
                var offset        = Marshal.SizeOf(shareinfoType);
                for (int i = 0, item = pBuffer.ToInt32(); i < entriesRead; i++, item += offset)
                {
                    var pItem              = new IntPtr(item);
                    var shareInfo2         = (Structs.ShareInfo2)Marshal.PtrToStructure(pItem, shareinfoType);
                    var netShareInfoResult = ShareInfo2.MapToSessionInfo502(shareInfo2);
                    list.Add(netShareInfoResult);
                }
            }
            NetApiBufferFree.FreeBuffer(pBuffer);
            return(list);
        }