SHARE
TWEET

Untitled

a guest Jul 21st, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <initguid.h>
  4. #include <virtdisk.h>
  5.  
  6. int main()
  7. {
  8.     // VHD Acma kodu
  9.     VIRTUAL_STORAGE_TYPE type;
  10.     type.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
  11.     type.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN;
  12.  
  13.     OPEN_VIRTUAL_DISK_PARAMETERS open_params;
  14.     memset(&open_params, 0, sizeof(open_params));
  15.     PCWSTR filename = L"deneme.vhd";
  16.  
  17.     open_params.Version = OPEN_VIRTUAL_DISK_VERSION_1;
  18.     open_params.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT; // Default RW Depth
  19.     HANDLE handle = INVALID_HANDLE_VALUE;
  20.     auto ret = OpenVirtualDisk(&type, filename, VIRTUAL_DISK_ACCESS_ALL, OPEN_VIRTUAL_DISK_FLAG_NONE,
  21.         &open_params, &handle);
  22.     if (ret != ERROR_SUCCESS)
  23.     {
  24.         std::cerr << GetLastError() << std::endl;
  25.         return 255;
  26.     }
  27.  
  28.     // Attach VHD
  29.     ATTACH_VIRTUAL_DISK_PARAMETERS attach_params;
  30.     memset(&attach_params, 0, sizeof(attach_params));
  31.     attach_params.Version = ATTACH_VIRTUAL_DISK_VERSION_1;
  32.     auto ret_attach = AttachVirtualDisk(handle, 0, ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, NULL, 0);
  33.     if (ret_attach != ERROR_SUCCESS)
  34.     {
  35.         std::cerr << GetLastError() << std::endl;
  36.         return 255;
  37.     }
  38.  
  39.     // Diske atanmis fiziksel yolu al
  40.     TCHAR physical_name[MAX_PATH];
  41.     DWORD physical_name_size;
  42.     auto ret_phys = GetVirtualDiskPhysicalPath(handle, &physical_name_size, physical_name);
  43.    
  44.     // Bu fiziksel yola ait handle olustur
  45.     auto device_handle = CreateFile(physical_name, GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  46.  
  47.     //// Diskin kendisini oluştur
  48.     int sign = 70002; // Unique olmali
  49.     CREATE_DISK disk;
  50.     disk.Mbr.Signature = sign;
  51.     disk.PartitionStyle = PARTITION_STYLE_MBR;
  52.  
  53.     // CREATE_DISK IOCTL komutu
  54.     auto res = DeviceIoControl(device_handle, IOCTL_DISK_CREATE_DISK, &disk, sizeof(disk), NULL, 0, NULL, NULL);
  55.     res = DeviceIoControl(device_handle, IOCTL_DISK_UPDATE_PROPERTIES, 0, 0, NULL, 0, NULL, NULL);
  56.     if (!res)
  57.     {
  58.         std::cerr << GetLastError() << std::endl;
  59.         return 255;
  60.     }
  61.     CloseHandle(handle);
  62.     CloseHandle(device_handle);
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top