Why "VirtualCopy" always returns false

J

jing.zong

Hello,

I don't know why "VirtualCopy" always returns false in the following
C# (.net compact framework).

Also, I cannot find the value of PAGE_PHYSICAL, which I really need
it.

Thanks,
Jing


//================== Code begin

[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
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 = ?
}

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

// Maps Physical address to Virtual Address
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr PHYSADDR = (IntPtr)0x10000000; // This could be any
physical address
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);

Console.WriteLine(bRet);

if (bRet == false)
{
Console.WriteLine("VirtualCopy Error");
VirtualFree(lpv,
0,
MemUsageFlags.MEM_RELEASE );
}
}
//====================== Code End
 
D

Daniel Moth

Hi

I am not an expert but...
...the same code works for me with the FlagsAttribute applied to the
PageAccess enumeration and, unlike your code, mine uses the same access
protection value for both VirtualAlloc and VirtualCopy...

Cheers
Daniel

PS PAGE_PHYSICAL 0x400
 
J

jing.zong

Thanks Daniel,

How to find Flag values (e.g PAGE_PHYSICAL)? What is access protection
value, and how to find this value? (i.e, Is there any documents?)

Thanks in advance,
Jing
 
D

Daniel Moth

Jing

Your "public enum PageAccessFlags : int" needs an attribute applied to it.
Namely the FlagsAttribute. You can look it up in MSDN help..

When you make this call:
VirtualCopy(lpv,
PHYSADDR,
SIZE,
PageAccessFlags.PAGE_READWRITE |
PageAccessFlags.PAGE_NOCACHE);

the last parameter you pass in (the enumeration value) is the access
protection value. You can look this function up in MSDN too.
In my case I pass the same value to the VirtualAlloc too [you pass NOACCESS
only]

If you make these 2 changes then you will have the same as mine and since
mine works so will yours hopefully.

I am running this on our own custom device where I know at which physical
address there is free space for me to play with...

Cheers
Daniel
 

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