예제 #1
0
        /// <summary>
        /// Enumerates computers on the local network.
        /// </summary>
        public static string[] EnumComputers()
        {
            IntPtr _pInfo;
            int    _entriesRead  = 0;
            int    _totalEntries = 0;
            int    result        = NetServerEnum(
                IntPtr.Zero,
                LEVEL_SERVER_INFO_100,
                out _pInfo,
                MAX_PREFERRED_LENGTH,
                out _entriesRead,
                out _totalEntries,
                SV_TYPE_WORKSTATION | SV_TYPE_SERVER,
                IntPtr.Zero,
                IntPtr.Zero);

            if (result != 0)
            {
                throw new ApplicationException("NetApi Error = " + String.Format("0x{0:X8}", result));
            }
            string[] computers = new string[_entriesRead];
            IntPtr   pos       = _pInfo;

            for (int ii = 0; ii < _entriesRead; ii++)
            {
                SERVER_INFO_100 info = (SERVER_INFO_100)Marshal.PtrToStructure(pos, typeof(SERVER_INFO_100));
                computers[ii] = info.sv100_name;
                pos           = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(SERVER_INFO_100)));
            }
            NetApiBufferFree(_pInfo);
            return(computers);
        }
        /// <summary>
        /// Devuelve una lista de Nombres de dominio de las PCs de la red local
        /// que sean del tipo SV_TYPE_WORKSTATION y SV_TYPE_SERVER
        ///
        /// Uses the DllImport : NetServerEnum with all its required parameters
        /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
        /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
        /// </summary>
        /// <returns>Lista de Domains encontrados</returns>
        public List <string> getNetworkComputers()
        {
            //local fields
            List <string> networkComputers     = new List <string>();
            const int     MAX_PREFERRED_LENGTH = -1;
            int           SV_TYPE_WORKSTATION  = 1;
            int           SV_TYPE_SERVER       = 2;
            IntPtr        buffer       = IntPtr.Zero;
            IntPtr        tmpBuffer    = IntPtr.Zero;
            int           entriesRead  = 0;
            int           totalEntries = 0;
            int           resHandle    = 0;
            int           sizeofINFO   = Marshal.SizeOf(typeof(SERVER_INFO_100));

            try
            {
                //call the DllImport : NetServerEnum  with all its required parameters
                //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
                //for full details of method signature
                int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead,
                                        out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);

                //if the returned with a NERR_Success
                if (ret == 0)
                {
                    //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
                    for (int i = 0; i < totalEntries; i++)
                    {
                        //get pointer to, Pointer to the buffer that received the data from the call to NetServerEnum.
                        //Must ensure to use correct size of STRUCTURE to ensure correct location in memory is pointed to
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));

                        //Have now got a pointer to the list of SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's, which is unmanaged memory.
                        //Needs to Marshal data from an unmanaged block of memory to a managed object, again using
                        //STRUCTURE to ensure the correct data is marshalled
                        SERVER_INFO_100 svrInfo = (SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(SERVER_INFO_100));

                        //add the PC names to the list
                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Problem with acessing network computers in NetworkBrowser.", ex);
            }
            finally
            {
                //The NetApiBufferFree function frees
                //the memory that the
                //NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer);
            }

            //return entries found
            return(networkComputers);
        }
