如何获得IP地址.工作组上连接的计算机数量 [英] how to get ip addr. of computers connected on workgroup

查看:177
本文介绍了如何获得IP地址.工作组上连接的计算机数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在用c#为我的大学创建一个应用程序,该应用程序列出工作组"MCA"上连接的计算机的所有ip地址.

我该如何在C#中做到这一点?有任何可用的库吗?

然后单击特定的IP地址(或PC名称)我怎么
在服务器上获取该计算机的硬件详细信息
?

预先谢谢你.

-ripal

hello friends,

i am creating an application for my college in c# that listout all the ip-address of computers connected on workgroup "MCA".

how can i do that in c# ? is there any library available ??

and after clicking on particular ip-address(or pc-name) how can i
get the hardware details of that computer
on the Server ??

thank you in advance.

-ripal

推荐答案

using System;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
namespace TcNetSendPlus
{
    /// <summary>
    /// Summary description for ClsNetApiSend.
    /// </summary>
    public class ClsNetApiSend
    {
        // External api declaration
        [DllImport("NetApi32")] public static extern int
            NetMessageBufferSend(uint ServerName,uint MsgName, uint FromName,uint Buffer,ulong BufferLen);
        // Local computer name
        private string ThisComputer;
        public string lstrError;
        public ClsNetApiSend()
        {
            // Store local computer name for further processes
            ThisComputer = Dns.GetHostName();
        }
        // Converting the address of an string variable to integer format
        unsafe public static uint GetAddress(string variable)
        {
            // stores the address of string variable
            uint ptrptr;
            // first convert it to a character pointer
            fixed(char* ptr = variable)
            {ptrptr = (uint) ptr;} // and then cast it to an integer
            return ptrptr; // return converted value
        }
        public bool Send(string ServerName, string Message )
        {
            // holds the length of message
            ulong LenMsg;
            // holds send function return status
            int Status;
            // holds the addresses of local computer name, destination computer name
            // and message that is to be sent
            uint ThisPtr, DestPtr, MsgPtr;
            // holds the name of the destination computer, in case of passing
            // the ip address of the destination computer instead of name
            string newServerName = "";
            try
            {
                IPHostEntry ipEntry = new IPHostEntry();
                // Because NetApi functions require parameters in unicode
                // so we need to multiply by 2 the calculated length of the message
                // to get the correct length of the message. The Length member of
                // string class holds the number of the charcters in the string only
                // but unicode charcters are represented by 2 bytes, one for ascii
                // value and the other contains null value.
                LenMsg = (ulong) Message.Length * 2;
                ThisPtr = GetAddress(ThisComputer);
                MsgPtr = GetAddress(Message);
                ipEntry = Dns.Resolve(ServerName);
                newServerName = ipEntry.HostName;
                DestPtr = GetAddress(newServerName);
            }
            catch (System.Exception e)
            {
                this.lstrError = e.Message;
                return false;
            }
            // NetApi32 api function call for message sending
            Status = NetMessageBufferSend(ThisPtr,DestPtr,ThisPtr,MsgPtr,LenMsg);
            // if the returned value is 0 then there is no error
            // otherwise the return value holds the error code.
            if( Status > 0 )
                return false;
            else
                return true;
        }
    }
}



http://www.codeproject.com/KB/applications/NetSendPlus.aspx [ ^ ]



http://www.codeproject.com/KB/applications/NetSendPlus.aspx[^]


这篇关于如何获得IP地址.工作组上连接的计算机数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