PDH获取指定网卡速度

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <pdh.h>
#include <pdhmsg.h>
  
  #pragma comment(lib, "pdh.lib")
  
  CONST ULONG SAMPLE_INTERVAL_MS    = 1000;
  CONST PWSTR BROWSE_DIALOG_CAPTION = L"Select a counter to monitor.";
  
  void wmain(void)
  {
      PDH_STATUS Status;
      HQUERY Query = NULL;
      HCOUNTER Counter;
      PDH_FMT_COUNTERVALUE DisplayValue;
      DWORD CounterType;
      SYSTEMTIME SampleTime;
      PDH_BROWSE_DLG_CONFIG BrowseDlgData;//----Intel[R] Centrino[R] Wireless-N 100-----
      WCHAR CounterPathBuffer[PDH_MAX_COUNTER_PATH]=
		  L"\\Network Interface(Intel[R] Centrino[R] Wireless-N 100)\\Bytes Received/sec";
  
      //
      // Create a query.
      //
  
      Status = PdhOpenQuery(NULL, NULL, &Query);
      if (Status != ERROR_SUCCESS) 
      {
         wprintf(L"\nPdhOpenQuery failed with status 0x%x.", 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Qt通过PDH获取网络流量需要使用Windows的性能数据帮助程序(PDH)接口。以下是获取网络流量的示例代码: ```cpp #include <windows.h> #include <pdh.h> #include <pdhmsg.h> #include <QDebug> #define MAX_COUNTER_PATH 256 void getNetworkStat(double* inBytes, double* outBytes) { PdhQueryOptions(NULL, PDH_GLOBAL_MACHINE, NULL); HCOUNTER inCounter; HCOUNTER outCounter; wchar_t inCounterPath[MAX_COUNTER_PATH]; wchar_t outCounterPath[MAX_COUNTER_PATH]; // 获取网络接口的计数器路径 PdhLookupPerfNameByIndex(NULL, 2, (LPWSTR)inCounterPath, NULL); PdhLookupPerfNameByIndex(NULL, 10, (LPWSTR)outCounterPath, NULL); // 打开网络接口的计数器 PdhOpenQuery(NULL, 0, NULL); PdhAddCounter(NULL, inCounterPath, 0, &inCounter); PdhAddCounter(NULL, outCounterPath, 0, &outCounter); PdhCollectQueryData(NULL); // 获取网络流量值 PDH_FMT_COUNTERVALUE inValue; PDH_FMT_COUNTERVALUE outValue; PdhCollectQueryData(NULL); PdhGetFormattedCounterValue(inCounter, PDH_FMT_DOUBLE, NULL, &inValue); PdhGetFormattedCounterValue(outCounter, PDH_FMT_DOUBLE, NULL, &outValue); *inBytes = inValue.doubleValue; *outBytes = outValue.doubleValue; // 关闭计数器和查询 PdhCloseQuery(NULL); PdhCloseCounter(inCounter); PdhCloseCounter(outCounter); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); double inBytes; double outBytes; getNetworkStat(&inBytes, &outBytes); qDebug() << "In Bytes: " << inBytes << " Out Bytes: " << outBytes; return a.exec(); } ``` 在这个示例代码中,我们使用 `PdhLookupPerfNameByIndex` 函数获取网络接口计数器的路径,并使用 `PdhOpenQuery` 函数打开查询,然后使用 `PdhAddCounter` 函数打开计数器。最后使用 `PdhGetFormattedCounterValue` 获取计数器的值。注意,需要在程序结束时使用 `PdhCloseCounter` 和 `PdhCloseQuery` 函数关闭计数器和查询。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值