1

基于并使用 Jedi Demo VHD,我创建了一个虚拟磁盘,可以挂载和卸载它。安装后,它在磁盘管理器中显示为磁盘 1“未知”。继续在我的代码中对其进行初始化和格式化,我正在尝试使用以下代码:

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;

当 edit1.text 包含我的虚拟磁盘的名称 (TestDisk.vhd) 并且 CreateFile 和 DeviceIoControl 生成“成功”时,GetDiskPath 获取“\.\PhysicalDisk1”,但磁盘管理器中的磁盘保持不变。

我究竟做错了什么 ?

注意!如果您有基于 C# 的答案,那也很好。

4

0 回答 0