获取进程的信息的代码

作者: RedProgramer

以下是获取进程的信息的代码,有一个选择窗口自己选择需要显示的信息
.h文件
typedef struct _tag_PDHCounterStruct {
    HCOUNTER hCounter;      // Handle to the counter - given to use by PDH Library
    int nNextIndex;         // element to get the next raw value
    int nOldestIndex;       // element containing the oldes raw value
    int nRawCount;          // number of elements containing raw values
    PDH_RAW_COUNTER a_RawValue[1024]; // Ring buffer to contain raw values
} PDHCOUNTERSTRUCT, *PPDHCOUNTERSTRUCT;

PDH_STATUS __stdcall PDH_BrowseCallback(DWORD dwParam);
BOOL PDH_AddCounter(LPTSTR szCounterName, int nItemIndex);
void InitQuery();
extern PDH_BROWSE_DLG_CONFIG gpdhBrowseDlgConfig;
extern PPDHCOUNTERSTRUCT MyCounter;
extern HQUERY   hQuery;
extern LPTSTR gszReturnPath;
extern DWORD gdwReturnPathSize;
extern PACKAGE TForm1 *Form1;
extern nCounterIndex;

.cpp文件
void InitQuery()
{
    PDH_STATUS pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);
    if (IsErrorSeverity(pdhStatus))
    {
      return ;
    }

    MyCounter = (PPDHCOUNTERSTRUCT)GlobalAlloc(GPTR, sizeof(PDHCOUNTERSTRUCT)*1024);
    gdwReturnPathSize = 1024;
    gszReturnPath = (LPTSTR) LocalAlloc(LPTR, gdwReturnPathSize);
    nCounterIndex = 0;
 }
int TForm1::GetProcessCount()
{
  PDH_STATUS      pdhStatus;
  PDH_FMT_COUNTERVALUE  fmtValue;
  DWORD          ctrType;
  int            nRetCode = 0;

    pdhStatus = PdhCollectQueryData (hQuery);

  // Get the current value of this counter.
  for(int i=0;i<ListView2->Items->Count;i++)
  {
    pdhStatus = PdhGetFormattedCounterValue (MyCounter[i].hCounter,PDH_FMT_LONG,
        NULL,&fmtValue);
    if (pdhStatus == ERROR_SUCCESS)
    {
        ListView2->Items->Item[i]->SubItems->Strings[0] = FormatFloat("#,###,###,##0.",fmtValue.longValue);
    }
  }
/*    else
    {
    // Print the error value.
       ShowMessage("错误");;
  }*/

  return 0;

}

int TForm1::GetTotalCommittedMemory(int ProcessID)
{
    HANDLE hProcess;
    Pointer pAddr =0;
    int dwTotalCommit =0;
    int ret;
    MEMORY_BASIC_INFORMATION mi;
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,0,ProcessID);
    ret = VirtualQueryEx((char*)hProcess,(char*)pAddr,&mi,sizeof(mi));

    do
    {
        pAddr = (char *)(mi.BaseAddress) + mi.RegionSize;
        if((int)pAddr > 0x7fffffff)
            break;
        if(mi.State == MEM_COMMIT)
        {
            dwTotalCommit +=mi.RegionSize;
        }
        ret = VirtualQueryEx((char*)hProcess,(char*)pAddr,&mi,sizeof(mi));
    }while(ret == sizeof(mi));
    CloseHandle(hProcess); 
    return dwTotalCommit/1024;
}

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    GetProcessCount();
}
//---------------------------------------------------------------------------
#include <pdhmsg.h>
PDH_BROWSE_DLG_CONFIG gpdhBrowseDlgConfig;
LPTSTR gszReturnPath;
DWORD gdwReturnPathSize;
PPDHCOUNTERSTRUCT MyCounter;
HQUERY  hQuery;
int nCounterIndex;

PDH_STATUS __stdcall PDH_BrowseCallback(DWORD dwParam)
{
    PDH_STATUS returnValue;
    LPTSTR szCurrentPath;
    int nLen;

    if (gpdhBrowseDlgConfig.CallBackStatus == PDH_MORE_DATA) {
        // Buffer too small for the counters selected

        // Free original buffer
        if (NULL != LocalFree(gszReturnPath)) {
            return PDH_MEMORY_ALLOCATION_FAILURE;
        }

        // Alloc new larger buffer
        gdwReturnPathSize += 1024;
        gszReturnPath = (LPTSTR) LocalAlloc(LPTR, gdwReturnPathSize);
        if (gszReturnPath == NULL) {
            return PDH_MEMORY_ALLOCATION_FAILURE;
        }

        // Set the config structure members for the new buffer
        gpdhBrowseDlgConfig.szReturnPathBuffer = gszReturnPath;
        gpdhBrowseDlgConfig.cchReturnPathLength = gdwReturnPathSize;

        // retry the counter browse selection
        return PDH_RETRY;
    }

    returnValue = ERROR_SUCCESS;
    szCurrentPath = gszReturnPath;
    // This string manipulation code is in a try/except block
    // to gracefully handle any problems
    // with pointer values, null termination, boundary conditions, etc.
    __try {
        while (TRUE) {
            nLen = lstrlen(szCurrentPath);

            if (nLen == 0)
                break;

            PDH_AddCounter(szCurrentPath, nCounterIndex);
            nCounterIndex++;
            szCurrentPath += (nLen + 1);
        }
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
        returnValue = PDH_INVALID_BUFFER;
    }

    return returnValue ;
}


void __fastcall TForm1::Button9Click(TObject *Sender)
{

    ZeroMemory(&gpdhBrowseDlgConfig, sizeof(gpdhBrowseDlgConfig));
   

    gpdhBrowseDlgConfig.hWndOwner = this->Handle;

    gpdhBrowseDlgConfig.bIncludeInstanceIndex = false;
    gpdhBrowseDlgConfig.bSingleCounterPerAdd = false;
    gpdhBrowseDlgConfig.bSingleCounterPerDialog = false;
    gpdhBrowseDlgConfig.bLocalCountersOnly = true;
    gpdhBrowseDlgConfig.bWildCardInstances = true;
    gpdhBrowseDlgConfig.bHideDetailBox = true;
    gpdhBrowseDlgConfig.bInitializePath = true;
    gpdhBrowseDlgConfig.szReturnPathBuffer = gszReturnPath;
    gpdhBrowseDlgConfig.cchReturnPathLength = gdwReturnPathSize;

    gpdhBrowseDlgConfig.pCallBack = PDH_BrowseCallback;

    __try {
        PdhBrowseCounters(&gpdhBrowseDlgConfig);
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
//        UpdateStatus(TEXT("Exception in PdhBrowseCounters. (Handled)"));
//        MessageBeep(0);
    }

   
}
//---------------------------------------------------------------------------
BOOL PDH_AddCounter(LPTSTR szCounterName, int nItemIndex)
{
    BOOL fRes = TRUE;

    // Allocate a PDHCOUNTERSTRUCT for this new counter

    // Add the counter to the list view control


    __try {
        // Add the counter to the current query
        if (ERROR_SUCCESS == PdhAddCounter(hQuery, szCounterName, (DWORD)(&MyCounter[nItemIndex]), &(MyCounter[nItemIndex].hCounter)))
        {
            TListItem * Item = Form1->ListView2->Items->Add();
            Item->Caption = szCounterName;
            Item->SubItems->Add("");
            fRes = FALSE;
        }
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
        fRes = FALSE;
    }

    // If the add failed, then clean up the list view and the counter struct

    return fRes;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值