当前位置 : 主页 > 编程语言 > c语言 >

windows c++获取获取物理磁盘的缓存、机械磁盘的转速、逻辑磁盘的Inode可用和inode使用率

来源:互联网 收集:自由互联 发布时间:2023-08-28
要获取物理磁盘的缓存,您可以使用Windows API函数GetSystemFileCacheSize()和GetSystemFileCacheSizeEx()。以下是一个示例代码: #include iostream#include windows.hint main(){ DWORD cacheSize; DWORD cacheSizePeak; if

要获取物理磁盘的缓存,您可以使用Windows API函数GetSystemFileCacheSize()和GetSystemFileCacheSizeEx()。以下是一个示例代码:

#include <iostream>
#include <windows.h>

int main()
{
    DWORD cacheSize;
    DWORD cacheSizePeak;
    if (GetSystemFileCacheSize(&cacheSize, &cacheSizePeak, NULL))
    {
        std::cout << "Physical disk cache size: " << cacheSize << " bytes\n";
    }
    else
    {
        std::cout << "Failed to get physical disk cache size\n";
    }
    return 0;
}

要获取机械磁盘的转速,您可以使用Windows WMI(Windows管理信息) API。以下是一个示例代码:

#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>

#pragma comment(lib, "wbemuuid.lib")

int main()
{
    HRESULT hres;

    // Initialize COM
    hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        std::cout << "Failed to initialize COM library\n";
        return 1;
    }

    // Connect to WMI namespace
    IWbemLocator* pLoc = NULL;
    hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
    if (FAILED(hres))
    {
        std::cout << "Failed to create IWbemLocator object\n";
        CoUninitialize();
        return 1;
    }

    IWbemServices* pSvc = NULL;
    hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
    if (FAILED(hres))
    {
        std::cout << "Failed to connect to IWbemServices\n";
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // Set security level on proxy
    hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
    if (FAILED(hres))
    {
        std::cout << "Failed to set security level on IWbemServices proxy\n";
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // Query Win32_DiskDrive class
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(_bstr_t("WQL"), _bstr_t("SELECT * FROM Win32_DiskDrive"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
    if (FAILED(hres))
    {
        std::cout << "Failed to execute Win32_DiskDrive query\n";
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;
    }

    // Get properties of each disk drive
    IWbemClassObject* pObj = NULL;
    ULONG uReturn = 0;
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pObj, &uReturn);
        if (0 == uReturn)
        {
            break;
        }

        VARIANT var;
        hr = pObj->Get(L"MediaType", 0, &var, 0, 0);
        if (SUCCEEDED(hr) && var.vt == VT_BSTR && wcscmp(var.bstrVal, L"HDD") == 0)
        {
            hr = pObj->Get(L"RotationalSpeed", 0, &var, 0, 0);
            if (SUCCEEDED(hr) && var.vt == VT_I4)
            {
                std::cout << "Mechanical disk rotational speed: " << var.intVal << " RPM\n";
            }
            else
            {
                std::cout << "Failed to get mechanical disk rotational speed\n";
            }
        }

        VariantClear(&var);
        pObj->Release();
    }

    // Cleanup
    pEnumerator->Release();
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();

    return 0;
}

要获取逻辑磁盘的Inode可用和Inode使用率,您可以使用Windows API函数GetDiskFreeSpaceEx()。以下是一个示例代码:

#include <iostream>
#include <windows.h>

int main()
{
    ULARGE_INTEGER freeBytesAvailable;
    ULARGE_INTEGER totalNumberOfBytes;
    ULARGE_INTEGER totalNumberOfFreeBytes;
    if (GetDiskFreeSpaceEx(L"C:", &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes))
    {
        std::cout << "Inodes available: " << freeBytesAvailable.QuadPart / 4096 << '\n';
        std::cout << "Inode usage: " << (1.0 - (double)totalNumberOfFreeBytes.QuadPart / totalNumberOfBytes.QuadPart) * 100 << "%\n";
    }
    else
    {
        std::cout << "Failed to get inode usage\n";
    }
    return 0;
}

请注意,这只是一个示例,并且在其他操作系统上可能需要不同的API来访问文件系统信息。

error C2664: “BOOL GetSystemFileCacheSize(PSIZE_T,PSIZE_T,PDWORD)”: 无法将参数 1 从“DWORD *”转换为“PSIZE_T”

该错误意味着您在调用GetSystemFileCacheSize函数时,第一个参数的类型不正确。PSIZE_T是一个指向SIZE_T类型的指针,而DWORD*是一个指向DWORD类型的指针。根据错误信息,您正在尝试将一个DWORD类型的指针转换为PSIZE_T类型的指针,这是不兼容的。

要解决此问题,请将第一个参数的类型更改为PSIZE_T类型的指针。下面是一个示例代码:

#include <iostream>
#include <windows.h>

int main()
{
    SIZE_T cacheSize;
    SIZE_T cacheSizePeak;
    if (GetSystemFileCacheSize(&cacheSize, &cacheSizePeak, NULL))
    {
        std::cout << "Physical disk cache size: " << cacheSize << " bytes\n";
    }
    else
    {
        std::cout << "Failed to get physical disk cache size\n";
    }
    return 0;
}

请注意,如果您使用的是Visual Studio编译器,并且正在使用更高版本的SDK,则可能还需要在代码中添加以下宏定义:

#define _WIN32_WINNT 0x0601

这将指示编译器使用Windows 7或更高版本的SDK,以便您可以使用PSIZE_T类型的指针。

【感谢数据中台厂商龙石数据为本站提供 http://www.longshidata.com/pages/government.html,,感恩 】
上一篇:渗透专题丨Vulnhub-Tr0ll2靶机打靶
下一篇:没有了
网友评论