VirtualAlloc/VirtualCopy

G

Giuseppe

Hi all, I have a problem with this code...(overflow Exception)
I'd like to access to 0x80920000 register but this is out of range of
IntPtr. What type can I use instead IntPtr?
Thank You very much

code:
[DllImport("Coredll.dll")]
extern public static bool VirtualCopy (IntPtr lpvDest, IntPtr lpvSrc,
UInt32 cbSize, PageAccessFlags fdwProtect);

[DllImport("Coredll.dll")]
extern public static IntPtr VirtualAlloc(IntPtr lpAddress, UInt32
dwSize, MemUsageFlags flAllocationType, PageAccessFlags
flProtect);

[DllImport("Coredll.dll")]
extern public static bool VirtualFree(IntPtr lpAddress, UInt32 dwSize,
MemUsageFlags dwFreeType);



//Flags
[FlagsAttribute]
public enum PageAccessFlags : int
{

PAGE_READONLY = 0x02,
PAGE_READWRITE = 0x04,
PAGE_EXECUTE = 0x10,
PAGE_EXECUTE_READ = 0x20,
PAGE_EXECUTE_READWRITE= 0x40,
PAGE_GUARD = 0x100,
PAGE_NOACCESS = 0x01,
PAGE_NOCACHE = 0x200,
PAGE_PHYSICAL = 0x400
}

public enum MemUsageFlags : int
{
MEM_COMMIT = 0x1000,
MEM_RESERVE = 0x2000,
MEM_DECOMMIT = 0x4000,
MEM_RELEASE = 0x8000
}



private void button1_Click(object sender, System.EventArgs e)
{

//*****************************
//
IntPtr PHYSADDR = (IntPtr)0x80920000;
//********************************

UInt32 SIZE = 4800*4; // Size to map
IntPtr lpv;
bool bRet;
lpv = VirtualAlloc(IntPtr.Zero,
SIZE,
MemUsageFlags.MEM_RESERVE,
PageAccessFlags.PAGE_NOACCESS);
bRet = VirtualCopy(lpv,
PHYSADDR,
SIZE,
PageAccessFlags.PAGE_READWRITE |
PageAccessFlags.PAGE_NOCACHE|PageAccessFlags.PAGE_PHYSICAL );

if (bRet==false)

{
label1.Text="Error";
VirtualFree(lpv,0,MemUsageFlags.MEM_RELEASE );
}else{ label1.Text="OK";}

}
 
G

Guest

What do you mean by "out of range"? It's a perfectly valid number for
IntPtr or uint. In fact, it would work as an int since the MSB isn't set.

-Chris
 
G

Giuseppe

What do you mean by "out of range"? It's a perfectly valid number for
IntPtr or uint. In fact, it would work as an int since the MSB isn't set.

-Chris

Thanks Chris,

I have these results:

IntPtr PHYSADDR = (IntPtr)0x10000000; // Valid Address
IntPtr PHYSADDR = (IntPtr)0x7FFF0000; // Valid

IntPtr PHYSADDR = (IntPtr)0x80000000; // NOT Valid-> raise OverFlow

Then I deduce that 0x80000000 or 0x80920000 is not a valid number...
Ooops, probably taking look to MSDN I can use UIntPtr.
Thanks
Giusepe
 
C

Chris Tacke, eMVP

No idea where my brain was at 6:30 AM, but the MSG *is* set in this number,
however that makes no difference as IntPtrs have no sign. These are valid:

IntPtr i = new IntPtr(0x80000000);
IntPtr i = (IntPtr)0x80000000;
uint i = 0x80000000;

This is not:

int i = 0x80000000;

Not sure what's going wrong with your code.
--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
P

Peter Foot [MVP]

You can probably work around this issue by using the unchecked expression
around the value since IntPtr doesn't have a constructor which takes a
UInt32, even though the IntPtr can store the value:-
IntPtr myptr = IntPtr.Zero;

unchecked

{

myptr = (IntPtr)0x80920000;

}

Alternatively you can use the UIntPtr type which allows you to create from a
uint as well as convert back into a uint with ToUInt32()

Peter
 
G

Giuseppe

Il Tue, 22 Mar 2005 09:41:57 -0500, Chris Tacke, eMVP ha scritto:
No idea where my brain was at 6:30 AM, but the MSG *is* set in this number,
however that makes no difference as IntPtrs have no sign. These are valid:

IntPtr i = new IntPtr(0x80000000);
IntPtr i = (IntPtr)0x80000000;
uint i = 0x80000000;

This is not:

int i = 0x80000000;

Not sure what's going wrong with your code.

Sorry, this

IntPtr i = (IntPtr)0x80000000;

raise an OverFlow exception then i can't use IntPtr.

Giuseppe
 
S

Steve Maillet \(eMVP\)

Since no-one else has asked... Why in the world do you think you need
VirtualAlloc/VirtualCopy in a MANAGED application? (Use of those APIS is
STRONGLY discouraged in NATIVE code let alone MANAGED!) What's the big
picture you are attempting to achieve? There is almost certainly a better
(easier) way of doing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top