将结构转换为与其第一组成员匹配的类型的正确方法是什么? [英] What is the correct way to cast a struct to a type that matches its first set of members?

查看:55
本文介绍了将结构转换为与其第一组成员匹配的类型的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑WinAPI中的以下结构:

Please consider the following structs from the WinAPI:

typedef struct _PROCESS_MEMORY_COUNTERS {
  DWORD  cb;
  DWORD  PageFaultCount;
  SIZE_T PeakWorkingSetSize;
  SIZE_T WorkingSetSize;
  SIZE_T QuotaPeakPagedPoolUsage;
  SIZE_T QuotaPagedPoolUsage;
  SIZE_T QuotaPeakNonPagedPoolUsage;
  SIZE_T QuotaNonPagedPoolUsage;
  SIZE_T PagefileUsage;
  SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;

typedef struct _PROCESS_MEMORY_COUNTERS_EX {
  DWORD  cb;
  DWORD  PageFaultCount;
  SIZE_T PeakWorkingSetSize;
  SIZE_T WorkingSetSize;
  SIZE_T QuotaPeakPagedPoolUsage;
  SIZE_T QuotaPagedPoolUsage;
  SIZE_T QuotaPeakNonPagedPoolUsage;
  SIZE_T QuotaNonPagedPoolUsage;
  SIZE_T PagefileUsage;
  SIZE_T PeakPagefileUsage;
  SIZE_T PrivateUsage;
} PROCESS_MEMORY_COUNTERS_EX;

如您所见, PROCESS_MEMORY_COUNTERS_EX 继承自" PROCESS_MEMORY_COUNTERS -前者以后者的确切定义开头,并添加一个附加字段.

As you can see, PROCESS_MEMORY_COUNTERS_EX "inherits from" PROCESS_MEMORY_COUNTERS - the former starts with the exact definition of the latter, and adds an additional field.

考虑一个功能:

BOOL GetProcessMemoryInfo(
  HANDLE                   Process,
  PPROCESS_MEMORY_COUNTERS ppsmemCounters,
  DWORD                    cb
);

如您所见,它需要一个 PPROCESS_MEMORY_COUNTERS .但是我想将其传递给 PROCESS_MEMORY_COUNTERS_EX 的指针.

As you can see, it takes a PPROCESS_MEMORY_COUNTERS. However I would like to pass it a pointer to a PROCESS_MEMORY_COUNTERS_EX.

根据标准,如果我错了,请纠正我,我应该能够将 PROCESS_MEMORY_COUNTERS_EX * 强制转换为 PPROCESS_MEMORY_COUNTERS * ,因为前者以后者的确切定义.

According to the standard, correct me if I'm wrong, I should be able to cast a PROCESS_MEMORY_COUNTERS_EX* to a PPROCESS_MEMORY_COUNTERS*, since the former starts with the exact definition of the latter.

在C语言中将这样完成:

In C it would be done like so:

PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(process, (PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc));

如果有的话,用C ++代码执行此操作的正确方法是什么?我应该使用 reinterpret_cast< PROCESS_MEMORY_COUNTERS *>(& pmc) static_cast< PROCESS_MEMORY_COUNTERS *>(& pmc)还是其他方式?

What is the correct way to do this in C++ code, if any? Should I use reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), static_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), or something else?

推荐答案

文档说明 GetProcessMemoryInfo 接受

指向PROCESS_MEMORY_COUNTERS或PROCESS_MEMORY_COUNTERS_EX结构的指针,该结构接收有关进程的内存使用情况的信息.

A pointer to the PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX structure that receives information about the memory usage of the process.

因此, reinterpret_cast< PROCESS_MEMORY_COUNTERS *>(& pmc)将是您的问题的答案.

So, reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc) would be the answer to your question.

但是您必须将 cb 设置为要传递的对象的大小.

But you must set cb to the size of object being passed.

这篇关于将结构转换为与其第一组成员匹配的类型的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