Read physical memory information from registry

本文介绍了一个用于从Windows注册表中读取物理内存信息的C语言程序。该程序通过访问注册表路径HKLM/HARDWARE/RESOURCEMAP/SystemResources/PhysicalMemory来获取系统的物理内存详情,包括总内存大小、内存范围等。
/////////////////////////////////////////////////////////////////////////
//
//  Module:
//    Read physical memory information from registry
//    HKLM/HARDWARE/RESOURCEMAP/System Resources/Physical Memory
//
//  Author:
//    CDrea (CDrea_at_SafeChina[dot]ORG
//
//  Version:
//    2007-12-24  v0.1
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>

struct _mi {
    LARGE_INTEGER    Address;
    DWORD            Length;
    DWORD            Reserved;
};

#define MI_LEN    sizeof(struct _mi)

typedef struct _phy_mem_info {
    DWORD        BusCount;
    DWORD        MemoryRangeCount;
    struct _mi*    MemInfoPtr;
}PHY_MEM_INFO;

BOOL
GetPhyMemoryInfo(
    OUT PHY_MEM_INFO* PhyMemInfo
    );

void logo();

int main()
{
    PHY_MEM_INFO    PhyMemInfo  = {0};
    LARGE_INTEGER    TotalSize = {0};

    logo();

    if(!GetPhyMemoryInfo(&PhyMemInfo)) return -1;

    for(DWORD i = 0; i < PhyMemInfo.MemoryRangeCount; i++)
    {
        TotalSize.QuadPart += (PhyMemInfo.MemInfoPtr + i)->Length;
    }

    printf("Total memory: %d MB/n", TotalSize.QuadPart / 1024 / 1024);
    printf("Bus count: %d/n", PhyMemInfo.BusCount);
    printf("Memory ranges:/n");
    for(DWORD i = 0; i < PhyMemInfo.MemoryRangeCount; i++)
    {
        printf("  0x%-016I64X  Length: 0x%X (%dK)/n",
            (PhyMemInfo.MemInfoPtr + i)->Address.QuadPart,
            (PhyMemInfo.MemInfoPtr + i)->Length,
            (PhyMemInfo.MemInfoPtr + i)->Length / 1024);
    }

    if(PhyMemInfo.MemInfoPtr != NULL)
        free(PhyMemInfo.MemInfoPtr);
    return 0;
}

BOOL
GetPhyMemoryInfo(
    OUT PHY_MEM_INFO* PhyMemInfo
)
{
    HKEY hKey;
    int nRet = 0;

    nRet = RegOpenKey(HKEY_LOCAL_MACHINE, "Hardware//ResourceMap//System Resources//Physical Memory", &hKey);
    if(nRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "RegOpenKey() failed. --err: %d/n", GetLastError());
        return FALSE;
    }

    DWORD Type = 0;
    DWORD cbData = 0;
    LPBYTE lpData = NULL;
    LPBYTE pmrc = NULL;

    RegQueryValueEx(hKey, ".Translated", 0, &Type, NULL, &cbData);
    lpData = (LPBYTE)malloc(cbData);
    RegQueryValueEx(hKey, ".Translated", 0, &Type, lpData, &cbData);
    if(nRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "RegQueryValueEx() failed. --err: %d/n", GetLastError());
        return FALSE;
    }

    DWORD BusCount = *(DWORD*)lpData;
    if(BusCount <= 0)
    {
        fprintf(stderr, "Cannot get valid memory information./n");
        return FALSE;
    }

    LPBYTE p = lpData + 0x10;
    DWORD MemoryRangesCount = 0;
    for(DWORD i = 1; i <= BusCount; i++)
    {
        MemoryRangesCount += *(DWORD*)p;
        p = p + 0x8 + i * (*(DWORD*)p) * 0x10;
    }
    p = lpData + 0x10;

    PhyMemInfo->MemInfoPtr = (struct _mi*)malloc(MI_LEN * MemoryRangesCount);
    if(PhyMemInfo->MemInfoPtr == NULL)
    {
        fprintf(stderr, "Not enough memory./n");
        return FALSE;
    }

    LPBYTE pmi = NULL;
    struct _mi* ptr = PhyMemInfo->MemInfoPtr;
    for(DWORD i = 1; i <= BusCount; i++)
    {
        pmi = p + 0x8;
        for(DWORD j = 0; j < *(DWORD*)p; j++)
        {
            ptr->Address.LowPart = *(DWORD*)pmi;
            ptr->Address.HighPart = *(long*)(pmi + 0x4);
            ptr->Length  = *(DWORD*)(pmi + 0x8);
            ptr++;
            pmi += MI_LEN;
        }

        p = p + 0x8 + i * (*(DWORD*)p) * 0x10;
    }

    PhyMemInfo->BusCount = BusCount;
    PhyMemInfo->MemoryRangeCount = MemoryRangesCount;

    RegCloseKey(hKey);
    free(lpData);
    return TRUE;
}

