I am trying to write some code which copies a locked file. the system registry to be exact.
Does someone have some experience with that?
If all else fails you can access the raw disk and copy the file from there. You can find out where exactly on disk a file is with the FSCTL_GET_RETRIEVAL_POINTERS system call. Opening a handle to a raw disk is easy:
VolumeHandle = CreateFile("C:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
For finding out where a file is on disk see the GetFragments() subroutine in the JkDefragLib.cpp source. I don't have an example for reading the file into memory, but take a look at the "Read the boot block from the disk" section in the AnalyzeNtfsVolume() subroutine in the ScanNtfs.cpp sources.