problem using VirtualAllocEx

>Subject: problem with VirtualAllocEx

From: “Murali S”
>Date: Mon, 18 Jun 2001 13:37:01 +0530
>X-Message-Number: 1
>
>Hi all,
>
> I have a little problem with VirtualAllocEx() Win API Call.
>
> I want to allocate some space in the process given the process
>handle. I used the calls VirtualQueryEx() to find the next free block in =
>the
> Address Space. Once i get the free block i used the BaseAddress of
>that block to allocate the Memory using VirtualAllocEx.
>
> Here is a Flow of My Algorithm. Can any one clarify me as to why it
>fails.
>
> {
>
>=09
> MEMORY_BASIC_INFORMATION memoryInformation;
> LPVOID StartAddress =3D 0x401055; // some address in the
>virtual address space of the process.
>=09
> HANDLE pProcess =3D OpenProcess ( PROCESS_CREATE_THREAD
>| // For CreateRemoteThread
>=09
>PROCESS_QUERY_INFORMATION | // For VirtualQueryEx
>=09
>PROCESS_VM_OPERATION | // For VirtualProtectEx
> PROCESS_VM_READ
>| // For ReadProcessMemory
> PROCESS_VM_WRITE,
>// For WriteProcessMemory
> FALSE , nProcessID )=20
> do {
>
> VirtualQueryEx ( pProcess,
>startAddress,&memoryInformation, sizeof( memoryInformation ) )
> // Used to get the status of the virtual memory.
>=09
> startAddress =3D (LPVOID ) ( (UINT32)startAddress +
>memoryInformation.RegionSize );
>
> } while ( memoryInformation.State !=3D MEM_FREE );
>
> VirtualAllocEx ( pProcess, memoryInformation.BaseAddress,
> 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE )
>
> }=09

1) Why bother finding the address? Why not just pass NULL to VirtualAllocEx
and have it find an address?
2) Shouldn’t you start the search at 0?
3) Um, how about checking the return value of VirtualQueryEx?
4)
< startAddress = startAddress + memInfo.RegionSize;
> startAddress = memInfo.BaseAddress + memInfo.RegionSize;
5)
< UINT32
> ULONG_PTR for Win64 portability, or make startAddress a PBYTE and then you
don’t need a cast (too bad you can’t add to PVOIDs…I once used a compiler
that generated a runtime divide by zero when subtracting PVOIDs…)

You might try AllocationBase instead of BaseAddress, but my reading of the
docs is that BaseAddress is what you want. I never knew the difference
between them before today.

- Jay


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Hi all,

This is about the problem about VirtualAllocEx() mentioned in the
earlier mail.
I am scanning through the memory till i get a free block using
VirtualQueryEx().
Ones i get the Free block i am using the BaseAddress returned by the
VirtualQueryEx() to allocate.

I did the above steps and i got a free block of size 32K at some
BaseAddress say X.
I used X to allocate (Commit 1 page of memory).

By any chance VirtualAllocEx() will try to reserve 64K of memory
before committing 1 page of memory.
The size present is not 64K and that’s why the call is failing.

Regards,
Murali

-----Original Message-----
From: xxxxx@cornell.edu [mailto:xxxxx@cornell.edu]
Sent: Tuesday, June 19, 2001 4:52 PM
To: File Systems Developers; Murali S
Subject: problem using VirtualAllocEx

Subject: problem with VirtualAllocEx
From: “Murali S”
>Date: Mon, 18 Jun 2001 13:37:01 +0530
>X-Message-Number: 1
>
>Hi all,
>
> I have a little problem with VirtualAllocEx() Win API Call.
>
> I want to allocate some space in the process given the process
>handle. I used the calls VirtualQueryEx() to find the next free block in =
>the
> Address Space. Once i get the free block i used the BaseAddress of
>that block to allocate the Memory using VirtualAllocEx.
>
> Here is a Flow of My Algorithm. Can any one clarify me as to why it
>fails.
>
> {
>
>=09
> MEMORY_BASIC_INFORMATION memoryInformation;
> LPVOID StartAddress =3D 0x401055; // some address in the
>virtual address space of the process.
>=09
> HANDLE pProcess =3D OpenProcess ( PROCESS_CREATE_THREAD
>| // For CreateRemoteThread
>=09
>PROCESS_QUERY_INFORMATION | // For VirtualQueryEx
>=09
>PROCESS_VM_OPERATION | // For VirtualProtectEx
> PROCESS_VM_READ
>| // For ReadProcessMemory
> PROCESS_VM_WRITE,
>// For WriteProcessMemory
> FALSE , nProcessID )=20
> do {
>
> VirtualQueryEx ( pProcess,
>startAddress,&memoryInformation, sizeof( memoryInformation ) )
> // Used to get the status of the virtual memory.
>=09
> startAddress =3D (LPVOID ) ( (UINT32)startAddress +
>memoryInformation.RegionSize );
>
> } while ( memoryInformation.State !=3D MEM_FREE );
>
> VirtualAllocEx ( pProcess, memoryInformation.BaseAddress,
> 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE )
>
> }=09

1) Why bother finding the address? Why not just pass NULL to VirtualAllocEx
and have it find an address?
2) Shouldn’t you start the search at 0?
3) Um, how about checking the return value of VirtualQueryEx?
4)
< startAddress = startAddress + memInfo.RegionSize;
> startAddress = memInfo.BaseAddress + memInfo.RegionSize;
5)
< UINT32
> ULONG_PTR for Win64 portability, or make startAddress a PBYTE and then you
don’t need a cast (too bad you can’t add to PVOIDs…I once used a compiler
that generated a runtime divide by zero when subtracting PVOIDs…)

You might try AllocationBase instead of BaseAddress, but my reading of the
docs is that BaseAddress is what you want. I never knew the difference
between them before today.

- Jay


You are currently subscribed to ntfsd as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntfsd-$subst(‘Recip.MemberIDChar’)@lists.osr.com