Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

void pointers 1

Status
Not open for further replies.

ADoozer

Programmer
Dec 15, 2002
3,487
AU
in conjuction with my other thread (thread116-1064158)
i have a follow up question regarding void pointers

when working with LPVOID how can i increment the value (see latest attempt in bold, which causes an exception error on about the 3rd loop)


this is visual basic version of the code which works
Code:
Dim hProcess As Long
Dim lpMem As Long, ret As Long, lLenMBI As Long
Dim si As SYSTEM_INFO
Dim mbi As MEMORY_BASIC_INFORMATION

hProcess = OpenProcess(PROCESS_READ_WRITE_QUERY, False, myPID)
lLenMBI = Len(mbi)

Call GetSystemInfo(si)
lpMem = si.lpMinimumApplicationAddress

Do While lpMem < si.lpMaximumApplicationAddress
	mbi.RegionSize = 0
   ret = VirtualQueryEx(hProcess, ByVal lpMem, mbi, lLenMBI)
    
   If ret = lLenMBI Then
       If ((mbi.lType = MEM_PRIVATE) And (mbi.State = MEM_COMMIT)) Then 
        	' this block is In use by this process
         'do some stuff here
        End If
        
        lpMem = mbi.BaseAddress + mbi.RegionSize
    Else
        MsgBox "debug:failed"
    End If
Loop
CloseHandle hProcess


im trying to now write a vis C++ version but i keep getting errors due to void pointers.

visual C++ code so far
Code:
{
HMODULE hModule[50];
char ModuleName[50];
unsigned char i=0;
SYSTEM_INFO si;
MEMORY_BASIC_INFORMATION mbi;
LPVOID lpMem=0;
LONG ret=0;
LONG lLenMBI=0;
LPVOID lowerGameAdd=0;
LPVOID upperGameAdd=0;

while(i<dwCbNeeded/sizeof(DWORD))
{
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_TERMINATE,0,ProcessIDList[i++]);
  if(hProcess)
  {
		cbRet=0;
      if(lpfEnumProcessModules(hProcess,hModule,50,&cbRet))
          if(lpfGetModuleBaseName(hProcess,hModule[0],ModuleName,50))
              if(!stricmp(ModuleName,"lithtech.exe"))
              {
						lLenMBI = sizeof(mbi);
          			GetSystemInfo(&si);
          			lpMem = si.lpMinimumApplicationAddress;
          			while(lpMem < si.lpMaximumApplicationAddress)
          			{
          				mbi.RegionSize = 0;
          				ret = VirtualQueryEx(hProcess, lpMem, &mbi, lLenMBI);
          				if(ret == lLenMBI)
          					if((mbi.Type == MEM_PRIVATE) && (mbi.State == MEM_COMMIT)) 
          					{
          						//do stuff here
          					}
          
          				//[b]this line is causing me the biggest headache[/b]
          				lpMem = (void *)((*((unsigned long*)mbi.BaseAddress)) + mbi.RegionSize);
          			}
              cbRet=GetLastError();
              CloseHandle(hProcess);
  				}
	}
}
FreeLibrary(hiPSAPI);
}

am i on the correct line of attack, or is there another way to work with SYSTEM_INFO void pointers?

any input greatly appreciated

If somethings hard to do, its not worth doing - Homer Simpson
 
Pointers in C/C++ are moving through objects they are pointing. If ptr is pointer to integer, then adding 1 to that pointer means moving it to the next integer forward, the addres value of that pointer actually increments by 4. LPVOID points to unknown object, so compiler doesn't know, how much pointer must be incremented. If you want move through bytes, cast that pointer to (char *).
 
im not sure i follow.

initially the values are:

lpmem : 0x00010000
lpmaxApplicationAddress : 0x7ffeffff
mbRegionSize : 8192 (in bytes)
BaseAddress : 0x00010000

i now need to update lpmem to 0x00012000 (i think)

however with:
lpMem = (void *)((*((unsigned long*)mbi.BaseAddress)) + mbi.RegionSize);

lpmem becomes 0x0044203d.

and the next time through i get exception errors.

If somethings hard to do, its not worth doing - Homer Simpson
 
Do you want to:
[tt]lpMem = (void *)(([red]*[/red]((unsigned long*)mbi.BaseAddress)) + mbi.RegionSize);[/tt]
or
[tt]lpMem = (void *)((((unsigned long*)mbi.BaseAddress)) + mbi.RegionSize);[/tt]

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You could break it down into simple steps: makes it easier to see what is going on in the debugger
Code:
unsigned long* pul = (unsigned long*)mbi.BaseAddress;
unsigned long ul = *pul;

As CajunCenturion said, do you want

pul += mbi.RegionSize;

or

ul += mbi.RegionSize;

Then it is

lpmem = (void*)pul;

or

lpmem = (void*) ul;   // You may get warnings about this
There is no point spending ages nesting brackets when you can achieve what you want with a few simple assignments. It also makes debugging a lot easier.
 
CC... that seems to have solved it.
(or at least the values of lpMem seem more consistant)

thanxs

If somethings hard to do, its not worth doing - Homer Simpson
 
Glad that it worked out. That's one of the tricky aspects of pointer arithmetic - whether to act on the actual value of the pointer, or on the value referenced by the pointer.

To supplement what mingis has already said with respect to pointer arithmetic. When you increment a pointer, the actual amount added depends on the type of the pointer. For example, a char is one-byte long, so (char *)++ will advance the pointer by one byte. A short is two bytes, so (short *)++ will advance the pointer by two bytes. An integer is four bytes, so (int *)++ will advance the pointer by four bytes, and so forth. Similarly, if you have (int *) += 2, the pointer will advance by 8 bytes - or by 2 integers. By casting a pointer to char (char *)(int *) += 3, you over-ride the default increment, and in this case, the pointer advances by exactly 3 bytes.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top