WIMMountImageHandle导致ERROR_INVALID_HANDLE

时间:2018-07-25 11:59:19

标签: c# .net api

我正在编写一个分析MSWIM files的程序。

最小示例:

    static void Test()
    {
        UIntPtr creationResult = UIntPtr.Zero;
        IntPtr WIMHandle = WIM.WIMCreateFile(@"D:\ISOCpy\install.wim", (uint)WIM.CreateFileAccess.Mount, (uint)WIM.CreateFileMode.OpenExisting, 0, 0, out creationResult);
        if (WIMHandle == IntPtr.Zero)
        {
            Console.WriteLine("Could not open WIM File. LastError: " + Marshal.GetLastWin32Error() + " / 0x" + Marshal.GetLastWin32Error().ToString("x"));
            return;
        }
        try
        {
            // Set TMP Path
            if (!WIM.WIMSetTemporaryPath(WIMHandle, @"D:\ISOCpy\y"))
            {
                // handle will be closed in destructor
                Console.WriteLine("Could not set WIM temp path. LastError: " + Marshal.GetLastWin32Error() + " / 0x" + Marshal.GetLastWin32Error().ToString("x"));
                return;
            }

            int imgnums = WIM.WIMGetImageCount(WIMHandle);
            if (imgnums <= 0)
            {
                // handle will be closed in destructor
                Console.WriteLine("Could not get number of WIMimages. Return: " + imgnums + "; LastError: " + Marshal.GetLastWin32Error() + " / 0x" + Marshal.GetLastWin32Error().ToString("x"));
                return;
            }

            for (uint mountindex = 1; mountindex <= imgnums; mountindex++)
            {
                String mountpath = @"D:\ISOCpy\z_" + mountindex;
                Directory.CreateDirectory(mountpath);

                IntPtr ImgHandle = WIM.WIMLoadImage(WIMHandle, mountindex);
                if (ImgHandle == IntPtr.Zero)
                {
                    int rc = Marshal.GetLastWin32Error();
                    Utils.DeleteDir(mountpath);
                    Console.WriteLine("Could not WIMLoadImage. Image: " + mountindex + "; LastError: " + rc + " / 0x" + rc.ToString("x"));
                    return;
                }
                if (!WIM.WIMMountImageHandle(ImgHandle, mountpath, 0))
                {
                    int rc = Marshal.GetLastWin32Error();
                    Utils.DeleteDir(mountpath);
                    Console.WriteLine("Could not WIMMountImageHandle. Image: " + mountindex + "; LastError: " + rc + " / 0x" + rc.ToString("x"));
                    return;
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("" + e);
        }
        finally
        {
            //WIM.WIMCloseHandle(WIMHandle); TODO
        }
    }

但是WIMMountImageHandle无效。它总是失败,并显示错误6:ERROR_INVALID_HANDLE。我没有在documentation

中看到任何内容

WIMMountImage可以使用,但是我需要WIMMountImageHandle提供的属性。

1 个答案:

答案 0 :(得分:0)

至少,我明白了。

必须使用其他“读取”访问权限打开文件:

IntPtr WIMHandle = WIM.WIMCreateFile(@"D:\ISOCpy\install.wim", 
    ((uint)WIM.CreateFileAccess.Mount | (uint)WIM.CreateFileAccess.Read), 
    (uint)WIM.CreateFileMode.OpenExisting, 0, 0, out creationResult);