In a WCE app I’m looking for a way to copy a file (I only have to file name/path of it) to a specific memory address. The file is rather large’ish, ~40MB, so with limited resources, I was hoping to avoid reading the whole file into memory (byte array), by using the answer from this post: Copy data from from IntPtr to IntPtr
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
static void Main()
{
const int size = 200;
IntPtr memorySource = Marshal.AllocHGlobal(size);
IntPtr memoryTarget = Marshal.AllocHGlobal(size);
CopyMemory(memoryTarget,memorySource,size);
}
That leaves me to 2 problems.
First of all: How do I assign a memory address to an IntPtr?, kind of like: int* startAddr = &0x00180000
.
And secondly: How do I obtain the memory address of a file?
With these two questions answered, my code would look something like:
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
private unsafe void CopyFile()
{
try
{
fixed (Int32* startAddr = /*0x00180000*/)
{
fixed(Int32* fileAddr = /*Memory Address of file*/)
{
CopyMemory(new IntPtr(startAddr), new IntPtr(fileAddr), (uint)new FileInfo("File name").Length);
}
}
}
catch { }
}
Would that be a valid way of going about it?
Any help would be greatly appreciated. Thanks in advance!!
Update: CopyMemory is not the way of going about it. So please disregard.
Also, Sorry for not being clearer. Basically I want to move a file to the start of a partition on disk. I thought that IntPtr could also point to a disk address, but in retrospect I can see that of course it can not. Anyway sorry for the confusion.