lscshj
lscshj

Reputation: 63

How to initialize and format a Virtual Disk using Delphi (or C#)?

Based on and using the Jedi Demo VHD, I've created a virtual disk, and can mount and dismount it. When mounted, it appears in the Disk Manager as disk 1 "unknown". Going on to initialize and format it in my code, I am trying with the following code:

procedure TMainForm.BtnInitClick(Sender: TObject);
var RetBytes: DWORD;
  hDevice: Cardinal;
  Status: LongBool;
  Drive: string;
  CDsk  : TCreateDisk;
  PS    : TPartitionStyle;
begin
  hDevice := INVALID_HANDLE_VALUE;
  Drive   := GetDiskPath(Edit1.Text);
  hDevice:=CreateFile(PChar(Drive), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
  try
    memoinfo.Lines.Add('CreateFile Success. hDevice = '+hDevice.ToString);
    CDsk.PartitionStyle := PARTITION_STYLE_GPT;
    CDsk.Gpt.DiskId := TGuid.Empty;
    CDsk.Gpt.MaxPartitionCount := 0;
    Status := DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, @CDsk, SizeOf(CDsk), nil, 0, @RetBytes, nil);
    try
      memoinfo.Lines.Add('DeviceControl Success');
    except
      on e: exception do memoinfo.Lines.Add('DeviceControl Error : '+e.Message);
    end;
  except
    on e: exception do memoinfo.Lines.Add('CreateFile Error : '+e.Message);
  end;
end;

GetDiskPath gets '\.\PhysicalDisk1' when edit1.text contains the name of my virtual disk (TestDisk.vhd) and both CreateFile and DeviceIoControl generate 'Success', but the disk in Disk Manager stays unchanged.

What am I doing wrong ?

NB! If you have a answer based on C#, that would be fine too.

Upvotes: 1

Views: 264

Answers (0)

Related Questions