您的位置:首页 > 其它

获取本机IP和mac地址

2010-07-08 08:31 134 查看
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=Net::Dns::GetHostName();//计算机名称
String ^str;
str=Net::Dns::GetHostName();

array<IPAddress^>^ ips;
ips = Dns::GetHostAddresses(str);
textBox2->Text=ips[0]->ToString();//获取ip
IPHostEntry^ host = Dns::GetHostEntry(str);
this->textBox3->Text=host->AddressList[0]->ToString();
readmac(textBox2->Text->ToString());
String ^strHostIP="";
IPHostEntry ^oIPHost=Dns::Resolve(Environment::MachineName); //获取IP

strHostIP=oIPHost->AddressList[0]->ToString();
this->textBox5->Text=strHostIP;

fetwwip();

}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
getmach();
}
public:

void readmac(String ^ ip)//获取mac
{

String ^mac = "";
System::Diagnostics::Process ^p=gcnew System::Diagnostics::Process();

p->StartInfo->FileName = "nbtstat";
p->StartInfo->Arguments = "-a " + ip;
p->StartInfo->UseShellExecute = false;
p->StartInfo->CreateNoWindow = true;
p->StartInfo->RedirectStandardOutput = true;
p->Start();

String ^output =p->StandardOutput->ReadToEnd();
int len = output->IndexOf("MAC Address = ");
if(len>0)
{
mac = output->Substring(len + 14, 17);
}
p->WaitForExit();
this->textBox4->Text=mac;

}

void fetwwip(void)//获取外网IP
{
System::Net::WebClient ^wc=gcnew WebClient();
textBox6->Text =wc->DownloadString("http://www.zu14.cn/ip")->ToString();
}

void getmach(void)//获取mac,这种方法需要添加management引用
{ String ^stringMAC;
String ^stringIP;

ManagementClass ^moc=gcnew ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection ^mc=moc->GetInstances();

for each (ManagementObject ^mcs in mc )
{
if (Convert::ToBoolean(mcs["ipEnabled"]) == true)
{

stringMAC += mcs["MACAddress"]->ToString();
textBox12->Text=stringMAC->ToString();

}
//throw(gcnew System::NotImplementedException);
}
}



由于这里不可以添加源代码。所以源代码传到了csdn。大家也可以加我交流学习啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: