Hi there
I would like to use Imdisk to acces and mount a VHD file, while I can manipulate the data which is read/writen from/to the Vhd. Following this post here : http://reboot.pro/to...th-devionetdll/ I created a class for the acces.
Because I would like to acces the file directly, because I want to use also bigger files, I took this example : http://reboot.pro/to...ample/?hl=proxy and created a class to use the SetFilePointerEx, ReadFile and WriteFile methods directly form C#.
The ProxyClass :
class ProxyVHD : DevioProviderManagedBase { String _VhdPath; long VhdSize; Microsoft.Win32.SafeHandles.SafeFileHandle file; public ProxyVHD(String VhdPath) { const int FILE_FLAG_NO_BUFFERING = 0x20000000; this._VhdPath = VhdPath; CreateDummyVHD(); this.file = ImDisk.WinClass.CreateFile(_VhdPath, FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, FILE_FLAG_NO_BUFFERING, IntPtr.Zero);//Namespace is called ImDisk FileInfo f = new FileInfo(_VhdPath); this.VhdSize = f.Length; } public override bool CanWrite { get { return true; } } public override long Length { get { return VhdSize; } } public override int Read(byte[] buffer, int bufferoffset, int count, long fileoffset) { int c = 0; int bytes_rad = 0; ImDisk.WinClass.SetFilePointer(file, bufferoffset, ref c, ImDisk.WinClass.EMoveMethod.Begin); ImDisk.WinClass.ReadFile(file, buffer, count, out bytes_rad, IntPtr.Zero); return bytes_rad; } public override uint SectorSize { get { return Convert.ToUInt32(512); } } public override int Write(byte[] buffer, int bufferoffset, int count, long fileoffset) { int c = 0; int bytes_rad = 0; ImDisk.WinClass.SetFilePointer(file, bufferoffset, ref c, ImDisk.WinClass.EMoveMethod.Begin); ImDisk.WinClass.WriteFile(file, buffer, count, out bytes_rad, IntPtr.Zero); return bytes_rad; } private void CreateDummyVHD()//Used to test the proxy { long diskSize = 10 * 1024 * 1024; using (Stream vhdStream = File.Create(_VhdPath)) { Disk disk = Disk.InitializeDynamic(vhdStream, DiscUtils.Ownership.None, diskSize); BiosPartitionTable bpt = BiosPartitionTable.Initialize(disk); using (NtfsFileSystem fs = NtfsFileSystem.Format(vhdStream, "MyVHD", Geometry.FromCapacity(diskSize), 0, Geometry.FromCapacity(diskSize).TotalSectorsLong)) { } } } }
The main program :
class Program { static void Main(string[] args) { string VhdFile = @"C:\Data\test\pp.vhd"; Trace.Listeners.Add(new ConsoleTraceListener()); char driveLetter = ImDiskAPI.FindFreeDriveLetter(); string mountingPoint = driveLetter.ToString() + @":"; ProxyVHD dp = new ProxyVHD(VhdFile); DevioShmService dss = new DevioShmService(dp, true); Console.WriteLine(mountingPoint); dss.StartServiceThreadAndMountImDisk(ImDiskFlags.DeviceTypeHD | ImDiskFlags.TypeProxy, mountingPoint); dss.WaitForServiceThreadExit(); Console.WriteLine("End"); Console.ReadLine(); } }
The WinAPIClass :
static class WinClass { public enum EMoveMethod : uint { Begin = 0, Current = 1, End = 2 } [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern int SetFilePointer( [In] SafeFileHandle hFile, [In] int lDistanceToMove, [In, Out] ref int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod); [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] internal static extern SafeFileHandle CreateFile( string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, int securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template); const int FILE_FLAG_NO_BUFFERING = 0x20000000; [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal extern static int ReadFile(SafeFileHandle handle, byte[] bytes, int numBytesToRead, out int numBytesRead, IntPtr overlapped_MustBeZero); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal extern static int WriteFile(SafeFileHandle handle, byte[] bytes, int numBytesToRead, out int numBytesRead, IntPtr overlapped_MustBeZero); }
This also works fine. But then running the program as admin, it first shows me :
G:
Creating objects for shared memory communication 'devio-365125873'.
Creating shared momory object, 2101248 bytes.
Raising service ready event.
Wait finished. Disposing file mapping object.
Client connected, waiting for request.
Largest requested read size is now: 512 bytes
and then trying to acces the vhd with Windows Explorer it tells me that the file is damaged and can not be read. But mounting the file with Imdisk works fine. I can´t find my mistake.
I would be very happy, if some of you could help me.
Thans in advance
Madome31
Edited by Madome31, 09 January 2015 - 02:26 PM.