Esempio n. 1
0
        //methodes
        public static string[] GetComputers()
        {
            ArrayList computers = new ArrayList();
            IntPtr    buffer = IntPtr.Zero, tmpBuffer = IntPtr.Zero;
            int       entriesRead, totalEntries, resHandle;
            int       sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));

            try
            {
                int ret = NetServerEnum(null, LVL_100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));

                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
                        computers.Add(svrInfo.sv100_name);
                    }
                }
                else
                {
                    throw new Win32Exception(ret);
                }
            }
            finally
            {
                NetApiBufferFree(buffer);
            }

            return((string[])computers.ToArray(typeof(string)));
        }
Esempio n. 2
0
        public ArrayList getNetworkComputers()
        {
            ArrayList arrayList      = new ArrayList();
            int       num            = 1;
            int       num2           = 2;
            IntPtr    pBuf           = IntPtr.Zero;
            IntPtr    zero           = IntPtr.Zero;
            int       dwEntriesRead  = 0;
            int       dwTotalEntries = 0;
            int       dwResumeHandle = 0;
            int       num3           = Marshal.SizeOf(typeof(_SERVER_INFO_100));

            try
            {
                if (NetServerEnum(null, 100, ref pBuf, -1, out dwEntriesRead, out dwTotalEntries, num | num2, null, out dwResumeHandle) == 0)
                {
                    for (int i = 0; i < dwTotalEntries; i++)
                    {
                        IntPtr           ptr          = new IntPtr((int)pBuf + i * num3);
                        _SERVER_INFO_100 sERVER_INFO_ = (_SERVER_INFO_100)Marshal.PtrToStructure(ptr, typeof(_SERVER_INFO_100));
                        arrayList.Add(sERVER_INFO_.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with acessing network computers in NetworkBrowser \r\n\r\n\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return(null);
            }
            finally
            {
                NetApiBufferFree(pBuf);
            }
            return(arrayList);
        }
        /// <summary>
        /// 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>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
        /// PC's in the Domain</returns>
        public ArrayList getNetworkComputers()
        {
            //local fields
            ArrayList networkComputers     = new ArrayList();
            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 (C++ term), =0 for C#
                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 ArrayList
                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error in getNetworkComputers: " + ex.Message);
            }
            finally
            {
                //The NetApiBufferFree function frees
                //the memory that the NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer);
            }
            //return entries found
            return(networkComputers);
        }
Esempio n. 4
0
        public ArrayList getNetworkComputers()
        {
            //local fields
            ArrayList networkComputers     = new ArrayList();
            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
            {
                int ret = NetServerEnum(null, 100, ref buffer,
                                        MAX_PREFERRED_LENGTH,
                                        out entriesRead,
                                        out totalEntries, SV_TYPE_WORKSTATION |
                                        SV_TYPE_SERVER, null, out
                                        resHandle);

                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer +
                                               (i * sizeofINFO));

                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
                                                   Marshal.PtrToStructure(tmpBuffer,
                                                                          typeof(_SERVER_INFO_100));

                        //add the PC names to the ArrayList
                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with acessing " +
                                "network computers in NetworkBrowser " +
                                "\r\n\r\n\r\n" + ex.Message,
                                "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(null);
            }
            finally
            {
                //The NetApiBufferFree function frees
                //the memory that the
                //NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer);
            }
            //return entries found
            return(networkComputers);
        }
Esempio n. 5
0
        public static List <string> GetNetworkComputerNames4()
        {
            List <string> networkComputerNames = 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
            {
                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
                //(C++ term), =0 for C#
                if (ret == 0)
                {
                    //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
                    for (int i = 0; i < totalEntries; i++)
                    {
                        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
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
                                                   Marshal.PtrToStructure(tmpBuffer,
                                                                          typeof(_SERVER_INFO_100));

                        //add the Computer name to the List
                        networkComputerNames.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                //The NetApiBufferFree function frees the allocated memory
                NetApiBufferFree(buffer);
            }

            return(networkComputerNames);
        }
Esempio n. 6
0
        public static List <string> GetComputers(bool includeLocalMachine)
        {
            try
            {
                List <string> networkComputers = new List <string>();

                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));

                int ret = NetServerEnum(
                    null,
                    100,
                    ref buffer,
                    MAX_PREFERRED_LENGTH,
                    out entriesRead,
                    out totalEntries,
                    SV_TYPE_WORKSTATION, //  | SV_TYPE_SERVER
                    null,
                    out resHandle);

                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));

                        if (includeLocalMachine ||
                            !svrInfo.sv100_name.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase))
                        {
                            networkComputers.Add(svrInfo.sv100_name);
                        }
                    }
                }

                NetApiBufferFree(buffer);

                if (networkComputers.Count > 0)
                {
                    return(networkComputers);
                }
            }
            catch
            {
            }

            return(null);
        }
