Example #1
0
        public static string[] GetShares(string server)
        {
            IntPtr buf = new IntPtr(0);

            Int32[] dwEntriesRead = new Int32[1];
            dwEntriesRead[0] = 0;
            Int32[] dwTotalEntries = new Int32[1];
            dwTotalEntries[0] = 0;
            Int32[] dwResumeHandle = new Int32[1];
            dwResumeHandle[0] = 0;
            Int32 success = 0;

            string[] shares = new string[0];


            success = NetShareEnum(server, 0, out buf, -1, dwEntriesRead, dwTotalEntries, dwResumeHandle);
            if (dwEntriesRead[0] > 0)
            {
                SHARE_INFO_0[] s0 = new SHARE_INFO_0[dwEntriesRead[0]];
                shares = new string[dwEntriesRead[0]];
                for (int i = 0; i < dwEntriesRead[0]; i++)
                {
                    s0[i]     = (SHARE_INFO_0)Marshal.PtrToStructure(buf, typeof(SHARE_INFO_0));
                    shares[i] = s0[i].shi0_netname;
                    buf       = (IntPtr)((long)buf + Marshal.SizeOf(s0[0]));
                }
                //NetApiBufferFree((long)buf);
            }
            return(shares);
        }
Example #2
0
        public static string[] GetShares(string server)
        {
            IntPtr buf = new IntPtr(0);
            Int32[] dwEntriesRead = new Int32[1];
            dwEntriesRead[0] = 0;
            Int32[] dwTotalEntries = new Int32[1];
            dwTotalEntries[0] = 0;
            Int32[] dwResumeHandle = new Int32[1];
            dwResumeHandle[0] = 0;
            Int32 success = 0;
            string[] shares = new string[0];

            success = NetShareEnum(server, 0, out buf, -1, dwEntriesRead, dwTotalEntries, dwResumeHandle);
            if (dwEntriesRead[0] > 0)
            {
                SHARE_INFO_0[] s0 = new SHARE_INFO_0[dwEntriesRead[0]];
                shares = new string[dwEntriesRead[0]];
                for (int i = 0; i < dwEntriesRead[0]; i++)
                {
                    s0[i] = (SHARE_INFO_0)Marshal.PtrToStructure(buf, typeof(SHARE_INFO_0));
                    shares[i] = s0[i].shi0_netname;
                    buf = (IntPtr)((long)buf + Marshal.SizeOf(s0[0]));
                }
                //NetApiBufferFree((long)buf);
            }
            return shares;
        }
Example #3
0
        public static SHARE_INFO_0[] GetShareInfos_0(string server_name)
        {
            List <SHARE_INFO_0> ret_list = new List <SHARE_INFO_0>();
            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_0,
                              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_0,
                              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_0.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_0.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());
        }
Example #4
0
 public static extern NET_API_STATUS NetGroupEnum([MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, out SHARE_INFO_0 siPtr, int prefmaxlen, out uint entriesread, out uint totalentries, out int resumeHandle);