unit createvhd;
interface
uses Sysutils, Windows;
{$ALIGN ON}
{$MINENUMSIZE 4}
const
VirtDisk_dll = '
VirtDisk.dll';
VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN = 0;
//Device type is unknown or not valid.
VIRTUAL_STORAGE_TYPE_DEVICE_ISO = 1;
//Internal use only. Not supported.
VIRTUAL_STORAGE_TYPE_DEVICE_VHD = 2;
//Virtual hard disk device type.
CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE = $200;
[COLOR="Red"]CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE = $0;
[/COLOR]
var
VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT: TGUID;
// '{EC984AEC-A0F9-47e9-901F-71415A66345B}';
VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN : TGUID;
//= 0;
type
/////////////////////////////////////////////////////////
// typedef struct _VIRTUAL_STORAGE_TYPE {
// ULONG DeviceId;
// GUID VendorId;
// } VIRTUAL_STORAGE_TYPE, *PVIRTUAL_STORAGE_TYPE;
_VIRTUAL_STORAGE_TYPE =
record
DeviceId: ULONG;
VendorId: TGUID;
end;
VIRTUAL_STORAGE_TYPE = _VIRTUAL_STORAGE_TYPE;
PVIRTUAL_STORAGE_TYPE = ^_VIRTUAL_STORAGE_TYPE;
////////////////////////////////////////////////////////
// typedef enum _VIRTUAL_DISK_ACCESS_MASK {
// VIRTUAL_DISK_ACCESS_ATTACH_RO = 0x00010000,
// VIRTUAL_DISK_ACCESS_ATTACH_RW = 0x00020000,
// VIRTUAL_DISK_ACCESS_DETACH = 0x00040000,
// VIRTUAL_DISK_ACCESS_GET_INFO = 0x00080000,
// VIRTUAL_DISK_ACCESS_CREATE = 0x00100000,
// VIRTUAL_DISK_ACCESS_METAOPS = 0x00200000,
// VIRTUAL_DISK_ACCESS_READ = 0x000d0000,
// VIRTUAL_DISK_ACCESS_ALL = 0x003f0000,
// VIRTUAL_DISK_ACCESS_WRITABLE = 0x00320000
// } VIRTUAL_DISK_ACCESS_MASK;
_VIRTUAL_DISK_ACCESS_MASK = (
VIRTUAL_DISK_ACCESS_ATTACH_RO = $00010000,
VIRTUAL_DISK_ACCESS_ATTACH_RW = $00020000,
VIRTUAL_DISK_ACCESS_DETACH = $00040000,
VIRTUAL_DISK_ACCESS_GET_INFO = $00080000,
VIRTUAL_DISK_ACCESS_CREATE = $00100000,
VIRTUAL_DISK_ACCESS_METAOPS = $00200000,
VIRTUAL_DISK_ACCESS_READ = $000d0000,
VIRTUAL_DISK_ACCESS_ALL = $003f0000,
VIRTUAL_DISK_ACCESS_WRITABLE = $00320000
);
VIRTUAL_DISK_ACCESS_MASK = _VIRTUAL_DISK_ACCESS_MASK;
PVIRTUAL_DISK_ACCESS_MASK = ^_VIRTUAL_DISK_ACCESS_MASK;
/////////////////////////////////////////////////////////////////////////
// typedef enum _CREATE_VIRTUAL_DISK_FLAG {
// CREATE_VIRTUAL_DISK_FLAG_NONE = 0x00000000,
// CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION = 0x00000001
// } CREATE_VIRTUAL_DISK_FLAG;
_CREATE_VIRTUAL_DISK_FLAG = (
CREATE_VIRTUAL_DISK_FLAG_NONE = $00000000,
CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION = $00000001
);
CREATE_VIRTUAL_DISK_FLAG = _CREATE_VIRTUAL_DISK_FLAG;
PCREATE_VIRTUAL_DISK_FLAG = ^_CREATE_VIRTUAL_DISK_FLAG;
////////////////////////////////////////////////////////////////
// typedef enum _CREATE_VIRTUAL_DISK_VERSION {
// CREATE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0,
// CREATE_VIRTUAL_DISK_VERSION_1 = 1
// } CREATE_VIRTUAL_DISK_VERSION;
_CREATE_VIRTUAL_DISK_VERSION = (
CREATE_VIRTUAL_DISK_VERSION_UNSPECIFIED = 0,
CREATE_VIRTUAL_DISK_VERSION_1 = 1
);
CREATE_VIRTUAL_DISK_VERSION = _CREATE_VIRTUAL_DISK_VERSION;
PCREATE_VIRTUAL_DISK_VERSION= ^_CREATE_VIRTUAL_DISK_VERSION;
////////////////////////////////////////////////////////////////////////////////
// typedef struct _CREATE_VIRTUAL_DISK_PARAMETERS {
// CREATE_VIRTUAL_DISK_VERSION Version;
// union {
// struct {
// GUID UniqueId;
// ULONGLONG MaximumSize;
// ULONG BlockSizeInBytes;
// ULONG SectorSizeInBytes;
// PCWSTR ParentPath;
// PCWSTR SourcePath;
// } Version1;
// } ;
//} CREATE_VIRTUAL_DISK_PARAMETERS, *PCREATE_VIRTUAL_DISK_PARAMETERS;
TCreateVirtualDiskVersion1 =
packed record
UniqueId: TGUID;
MaximumSize: ULONGLONG;
BlockSizeInBytes: ULONG;
SectorSizeInBytes: ULONG;
ParentPath: Pointer;
SourcePath: Pointer;
end;
_CREATE_VIRTUAL_DISK_PARAMETERS =
packed record
Version: _CREATE_VIRTUAL_DISK_VERSION;
[COLOR="Red"]
case V1:DWORD
of[/COLOR]
0: (Version1: TCreateVirtualDiskVersion1);
end;
CREATE_VIRTUAL_DISK_PARAMETERS = _CREATE_VIRTUAL_DISK_PARAMETERS;
PCREATE_VIRTUAL_DISK_PARAMETERS = ^_CREATE_VIRTUAL_DISK_PARAMETERS;
{
DWORD CreateVirtualDisk(
__in PVIRTUAL_STORAGE_TYPE VirtualStorageType,
__in PCWSTR Path,
__in VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask,
__in PSECURITY_DESCRIPTOR SecurityDescriptor,
__in CREATE_VIRTUAL_DISK_FLAG Flags,
__in ULONG ProviderSpecificFlags,
__in PCREATE_VIRTUAL_DISK_PARAMETERS Parameters,
__in LPOVERLAPPED Overlapped,
__out PHANDLE Handle
);
}
function CreateVirtualDisk(VirtualStorageType: PVIRTUAL_STORAGE_TYPE;
Path: PWideChar;
VirtualDiskAccessMask: VIRTUAL_DISK_ACCESS_MASK;
SecurityDescriptor: PSECURITY_DESCRIPTOR;
Flags: CREATE_VIRTUAL_DISK_FLAG;
ProviderSpecificFlags: ULong;
Parameters: PCREATE_VIRTUAL_DISK_PARAMETERS;
Overlapped: POverlapped;
Handle: PHandle): Cardinal;
stdcall external '
VirtDisk.dll'
name '
CreateVirtualDisk';
function CreateFixed(path: PWideChar; size: ULONGLONG; accessMask: _VIRTUAL_DISK_ACCESS_MASK;
var AHandle: THandle;
source: PWideChar; securityDescriptor: Pointer; overlapped: POverlapped): Cardinal;
implementation
function CreateFixed(path: PWideChar; size: ULONGLONG; accessMask: _VIRTUAL_DISK_ACCESS_MASK;
var AHandle: THandle;
source: PWideChar; securityDescriptor: Pointer; overlapped: POverlapped): Cardinal;
var
pParam: PCREATE_VIRTUAL_DISK_PARAMETERS;
pst: PVIRTUAL_STORAGE_TYPE;
m_h: THandle;
begin
pParam:=
nil;
pst:=
nil;
pst:= AllocMem(sizeof(VIRTUAL_STORAGE_TYPE));
pst.DeviceId:= VIRTUAL_STORAGE_TYPE_DEVICE_VHD;
pst.VendorId:= VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT;
pParam:= AllocMem(sizeof(CREATE_VIRTUAL_DISK_PARAMETERS));
pParam.Version:= CREATE_VIRTUAL_DISK_VERSION_1;
pParam.Version1.BlockSizeInBytes:= CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_BLOCK_SIZE;
pParam.Version1.SectorSizeInBytes:= CREATE_VIRTUAL_DISK_PARAMETERS_DEFAULT_SECTOR_SIZE;
pParam.Version1.MaximumSize:= size;
pParam.Version1.ParentPath:=
nil;
pParam.Version1.SourcePath:=
nil;
pParam.Version1.UniqueId:= StringToGUid('
{00000000-0000-0000-0000-000000000000}');
Result:= CreateVirtualDisk(pst,
path,
accessMask,
securityDescriptor,
CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION,
0,
pParam,
overlapped,
@m_h);
if Result = ERROR_SUCCESS
then
AHandle:= m_h
else
AHandle:= INVALID_HANDLE_VALUE;
if Assigned(pParam)
then FreeMem(pParam);
if Assigned(pst)
then FreeMem(pst);
end;
initialization
VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT:= StringToGUid('
{EC984AEC-A0F9-47e9-901F-71415A66345B}');
VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN := StringToGuid('
{00000000-0000-0000-0000-000000000000}');
end.