Esempio n. 7
0
        public ArrayList getNetworkComputers()
        {
            ArrayList networkComputers     = new ArrayList();
            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
            {
                int ret = NetServerEnum(null, 100, ref buffer,
                                        MAX_PREFERRED_LENGTH,
                                        out entriesRead,
                                        out totalEntries, SV_TYPE_WORKSTATION |
                                        SV_TYPE_SERVER, null, out
                                        resHandle);

                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer +
                                               (i * sizeofINFO));

                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
                                                   Marshal.PtrToStructure(tmpBuffer,
                                                                          typeof(_SERVER_INFO_100));


                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
            finally
            {
                NetApiBufferFree(buffer);
            }

            return(networkComputers);
        }
        /// <summary>
        /// Retrieves a list of domain SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's (thx to Sacha Barber)
        /// </summary>
        /// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's in the Domain</returns>
        private ArrayList GetNetworkComputers()
        {
            //local fields
            ArrayList networkComputers     = new ArrayList();
            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 NetServerEnum
                int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries,
                                        SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
                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 the buffer that received the data
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));

                        // Check if the PC is a MP TV server
                        string hostname = svrInfo.sv100_name.ToLower(CultureInfo.CurrentCulture);
                        if (VerifyHostname(hostname, silent))
                        {
                            networkComputers.Add(hostname);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("GetNetworkComputers: {0}", ex.Message);
                return(new ArrayList());
            }
            finally
            {
                //Free the memory that the NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer);
            }
            return(networkComputers);
        }
Esempio n. 9
0
        public ArrayList BuscarEquiposEnRed()
        {
            ArrayList Buscar = new ArrayList();
            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
            {
                int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle);
                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
                        Buscar.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Al Intentar Acceder A La Red " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
            finally
            {
                NetApiBufferFree(buffer);
            }
            return(Buscar);
        }
Esempio n. 10
0
        public async void GetNetworkComputerNames()
        {
            List <string> networkComputerNames = 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
            {
                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
                //(C++ term), =0 for C#
                if (ret == 0)
                {
                    //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
                    for (int i = 0; i < totalEntries; i++)
                    {
                        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
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
                                                   Marshal.PtrToStructure(tmpBuffer,
                                                                          typeof(_SERVER_INFO_100));

                        //add the Computer name to the List
                        //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! EVENT
                        if (!PotentialServersList.Contains(svrInfo.sv100_name.ToUpper().Trim()))
                        {
                            await Task.Run(() =>
                            {
                                if (!bw.CancellationPending)
                                {
                                    PotentialServersList.Add(svrInfo.sv100_name.ToUpper().Trim());
                                    Log.Instance.W(this, "C++, added" + svrInfo.sv100_name.ToUpper().Trim());
                                    if (PotentialServerFound != null)
                                    {
                                        PotentialServerFound(svrInfo.sv100_name.ToUpper().Trim());
                                    }
                                    //ProcessedServerUpdate(1);
                                }
                            });
                        }


                        networkComputerNames.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                //The NetApiBufferFree function frees the allocated memory
                NetApiBufferFree(buffer);
            }

            //return networkComputerNames;
        }
Esempio n. 11
0
        /// <summary>
        /// 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>Arraylist that represents
        /// all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
        /// PC's in the Domain</returns>
        private List <string> getNetworkComputers(uint sv_flags, string domain)
        {
            //local fields
            var       networkComputers     = new List <string>();
            const int MAX_PREFERRED_LENGTH = -1;
            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 | SV_TYPE_ALL,
                    sv_flags,
                    domain,
                    out resHandle);
                //if the returned with a NERR_Success
                //(C++ term), =0 for C#
                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 ArrayList
                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with accessing " +
                                "network computers in NetworkBrowser " +
                                "\r\n\r\n\r\n" + ex.Message,
                                "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(null);
            }
            finally
            {
                //The NetApiBufferFree function frees the memory that the NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer);
            }
            //return entries found
            return(networkComputers);
        }
Esempio n. 12
0
        private static List <NetworkAddressItems> GetNetworkMachines()
        {
            const int MAX_PREFERRED_LENGTH = -1;
            int       SV_TYPE_ALL          = -1;
            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));

            if (networkAddressItems.Count > 0)
            {
                networkAddressItems.Clear();
            }

            try
            {
                int ret = NetServerEnum(null, 100, ref buffer,
                                        MAX_PREFERRED_LENGTH,
                                        out entriesRead,
                                        out totalEntries,
                                        SV_TYPE_ALL,
                                        null,
                                        out resHandle);
                if (ret == 0)
                {
                    for (int i = 0; i < totalEntries; i++)
                    {
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));

                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));

                        string      computerName = svrInfo.sv100_name;
                        IPAddress[] ipAddresses  = Dns.GetHostAddresses(computerName);
                        if (ipAddresses != null)
                        {
                            IPAddress ip = ipAddresses.FirstOrDefault(c => c.AddressFamily == AddressFamily.InterNetwork);
                            networkAddressItems.Add(new NetworkAddressItems(computerName, ip != null ? ip.ToString() : "", CHECK_PORTS));
                        }
                    }
                }
                else
                {
                    switch (ret)
                    {
                    case 6118:
                    {
                        Logger.WriteErrorMessage("Not found machines in your network");
                        break;
                    }

                    default:
                    {
                        Logger.WriteErrorMessage($"Error code : {ret}");
                        break;
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteErrorLog(ex);
            }
            finally
            {
                NetApiBufferFree(buffer);
            }

            return(networkAddressItems);
        }