vb .net 中如何获得客户端的机器名,IP地址,网卡mac地址.

jinghujing 2003-11-22 10:45:22
如标题,怎么实现,最好有一个具体例子.
...全文
2085 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
码视野 2003-11-22
  • 打赏
  • 举报
回复
呵呵,楼上的牛!!
wincarf 2003-11-22
  • 打赏
  • 举报
回复
c#获mac,获ip地址应该有函数
public enum NCBCONST
{
NCBNAMSZ =16, /* absolute length of a net name */
MAX_LANA =254, /* lana's in range 0 to MAX_LANA inclusive */
NCBENUM =0x37, /* NCB ENUMERATE LANA NUMBERS */
NRC_GOODRET =0x00, /* good return */
NCBRESET =0x32, /* NCB RESET */
NCBASTAT =0x33, /* NCB ADAPTER STATUS */
NUM_NAMEBUF =30, /* Number of NAME's BUFFER */
}

[StructLayout(LayoutKind.Sequential)]
public struct ADAPTER_STATUS
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public byte[] adapter_address;
public byte rev_major;
public byte reserved0;
public byte adapter_type;
public byte rev_minor;
public ushort duration;
public ushort frmr_recv;
public ushort frmr_xmit;
public ushort iframe_recv_err;
public ushort xmit_aborts;
public uint xmit_success;
public uint recv_success;
public ushort iframe_xmit_err;
public ushort recv_buff_unavail;
public ushort t1_timeouts;
public ushort ti_timeouts;
public uint reserved1;
public ushort free_ncbs;
public ushort max_cfg_ncbs;
public ushort max_ncbs;
public ushort xmit_buf_unavail;
public ushort max_dgram_size;
public ushort pending_sess;
public ushort max_cfg_sess;
public ushort max_sess;
public ushort max_sess_pkt_size;
public ushort name_count;
}

[StructLayout(LayoutKind.Sequential)]
public struct NAME_BUFFER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] name;
public byte name_num;
public byte name_flags;
}

[StructLayout(LayoutKind.Sequential)]
public struct NCB
{
public byte ncb_command;
public byte ncb_retcode;
public byte ncb_lsn;
public byte ncb_num;
public IntPtr ncb_buffer;
public ushort ncb_length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] ncb_callname;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NCBNAMSZ)]
public byte[] ncb_name;
public byte ncb_rto;
public byte ncb_sto;
public IntPtr ncb_post;
public byte ncb_lana_num;
public byte ncb_cmd_cplt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public byte[] ncb_reserve;
public IntPtr ncb_event;
}

[StructLayout(LayoutKind.Sequential)]
public struct LANA_ENUM
{
public byte length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.MAX_LANA)]
public byte[] lana;
}

[StructLayout(LayoutKind.Auto)]
public struct ASTAT
{
public ADAPTER_STATUS adapt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NUM_NAMEBUF)]
public NAME_BUFFER[] NameBuff;
}
public class Win32API
{
[DllImport("NETAPI32.DLL")]
public static extern char Netbios(ref NCB ncb);
}
[/code]

2、
[code]
public string GetMacAddress()
{
string addr="";
int cb;
ASTAT adapter;
NCB Ncb=new NCB();
char uRetCode;
LANA_ENUM lenum;

Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
cb = Marshal.SizeOf(typeof(LANA_ENUM));
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
Marshal.FreeHGlobal(Ncb.ncb_buffer);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return "";

for(int i=0; i < lenum.length ;i++)
{
Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Win32API.Netbios(ref Ncb);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return "";

Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
Ncb.ncb_callname[0]=(byte)'*';
cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER))*(int)NCBCONST.NUM_NAMEBUF;
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
Marshal.FreeHGlobal(Ncb.ncb_buffer);

if (uRetCode == (short)NCBCONST.NRC_GOODRET)
{
if(i>0)
addr += ":";
addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
adapter.adapt.adapter_address[0],
adapter.adapt.adapter_address[1],
adapter.adapt.adapter_address[2],
adapter.adapt.adapter_address[3],
adapter.adapt.adapter_address[4],
adapter.adapt.adapter_address[5]);
}
}
return addr.Replace(' ', '0');
}
menuvb 2003-11-22
  • 打赏
  • 举报
回复
对方IP:
Dim en As IPEndPoint
en = socket.RemoteEndPoint
MsgBox(en.ToString)

rock29 2003-11-22
  • 打赏
  • 举报
回复
ComputerName = System.Net.Dns.GetHostName().ToString
获得客户端的机器名
terryxin 2003-11-22
  • 打赏
  • 举报
回复
我知道有个例子,有时间找你给
yuzaichun 2003-11-22
  • 打赏
  • 举报
回复
Dim mc As New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances
Dim mo As Management.ManagementObject

'先得到网卡数目
Dim i As Integer = 0

For Each mo In moc

If CBool(mo("IPEnabled")) = True Then

i += 1

End If

mo.Dispose()

Next

'赋值给数组

Dim mc_2 As New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc_2 As Management.ManagementObjectCollection = mc_2.GetInstances()
Dim array(i) As String, delcolon As String

Dim j As Integer = 0

For Each mo In moc_2

If CBool(mo("IPEnabled")) = True Then

delcolon = mo("MacAddress").ToString()
delcolon = delcolon.Replace(":", "")
array(j) = delcolon
j += 1

End If

mo.Dispose()

Next

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