使用Windows虚拟磁盘服务时,ISO文件将无法打开,错误代码为50:ERROR_NOT_SUPPORTED

时间:2014-05-15 21:07:33

标签: c++ windows virtualization mount iso

我正在尝试在程序中加载ISO文件并使用Windows 8.1虚拟磁盘服务进行安装。在加载ISO之前,我加载游戏所在的VHD文件。然后我想挂载ISO以使游戏工作(ISO驻留在VHD上,但当ISO位于程序目录中时也会出现问题。)

现在,我不知道为什么操作是“NOT SUPPORTED”(错误代码50),但我知道windows能够挂载ISO,因为当我双击它们时,它们会被Windows资源管理器挂载(并且它显然使用虚拟磁盘服务,因为虚拟服务的KB表明从Windows 7及更高版本支持ISO挂载。)

现在,我如何尝试加载iso文件:

GUID GUID_TEST = { 12345678 - 1234 - 5678 - 1234 - 000000000000 };

//convert string to wstring
std::wstring s2ws(const std::string& str)
{
    typedef std::codecvt_utf8<wchar_t> convert_typeX;
    std::wstring_convert<convert_typeX, wchar_t> converterX;

    return converterX.from_bytes(str);
}

DWORD OpenISO(const char* isoFilePath, HANDLE *handle)
{
    VIRTUAL_STORAGE_TYPE storageType =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_ISO,
        GUID_TEST
    };

    OPEN_VIRTUAL_DISK_PARAMETERS parameters =
    {
        OPEN_VIRTUAL_DISK_VERSION_1
    };

    return ::OpenVirtualDisk(
        &storageType,
        s2ws(std::string(isoFilePath)).c_str(),
        VIRTUAL_DISK_ACCESS_READ,
        OPEN_VIRTUAL_DISK_FLAG_NONE,
        &parameters,
        handle);
}

void main()
{
    //load VHD here, do some program setup, etc etc
    //then load iso:

    //makesure VHD is mounted and ready
    UINT dtype = GetDriveTypeA(Config.MountPoint.c_str());
    while (dtype == 1 || dtype == 0)
    {
        dtype = GetDriveTypeA(Config.MountPoint.c_str());
        Sleep(10);
    }
    std::cout << "OK" << std::endl;
    //just a bit more waiting to be sure it is really loaded correctly
    Sleep(2500);
    //now load the iso our game needs to run
    HANDLE  isohandle;
    if (FileExists(Config.ISO_location.c_str()))
    {
        std::cout << "Mounting ISO... " << std::flush;
        if (OpenISO(Config.ISO_location.c_str(), &isohandle) == ERROR_SUCCESS)
        {
            if (AttachVirtualDisk(isohandle, NULL, ATTACH_VIRTUAL_DISK_FLAG_READ_ONLY, 0, 0, NULL) == ERROR_SUCCESS)
            {
                std::cout << "ISO mounted.\r\n";
            }
            else
            {
                std::cout << "ISO NOT mounted (" << GetLastError() << ").\r\n";
            }
        }
        else
        {
            std::cout << "Cannot open ISO file, skipping (" << GetLastError() << ").\r\n";
        }
    }
    //launch the game process here
    //wait til game ends
    //cleanup
    //shutdown
}

我总是收到消息ISO NOT mount(50)。当使用microsoft默认guid替换GUID_TEST时,我得到ISO NOT挂载(0),错误代码0应该表示成功,但不知何故,ISO不会出现在磁盘管理中。这有什么不对?为什么我的ISO不能以编程方式安装,而探险家可以?

我承认我并不完全知道如何安装ISO,但我这样安装VHD:

DWORD OpenVDisk(const char* virtualDiskFilePath, HANDLE *handle)
{
    VIRTUAL_STORAGE_TYPE storageType =
    {
        VIRTUAL_STORAGE_TYPE_DEVICE_VHD,
        VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
    };

    OPEN_VIRTUAL_DISK_PARAMETERS parameters =
    {
        OPEN_VIRTUAL_DISK_VERSION_1
    };

    parameters.Version1.RWDepth = 1024;

    return ::OpenVirtualDisk(
        &storageType,
        s2ws(std::string(virtualDiskFilePath)).c_str(),
        VIRTUAL_DISK_ACCESS_ALL,
        OPEN_VIRTUAL_DISK_FLAG_NONE,
        &parameters,
        handle);
}

    //somewhere else
HANDLE  handle;
DWORD   result;
ULONG   bytesUsed;

result = OpenVDisk(Config.VHD_location.c_str(), &handle);
if (result != ERROR_SUCCESS)
{
    std::cout << "Unable to open virtual disk '" << Config.VHD_location << "'" << std::endl;
    return (int)pressanykey("Hit any key to quit the application\r\n");
}
std::cout << "OK" << std::endl;
std::cout << "Attaching Game Disk..." << std::flush;
result = AttachVirtualDisk(handle, NULL, ATTACH_VIRTUAL_DISK_FLAG_NO_DRIVE_LETTER, 0, 0, NULL);
if (result != ERROR_SUCCESS)
{
    std::cout << "Unable to attach virtual disk (did you forget to run this program with administrative rights? -- " << GetLastError() << ")" << std::endl;
    return (int)pressanykey("Hit any key to quit the application\r\n");
}
std::cout << "OK" << std::endl;

和VHD安装工作。这让我真的很奇怪为什么ISO没有。

1 个答案:

答案 0 :(得分:0)

解决方案是确保指定了ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME标志,然后还要确保ISO文件大小是2048字节的倍数。现在它有效。