void logo()
{
    printf("+------------------------------------+/n");
    printf("|  Physical Memory Information v0.1  |/n");
    printf("|  Write by CDrea                    |/n");
    printf("|  CDrea_at_SafeChina[dot]ORG        |/n");
    printf("|  2007-12-24                        |/n");
    printf("|  http://www.safechina.org          |/n");
    printf("+------------------------------------+/n/n");
}

 
/******************************************************************************** * * * G-TcpClient:基于完成端口的Tcp客户端通讯模块(IOCP TcpClient) * * * * Copyright © 2009-2010 GuestCode 代码客(卢益贵) * * 版权所有 侵权必究 * * * * QQ:48092788 E-Mail:48092788@qq.com 源码博客:http://blog.youkuaiyun.com/guestcode * * * * GSN:34674B4D-1F63-11D3-B64C-11C04F79498E * * * ********************************************************************************/ #pragma once extern "C" { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 类型定义 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #ifndef _GTYPE #define _GTYPE typedef unsigned char* PGBUF; typedef void(__stdcall *PGFN_ON_CONNECTED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_RECEIVED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_SENDED)(unsigned int unPerHandle, unsigned int unSendID, unsigned int unLen); typedef void(__stdcall *PGFN_ON_DISCONNECTED)(unsigned int unPerHandle, unsigned int unFlag); typedef void(__stdcall *PGFN_ON_THREAD)(unsigned int unThreadContext, unsigned int unThreadHandle, unsigned int unThreadID, BOOL bIsBegin, unsigned int unFlag); /* typedef struct _CONNECTION { unsigned int unPerHandle; }CONNECTION, *PCONNECTION; typedef void(__stdcall *PGFN_ON_CONNECTED)(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_RECEIVED)(PCONNECTION pConnection, unsigned char* pBuf, unsigned int unLen); typedef void(__stdcall *PGFN_ON_SENDED)(PCONNECTION pConnection, unsigned int unSendID, unsigned int unLen); typedef void(__stdcall *PGFN_ON_DISCONNECTED)(PCONNECTION pConnection, unsigned int unFlag); void __stdcall GTcpClt_OnThread(unsigned int unThreadContext, unsigned int unThreadHandle, unsigned int unThreadID, BOOL bIsBegin, unsigned int unFlag) { } void __stdcall GTcpClt_OnConnected(unsigned int unPerHandle, void* _NULL, unsigned int unNULL) { } void __stdcall GTcpClt_OnReceived(PCONNECTION pConnection, unsigned char* pBuf, unsigned int unLen) { } void __stdcall GTcpClt_OnSended(PCONNECTION pConnection, unsigned int unSendID, unsigned int unLen) { } void __stdcall GTcpClt_OnDisconnected(PCONNECTION pConnection, unsigned int unFlag) { } */ #define _USE_UNICODE 1 #ifndef _DLL //#define _DLL #endif #ifdef _DLL #define DllExport _declspec(dllexport) #else #define DllExport #endif #define VER_FLAG_WIDE_CHAR 0x01 #define VER_FLAG_BETA 0x02 #define VER_FLAG_ZERO_READ 0x04 #define VER_FLAG_TRIAL 0x08 #define VER_FLAG_DEBUG 0x10 #define HNDS_CONNECT 1 #define HNDS_CONNECTED 2 #define HNDS_DISCONNECT 3 #endif //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 类型定义 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 版本信息 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #if(_USE_UNICODE) DllExport wchar_t* __stdcall GTcpClt_GetVersionName(void); #else DllExport char* __stdcall GTcpClt_GetVersionName(void); #endif DllExport float __stdcall GTcpClt_GetVersionNumber(void); DllExport unsigned int __stdcall GTcpClt_GetVersionFlag(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 版本信息 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 功能函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport DWORDLONG __stdcall GTcpClt_GetPhyMemInfo(DWORDLONG* pdwTotal); #if(_USE_UNICODE) DllExport void __stdcall GTcpClt_WriteLog(wchar_t* pstrLog, unsigned int unCode = 0); DllExport void __stdcall GTcpClt_GetHostIP(wchar_t* pstrIP, unsigned int unLen, BOOL bIsInternetIP = FALSE); #else DllExport void __stdcall GTcpClt_WriteLog(char* pstrLog, unsigned int unCode = 0); DllExport void __stdcall GTcpClt_GetHostIP(char* pstrIP, unsigned int unLen, BOOL bIsInternetIP = FALSE); #endif //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 功能函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>> PerIoData函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetGBufSize(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataSize(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataUse(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataTotal(void); DllExport float __stdcall GTcpClt_GetIoDataUseRate(void); DllExport unsigned int __stdcall GTcpClt_GetIoDataUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<< PerIoData函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>> PerHndData函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetHndDataUse(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataTotal(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataSize(void); DllExport float __stdcall GTcpClt_GetHndDataUseRate(void); DllExport unsigned int __stdcall GTcpClt_GetHndDataUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<< PerHndData函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 信息函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport unsigned int __stdcall GTcpClt_GetThreadNumber(void); DllExport unsigned int __stdcall GTcpClt_GetPageSize(void); DllExport unsigned int __stdcall GTcpClt_GetBlockSize(void); DllExport unsigned int __stdcall GTcpClt_GetConnectCount(void); DllExport unsigned int __stdcall GTcpClt_GetThreadRunCount(unsigned int unThreadContext); DllExport unsigned int GTcpClt_GetState(unsigned int unPerHandle); #if(_USE_UNICODE) DllExport wchar_t* __stdcall GTcpClt_GetThreadName(unsigned int unThreadContext); DllExport BOOL __stdcall GTcpSock_GetPerHandleInfo(unsigned int unPerHandle, wchar_t* pstrIP, unsigned int unIPLen, wchar_t* pstrPort, unsigned int unPortLen); DllExport BOOL __stdcall GTcpSock_GetPerHandleName(unsigned int unPerHandle, wchar_t* pstrName, unsigned int unLen); #else DllExport char* __stdcall GTcpClt_GetThreadName(unsigned int unThreadContext); DllExport BOOL __stdcall GTcpSock_GetPerHandleInfo(unsigned int unPerHandle, char* pstrIP, unsigned int unIPLen, char* pstrPort, unsigned int unPortLen); DllExport BOOL __stdcall GTcpSock_GetPerHandleName(unsigned int unPerHandle, char* pstrName, unsigned int unLen); #endif DllExport unsigned int __stdcall GTcpClt_GetProcesserNumber(void); DllExport BOOL __stdcall GTcpClt_IsActive(); DllExport unsigned int __stdcall GTcpClt_GetUseMem(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 信息函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 操作函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DllExport void* __stdcall GTcpClt_GetPerHandleOwner(unsigned int unPerHandle); DllExport BOOL __stdcall GTcpClt_SetPerHandleOwner(unsigned int unPerHandle, void* pOwner); DllExport PGBUF __stdcall GTcpClt_AllocGBuf(void); DllExport BOOL __stdcall GTcpClt_FreeGBuf(PGBUF pGBuf); DllExport unsigned int __stdcall GTcpClt_PostSendGBuf(unsigned int unPerHandle, PGBUF pGBuf, unsigned int unLen); DllExport unsigned int __stdcall GTcpClt_PostSendBuf(unsigned int unPerHandle, unsigned char* pBuf, unsigned int unLen); DllExport void __stdcall GTcpClt_PostBroadcast(unsigned char* pBuf, unsigned int unLen); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 操作函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 主要函数 DllExport BOOL __stdcall GTcpClt_CloseConnect(unsigned int unPerHandle); #if(_USE_UNICODE) DllExport unsigned int __stdcall GTcpClt_OpenConnect( wchar_t* pstrRemoteIP, wchar_t* pstrRemotePort, wchar_t* pstrLocalIP, PGFN_ON_CONNECTED pfnOnConnected, PGFN_ON_RECEIVED pfnOnReceived, PGFN_ON_SENDED pfnOnSended, PGFN_ON_DISCONNECTED pfnOnDisconnected, void* pOwner = NULL); #else DllExport unsigned int __stdcall GTcpClt_OpenConnect( char* pstrRemoteIP, char* pstrRemotePort, char* pstrLocalIP, PGFN_ON_CONNECTED pfnOnConnected, PGFN_ON_RECEIVED pfnOnReceived, PGFN_ON_SENDED pfnOnSended, PGFN_ON_DISCONNECTED pfnOnDisconnected, void* pOwner = NULL); #endif DllExport BOOL __stdcall GTcpClt_Start(unsigned int unHeartbeatTime = 60, unsigned int unMaxNetDelayTime = 5, unsigned int unGuardThreadSleepTime = 2, PGFN_ON_THREAD pfnOnThread = NULL, unsigned int unHndDataInitNumber = 1000, unsigned int unIoDataInitNumber = 1500, unsigned int unProcesserThreadNumber = 0, unsigned int unWorkerThreadNumber = 0); DllExport void __stdcall GTcpClt_Stop(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 主要函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< } /* ... extern "C" */
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值