使用Windows API获取系统信息

397 阅读2分钟

使用的API

Windows API说明
GetVersionEx获取系统版本信息
GetSystemInfo获取内存,CPU信息
GetSystemDirectory获取系统目录
GetWindowsDirectory获取Windows目录
GetComputerName获取计算机名称
GetUserName获取用户名
SystemParametersInfo获取系统设备信息

获取系统版本

代码

#pragma warning(disable: 4996)  // 屏蔽GetVersionEx报的警告
#include <Windows.h>
#include <stdio.h>

// 显示Windows版本信息
void ShowVersionInfo()
{
    OSVERSIONINFO ovex;  // 保存系统版本信息
    ovex.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);  // 设置结构的大小
    if (!GetVersionEx(&ovex))  // 获取系统版本
    {
        printf("error:  %d\n", GetLastError());
        return;
    }

    // windows主版本号
    printf("major version: %d\n", ovex.dwMajorVersion);

    // windows次版本号
    printf("minor version: %d\n", ovex.dwMinorVersion);

    // windows 构建号
    printf("build  number: %d\n", ovex.dwBuildNumber);

    //操作系统的服务包(Service Pack)版本号或更新补丁(Update)版本号。

    printf("CSD  number : %d\n", ovex.szCSDVersion);

}

测试效果

image.png

获取系统硬件信息

代码

// 显示CPU, 内存系统信息
void ShowSystemInfo()
{
    SYSTEM_INFO si;

    GetSystemInfo(&si);

    printf("内存页的大小:0x%.8X, 可用内存起始地址:0x%.8X, 可用内存结束地址:0x%.8X\n", si.dwPageSize, si.lpMinimumApplicationAddress, si.lpMaximumApplicationAddress);
    printf("处理器个数:%d\n", si.dwNumberOfProcessors);
    printf("处理器类型:%d, ", si.dwProcessorType);

    switch (si.dwProcessorType)
    {
    case PROCESSOR_INTEL_386:
        printf("PROCESSOR_INTEL_386\n");
        break;
    case PROCESSOR_INTEL_486:
        printf("PROCESSOR_INTEL_486\n");
        break;
    case PROCESSOR_AMD_X8664:
        printf("PROCESSOR_AMD_X8664\n");
        break;
    case PROCESSOR_INTEL_IA64:
        printf("PROCESSOR_INTEL_IA64\n");
        break;
    }

    printf("处理器架构:");

    switch (si.wProcessorArchitecture)
    {
    case PROCESSOR_ARCHITECTURE_AMD64:
        printf("PROCESSOR_ARCHITECTURE_AMD64\n");
    }
}

测试效果

image.png

获取系统目录

代码

#include <Windows.h>
#include <stdio.h>

int main()
{
    TCHAR szSystemDirectory[MAX_PATH];

    // 获取系统目录
    GetSystemDirectory(szSystemDirectory, MAX_PATH);

    printf("系统目录:\t%ls\n", szSystemDirectory);

    return 0;

}

执行效果

image.png

获取Windows目录

代码

#include <Windows.h>
#include <stdio.h>

int main()
{

    TCHAR szWindowsDirectory[MAX_PATH];

    // 获取Windows目录
    GetWindowsDirectory(szWindowsDirectory, MAX_PATH);

    printf("Windows目录: \t%ls\n", szWindowsDirectory);

    return 0;

}

执行效果

image.png

获取计算机名称

代码

#include <Windows.h>
#include <stdio.h>

int main()
{

    TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwComputerNameLen = MAX_COMPUTERNAME_LENGTH + 1;

    // 获取计算机名称
    GetComputerName(szComputerName, &dwComputerNameLen);

    printf("计算机名称: %ls\n", szComputerName);


    return 0;

}

执行效果

image.png

获取用户名

代码

#include <Windows.h>
#include <stdio.h>

int main()
{

    DWORD dwUserNameLen = 64;
    TCHAR szUserName[64];

    // 获取用户名
    GetUserName(szUserName, &dwUserNameLen);

    printf("用户名:\t%ls\n", szUserName);


    return 0;

}

运行效果

image.png

修改鼠标指针

在我的电脑上测试设置没效果😓

代码

#include <Windows.h>
#include <stdio.h>

int main()
{
    
    BOOL fResult;       // 保存API函数调用的结果

    int aMouseInfo[3];  // 保存获取的鼠标信息

    // 获取鼠标信息
    // SPI_GETMOUSE表示获取鼠标
    fResult = SystemParametersInfo(SPI_GETMOUSE, 0, &aMouseInfo, 0);

    if (fResult)
    {
        aMouseInfo[2] = 2 * aMouseInfo[2];  // 将鼠标速度加倍

        // 设置鼠标速度
        // SPI_SETMOUSE表示设置鼠标
        // SPIF_SENDCHANGE将修改保存起来
        SystemParametersInfo(SPI_SETMOUSE, 0, &aMouseInfo, SPIF_SENDCHANGE);
    }



    return 0;

}