DiskPart VDisk entry

Hi All,
I am trying to open and attach a virtual disk, and then detach it.

My problem is when i detach it, i still get entry from diskpart as follows.
DISKPART> list vdisk

VDisk ### Disk ### State Type File


VDisk 0 Disk — Added Unknown \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}\846B846B-0000-0000-007E-000000000000
VDisk 1 Disk — Added Unknown \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}\846B846B-0000-0000-007E-000000000000
VDisk 2 Disk — Added Unknown \Device\Volume{d6cc17c5-1735-4085-bce7-964f1e9f5de9}\846B846B-0000-0000-007E-000000000000
VDisk 3 Disk — Added Unknown \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}\846B846B-0000-0000-007E-000000000000

Even though the disk is detached successfully, and the disk isn’t available in diskmgmt.

I use the code from microsoft to open/attach and detech virtual disks.
Code is as follows
BOOL OpenAndAttachVHD(PCWSTR pszVhdPath)
{
BOOL bRet = FALSE;
HANDLE hVhd = INVALID_HANDLE_VALUE;
DWORD ret;
OPEN_VIRTUAL_DISK_PARAMETERS oparams;
ATTACH_VIRTUAL_DISK_PARAMETERS iparams;
VIRTUAL_STORAGE_TYPE vst =
{
VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
};

wprintf(L"OpenAndAttachVHD %s\n", pszVhdPath);

oparams.Version = OPEN_VIRTUAL_DISK_VERSION_1;
oparams.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

iparams.Version = ATTACH_VIRTUAL_DISK_VERSION_1;

ret = OpenVirtualDisk(&vst, pszVhdPath,
VIRTUAL_DISK_ACCESS_ATTACH_RW | VIRTUAL_DISK_ACCESS_GET_INFO | VIRTUAL_DISK_ACCESS_DETACH,
OPEN_VIRTUAL_DISK_FLAG_NONE, &oparams, &hVhd);

if (ERROR_SUCCESS == ret)
{
printf(“success opening vdisk…\n”);
ret = AttachVirtualDisk(hVhd, NULL,
ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, &iparams, NULL);

if (ERROR_SUCCESS == ret)
{
printf(“success attaching vdisk…\n”);
}
else
{
printf(“failed to attach vdisk…err 0x%x\n”, ret);
PrintErrorMessage(GetLastError());
bRet = FALSE;
}
}
else
{
printf(“failed to open vdisk…err 0x%x\n”, ret);
PrintErrorMessage(GetLastError());
bRet = FALSE;
}

if (INVALID_HANDLE_VALUE != hVhd)
{
CloseHandle(hVhd);
}

return bRet;
}

BOOL OpenAndDetachVHD(PCWSTR pszVhdPath)
{
BOOL bRet = FALSE;
DWORD ret;
DETACH_VIRTUAL_DISK_FLAG Flags;
HANDLE hVhd = INVALID_HANDLE_VALUE;
OPEN_VIRTUAL_DISK_PARAMETERS oparams;
VIRTUAL_STORAGE_TYPE vst =
{
VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
};

wprintf(L"OpenAndDetachVHD %s\n", pszVhdPath);

oparams.Version = OPEN_VIRTUAL_DISK_VERSION_1;
oparams.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;

ret = OpenVirtualDisk(&vst, pszVhdPath,
VIRTUAL_DISK_ACCESS_DETACH,
OPEN_VIRTUAL_DISK_FLAG_NONE, NULL /*&oparams*/, &hVhd);

if (ERROR_SUCCESS == ret)
{
printf(“success opening vdisk…\n”);
Flags = DETACH_VIRTUAL_DISK_FLAG_NONE;
ret = DetachVirtualDisk(hVhd, Flags, 0);
if (ERROR_SUCCESS == ret)
{
printf(“success detaching vdisk…\n”);
}
else
{
printf(“failed to detach vdisk… %d\n”, ret);
PrintErrorMessage(GetLastError());
bRet = FALSE;
}
}
else
{
printf(“failed to open vdisk…err %d\n”, ret);
PrintErrorMessage(GetLastError());
bRet = FALSE;
}

if (INVALID_HANDLE_VALUE != hVhd)
{
CloseHandle(hVhd);
}

return bRet;
}

this is by design

On Tue, Dec 6, 2016 at 11:10 AM, wrote:

> Hi All,
> I am trying to open and attach a virtual disk, and then detach it.
>
> My problem is when i detach it, i still get entry from diskpart as follows.
> DISKPART> list vdisk
>
> VDisk ### Disk ### State Type File
> --------- -------- -------------------- --------- ----
> VDisk 0 Disk — Added Unknown
> \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}<br>> 846B846B-0000-0000-007E-000000000000
> VDisk 1 Disk — Added Unknown
> \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}<br>> 846B846B-0000-0000-007E-000000000000
> VDisk 2 Disk — Added Unknown
> \Device\Volume{d6cc17c5-1735-4085-bce7-964f1e9f5de9}<br>> 846B846B-0000-0000-007E-000000000000
> VDisk 3 Disk — Added Unknown
> \Device\Volume{d6cc17c5-1734-4085-bce7-964f1e9f5de9}<br>> 846B846B-0000-0000-007E-000000000000
>
> Even though the disk is detached successfully, and the disk isn’t
> available in diskmgmt.
>
> I use the code from microsoft to open/attach and detech virtual disks.
> Code is as follows
> BOOL OpenAndAttachVHD(PCWSTR pszVhdPath)
> {
> BOOL bRet = FALSE;
> HANDLE hVhd = INVALID_HANDLE_VALUE;
> DWORD ret;
> OPEN_VIRTUAL_DISK_PARAMETERS oparams;
> ATTACH_VIRTUAL_DISK_PARAMETERS iparams;
> VIRTUAL_STORAGE_TYPE vst =
> {
> VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
> VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
> };
>
> wprintf(L"OpenAndAttachVHD %s\n", pszVhdPath);
>
> oparams.Version = OPEN_VIRTUAL_DISK_VERSION_1;
> oparams.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;
>
> iparams.Version = ATTACH_VIRTUAL_DISK_VERSION_1;
>
> ret = OpenVirtualDisk(&vst, pszVhdPath,
> VIRTUAL_DISK_ACCESS_ATTACH_RW | VIRTUAL_DISK_ACCESS_GET_INFO |
> VIRTUAL_DISK_ACCESS_DETACH,
> OPEN_VIRTUAL_DISK_FLAG_NONE, &oparams, &hVhd);
>
> if (ERROR_SUCCESS == ret)
> {
> printf(“success opening vdisk…\n”);
> ret = AttachVirtualDisk(hVhd, NULL,
> ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME, 0, &iparams,
> NULL);
>
> if (ERROR_SUCCESS == ret)
> {
> printf(“success attaching vdisk…\n”);
> }
> else
> {
> printf(“failed to attach vdisk…err 0x%x\n”, ret);
> PrintErrorMessage(GetLastError());
> bRet = FALSE;
> }
> }
> else
> {
> printf(“failed to open vdisk…err 0x%x\n”, ret);
> PrintErrorMessage(GetLastError());
> bRet = FALSE;
> }
>
> if (INVALID_HANDLE_VALUE != hVhd)
> {
> CloseHandle(hVhd);
> }
>
> return bRet;
> }
>
> BOOL OpenAndDetachVHD(PCWSTR pszVhdPath)
> {
> BOOL bRet = FALSE;
> DWORD ret;
> DETACH_VIRTUAL_DISK_FLAG Flags;
> HANDLE hVhd = INVALID_HANDLE_VALUE;
> OPEN_VIRTUAL_DISK_PARAMETERS oparams;
> VIRTUAL_STORAGE_TYPE vst =
> {
> VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
> VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
> };
>
> wprintf(L"OpenAndDetachVHD %s\n", pszVhdPath);
>
> oparams.Version = OPEN_VIRTUAL_DISK_VERSION_1;
> oparams.Version1.RWDepth = OPEN_VIRTUAL_DISK_RW_DEPTH_DEFAULT;
>
> ret = OpenVirtualDisk(&vst, pszVhdPath,
> VIRTUAL_DISK_ACCESS_DETACH,
> OPEN_VIRTUAL_DISK_FLAG_NONE, NULL /&oparams/, &hVhd);
>
> if (ERROR_SUCCESS == ret)
> {
> printf(“success opening vdisk…\n”);
> Flags = DETACH_VIRTUAL_DISK_FLAG_NONE;
> ret = DetachVirtualDisk(hVhd, Flags, 0);
> if (ERROR_SUCCESS == ret)
> {
> printf(“success detaching vdisk…\n”);
> }
> else
> {
> printf(“failed to detach vdisk… %d\n”, ret);
> PrintErrorMessage(GetLastError());
> bRet = FALSE;
> }
> }
> else
> {
> printf(“failed to open vdisk…err %d\n”, ret);
> PrintErrorMessage(GetLastError());
> bRet = FALSE;
> }
>
> if (INVALID_HANDLE_VALUE != hVhd)
> {
> CloseHandle(hVhd);
> }
>
> return bRet;
> }
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:>

But the design doesn’t apply, if we open, attach and then detach the virtual disk directly with diskpart.
Any ideas?