예제 #3
0
        public static void Main(string[] args)
        {
            IntPtr        resume_handle = IntPtr.Zero;
            IntPtr        ptr           = IntPtr.Zero;
            int           totalentries  = 0;
            int           entriesread   = 0;
            const int     prefmaxlen    = 65535;
            List <string> dcs           = new List <string>();

            if (args.Length < 2)
            {
                Console.WriteLine("Need more args: FastDomainDump [domain|server] DOMAIN/SERVER");
                return;
            }

            if (args[0] == "domain")
            {
                int status = NetServerEnum(0, 100, out ptr, prefmaxlen, ref entriesread, ref totalentries, SV_TYPES.SV_TYPE_DOMAIN_CTRL | SV_TYPES.SV_TYPE_DOMAIN_BAKCTRL, args[1], resume_handle);

                if (status != 0)
                {
                    Console.WriteLine("NetServerEnum failed.");
                    return;
                }

                for (int i = 0; i < entriesread; i++)
                {
                    // cast pointer to a SERVER_INFO_101 structure
                    SERVER_INFO_100 server = (SERVER_INFO_100)Marshal.PtrToStructure(ptr, typeof(SERVER_INFO_100));
                    Console.WriteLine("Adding server to check: " + server.Name);
                    dcs.Add(server.Name);
                    //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems.
                    ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server));
                }

                foreach (string dc in dcs)
                {
                    Console.WriteLine("Dumping Server: " + dc);
                    DumpServer(dc);
                }
            }
            else if (args[0] == "server")
            {
                DumpServer(args[1]);
            }

            Console.ReadKey(true);
        }
예제 #4
0
        public static SERVER_INFO_100 GetServerInfo_100(string server_name)
        {
            IntPtr          buffer = IntPtr.Zero;
            SERVER_INFO_100 ret    = new SERVER_INFO_100();

            try
            {
                int res = WinApiNET.NetServerGetInfo(server_name, NetserverInfoLevel.INFO_100, ref buffer);
                if (res != WinApiNET.NERR_Success)
                {
                    throw new Win32Exception(res);
                }
                ret = (SERVER_INFO_100)Marshal.PtrToStructure(buffer, typeof(SERVER_INFO_100));
            }//end of try
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    WinApiNET.NetApiBufferFree(buffer);
                }
            }

            return(ret);
        }
        /// <summary>
        /// Enumerates computers on the local network.
        /// </summary>
        private void OnEnumerate(object state)
        {
            IntPtr pInfo;

            int entriesRead  = 0;
            int totalEntries = 0;
            int resumeHandle = 0;
            int result       = ERROR_MORE_DATA;

            GCHandle dwResumeHandle = GCHandle.Alloc(resumeHandle, GCHandleType.Pinned);

            try
            {
                while (m_stopped == 0 && result == ERROR_MORE_DATA)
                {
                    // enumerate the first batch of servers.
                    result = NetServerEnum(
                        IntPtr.Zero,
                        LEVEL_SERVER_INFO_100,
                        out pInfo,
                        MAX_PREFERRED_LENGTH,
                        out entriesRead,
                        out totalEntries,
                        SV_TYPE_WORKSTATION | SV_TYPE_SERVER,
                        m_domain,
                        dwResumeHandle.AddrOfPinnedObject());

                    // check for fatal error.
                    if ((result != NERR_Success) && (result != ERROR_MORE_DATA))
                    {
                        Utils.Trace("Could not enumerate hosts on network. Error = {0}", result);
                        return;
                    }

                    // copy host names from the returned structures.
                    string[] hostnames = new string[entriesRead];

                    IntPtr pos = pInfo;

                    for (int ii = 0; ii < entriesRead; ii++)
                    {
                        SERVER_INFO_100 info = (SERVER_INFO_100)Marshal.PtrToStructure <SERVER_INFO_100>(pos);

                        hostnames[ii] = info.sv100_name;

                        pos = (IntPtr)(pos.ToInt64() + Marshal.SizeOf <SERVER_INFO_100>());
                    }

                    NetApiBufferFree(pInfo);

                    // raise an event.
                    if (m_stopped == 0 && m_HostsDiscovered != null)
                    {
                        try
                        {
                            m_HostsDiscovered(this, new HostEnumeratorEventArgs(hostnames));
                        }
                        catch (Exception e)
                        {
                            Utils.Trace(e, "Unexpected exception raising HostsDiscovered event.");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected exception calling NetServerEnum.");
            }
            finally
            {
                if (dwResumeHandle.IsAllocated)
                {
                    dwResumeHandle.Free();
                }
            }
        }