iterating over a list, each item consisting of a struct within a struct, difficulty getting values
I have a list which contains a number of _MEMORY_BASIC_INFORMATION structs nested within mb structs (defs below). What I am having problems with is getting the info开发者_StackOverflow from the nested _MEMORY_BASIC_INFORMATION out. I am using an iterator to traverse the list. From below I know the error is g->mbi; but I don't know how I should reference the nested struct... Thanks.
Basically I am trying to write the base address from gMemList[i] to start = (DWORD)g.mbi.BaseAddress; but I am getting the error c2228: left of '.mbi' must have a class/struct/union.
list<struct mb *> gMemList
std::list<mb *>::iterator i = gMemList.begin();
while(i != gMemList.end())
{
struct mb *g = *i;
MEMORY_BASIC_INFORMATION mbi2 = g->mbi;
start = (DWORD)mbi2.BaseAddress;
buf = new wchar_t[255];
while(start < mbi2.RegionSize)
{...
//struct mb
//{
// MEMORY_BASIC_INFORMATION mbi;
// char *p;
//};
/*typedef struct _MEMORY_BASIC_INFORMATION {
PVOID BaseAddress;
PVOID AllocationBase;
DWORD AllocationProtect;
SIZE_T RegionSize;
DWORD State;
DWORD Protect;
DWORD Type;
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;*/
When posting such a problem, it is always extremely helpful to include the specific error message that you get from your compiler.
At the first sight I can't see why your code does not compile. However, you should be aware that your assignment of mbi2 does create a copy of the whole _MEMORY_BASIC_INFORMATION structure. (And maybe that is the source of the problem here.) You probably do not need to copy the structure, if I understand your case correctly, since you only want to get some information from it.
Try this:
_MEMORY_BASIC_INFORMATION &mbi2=(*i)->mbi;
If you do not need to change anything in mbi2, you should definitely use a const reference:
const _MEMORY_BASIC_INFORMATION &mbi2=(*i)->mbi;
Always remember that const-correctness is your friend!
Maybe this is what you are looking for:
while(start < (char*)(g->mbi.BaseAddress) + mbi2.RegionSize)
{
...
start++;
}
Could be DWORD* instead of char*, I don't know what RegionSize stands for.
BTW variable start should be DWORD*, not DWORD. Or char*, there's not much information provided.
g is a pointer, you got it right with:
MEMORY_BASIC_INFORMATION mbi2 = g->mbi;
Why are you then doing this?:
start = (DWORD)g.mbi.BaseAddress;
dereference g, or use mbi2, which you already declared.
I think the problem is your use of the word struct.
In c++ there is little difference between struct and class.
So, just for giggles change all instances of the word struct, to class.
The problem becomes more obvious when you do that. You don't have a "g" object, you have a struct g. (PS BAD NAMING)
Pull off all instances of the word struct from this code and it should all work out.
Interview question: What is the difference between a class and an object?
精彩评论