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);        //异常退出 
    }  

    char hostName[256];
    if(gethostname(hostName,sizeof(hostName)))        //获取主机名
    {
        printf("Error: %u\n", WSAGetLastError());
        exit(-1);        //异常退出 
    }
    printf("主机名:             %s\n", hostName);

    hostent *host=gethostbyname(hostName);    // 根据主机名获取主机信息. 
    if(host==NULL)
    {
        printf("Error: %u\n", WSAGetLastError());
        exit(-1);
    }

    cout<<"主机地址类型:        "<<host->h_addrtype<<endl
        <<"地址清单:            "<<host->h_addr_list<<endl
        <<"别名列表:            "<<host->h_aliases<<endl
        <<"地址长度:            "<<host->h_length<<endl
        <<"正式的主机名:        "<<host->h_name<<endl;

    for(int i=0;host->h_addr_list[i]!=0;i++)
    {
        cout<<"该主机IP"<<i+1<<":           "<<inet_ntoa(*(struct in_addr*)*host->h_addr_list)<<endl;
    }
    cout<<"-----------------------------------------------"<<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;   
}

报错:

报错1:“printf”: 格式字符串“%s”需要类型“char *”的参数,但可变参数 1 拥有了类型“int”错误

解决办法:printf换成cout

报错2:Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings XXX

解决办法:打开项目属性,编辑预处理器定义,增加 _WINSOCK_DEPRECATED_NO_WARNINGS,如图:

报错3:error C4996: 'GetVersionExW': 被声明为已否决

解决办法:Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉 

修改后代码:

#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
    {
        cout<<"WSAStartup failed "<<WSAGetLastError()<<endl;
        exit(-1);        //异常退出 
    }

    char hostName[256];
    if (gethostname(hostName, sizeof(hostName)))        //获取主机名
    {
        cout<<"Error: "<< WSAGetLastError()<<endl;
        exit(-1);        //异常退出 
    }
    cout<<"主机名:            "<< hostName<<endl;

    hostent* host = gethostbyname(hostName);    // 根据主机名获取主机信息. 
    if (host == NULL)
    {
        cout<<"Error: "<<WSAGetLastError()<<endl;
        exit(-1);
    }

    cout << "主机地址类型:        " << host->h_addrtype << endl
        << "地址清单:            " << host->h_addr_list << endl
        << "别名列表:            " << host->h_aliases << endl
        << "地址长度:            " << host->h_length << endl
        << "正式的主机名:        " << host->h_name << endl;

    for (int i = 0; host->h_addr_list[i] != 0; i++)
    {
        cout << "该主机IP" << i + 1 << ":           " << inet_ntoa(*(struct in_addr*)*host->h_addr_list) << endl;
    }
    cout << "-----------------------------------------------" << endl;
    WSACleanup();
}
void getSysInfo()
{
    SYSTEM_INFO  sysInfo;    //该结构体包含了当前计算机的信息:计算机的体系结构、中央处理器的类型、系统中中央处理器的数量、页面的大小以及其他信息。
    OSVERSIONINFOEX osvi;
    GetSystemInfo(&sysInfo);
    osvi.dwOSVersionInfoSize = sizeof(osvi);
    if (GetVersionEx((LPOSVERSIONINFOW)&osvi))
    {
        cout<<"操作系统版本 :   "<<osvi.dwMajorVersion<<osvi.dwMinorVersion<<osvi.dwBuildNumber<<endl;
        cout << "Service Pack :   "<<osvi.wServicePackMajor<<osvi.wServicePackMinor<<endl;
    }
   
    cout << "处理器架构 :        "<< sysInfo.wProcessorArchitecture<<endl;
    cout << "处理器级别 :        " << sysInfo.wProcessorLevel << endl;
    cout << "处理器版本 :        " << sysInfo.wProcessorRevision << endl;
    cout << "处理器掩码 :        " << sysInfo.dwActiveProcessorMask << endl;
    cout << "处理器数量 :        " << sysInfo.dwNumberOfProcessors << endl;
    cout << "处理器类型 :        " << sysInfo.dwProcessorType << endl;
    cout << "页面大小 :          " << sysInfo.dwPageSize << endl;
    cout << "应用程序最小地址 :  " << sysInfo.lpMinimumApplicationAddress << endl;
    cout << "应用程序最大地址 :  " << sysInfo.lpMaximumApplicationAddress << endl;
    cout << "虚拟内存分配粒度 :  " << sysInfo.dwAllocationGranularity << endl;
    cout << "OemId :             " << sysInfo.dwOemId << endl;
    cout << "wReserved :         " << sysInfo.wReserved << endl;
}
int main()
{
    getIP();
    getSysInfo();
    return 0;
}

 运行结果:

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易小侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值