C/C++获取系统IP地址/硬件信息等

#include<stdio.h>
#include<winsock2.h>    //该头文件需在windows.h之前
#include<windows.h>
#include<string>
#include<iostream>
#pragma comment(lib,"ws2_32.lib") 
using namespace std;

void getIP()
{
WSADATA WSAData;
//WSADATA结构被用来储存调用AfxSocketInit全局函数返回的Windows Sockets初始化信息。
if (WSAStartup(MAKEWORD(2, 0),&WSAData)) // 初始化Windows sockets API
{
printf(
WSAStartup failed %s\n, WSAGetLastError());
exit(
-1); //异常退出
}

</span><span style="color: #0000ff;">char</span> hostName[<span style="color: #800080;">256</span><span style="color: #000000;">];
</span><span style="color: #0000ff;">if</span>(gethostname(hostName,<span style="color: #0000ff;">sizeof</span>(hostName)))        <span style="color: #008000;">//</span><span style="color: #008000;">获取主机名</span>

{
printf(
Error: %u\n, WSAGetLastError());
exit(
-1); //异常退出
}
printf(
主机名: %s\n, hostName);

hostent </span>*host=gethostbyname(hostName);    <span style="color: #008000;">//</span><span style="color: #008000;"> 根据主机名获取主机信息. </span>
<span style="color: #0000ff;">if</span>(host==<span style="color: #000000;">NULL)
{
    printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Error: %u\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, WSAGetLastError());
    exit(</span>-<span style="color: #800080;">1</span><span style="color: #000000;">);
}

cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">主机地址类型:        </span><span style="color: #800000;">"</span>&lt;&lt;host-&gt;h_addrtype&lt;&lt;<span style="color: #000000;">endl
    </span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">地址清单:            </span><span style="color: #800000;">"</span>&lt;&lt;host-&gt;h_addr_list&lt;&lt;<span style="color: #000000;">endl
    </span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">别名列表:            </span><span style="color: #800000;">"</span>&lt;&lt;host-&gt;h_aliases&lt;&lt;<span style="color: #000000;">endl
    </span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">地址长度:            </span><span style="color: #800000;">"</span>&lt;&lt;host-&gt;h_length&lt;&lt;<span style="color: #000000;">endl
    </span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">正式的主机名:        </span><span style="color: #800000;">"</span>&lt;&lt;host-&gt;h_name&lt;&lt;<span style="color: #000000;">endl;

</span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;host-&gt;h_addr_list[i]!=<span style="color: #800080;">0</span>;i++<span style="color: #000000;">)
{
    cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">该主机IP</span><span style="color: #800000;">"</span>&lt;&lt;i+<span style="color: #800080;">1</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">:           </span><span style="color: #800000;">"</span>&lt;&lt;inet_ntoa(*(<span style="color: #0000ff;">struct</span> in_addr*)*host-&gt;h_addr_list)&lt;&lt;<span style="color: #000000;">endl;
}
cout</span>&lt;&lt;<span style="color: #800000;">"</span><span style="color: #800000;">-----------------------------------------------</span><span style="color: #800000;">"</span>&lt;&lt;<span style="color: #000000;">endl;
WSACleanup();  

}
void getSysInfo()
{
SYSTEM_INFO sysInfo;
//该结构体包含了当前计算机的信息:计算机的体系结构、中央处理器的类型、系统中中央处理器的数量、页面的大小以及其他信息。
OSVERSIONINFOEX osvi;
GetSystemInfo(
&sysInfo);
osvi.dwOSVersionInfoSize
=sizeof(osvi);
if (GetVersionEx((LPOSVERSIONINFOW)&osvi))
{
printf(
操作系统版本 : %u.%u.%u\n, osvi.dwMajorVersion, osvi.dwMinorVersion,osvi.dwBuildNumber);
printf(
Service Pack : %u.%u\n, osvi.wServicePackMajor, osvi.wServicePackMinor);
}
printf(
处理器架构 : %u\n, sysInfo.wProcessorArchitecture);
printf(
处理器级别 : %u\n, sysInfo.wProcessorLevel);
printf(
处理器版本 : %u\n, sysInfo.wProcessorRevision);
printf(
处理器掩码 : %u\n, sysInfo.dwActiveProcessorMask);
printf(
处理器数量 : %u\n, sysInfo.dwNumberOfProcessors);
printf(
处理器类型 : %u\n, sysInfo.dwProcessorType);
printf(
页面大小 : %u\n, sysInfo.dwPageSize);
printf(
应用程序最小地址 : %u\n, sysInfo.lpMinimumApplicationAddress);
printf(
应用程序最大地址 : %u\n, sysInfo.lpMaximumApplicationAddress);
printf(
虚拟内存分配粒度 : %u\n, sysInfo.dwAllocationGranularity);
printf(
OemId : %u\n, sysInfo.dwOemId);
printf(
wReserved : %u\n, sysInfo.wReserved);
}
int main()
{
getIP();
getSysInfo();
return 0;
}