共用方式為


Export a .wim File to Another .wim File

The following code example demonstrates how to export an image in a .wim file to another .wim file, by using the WIMExportImage function. The WIMExportImage function transfers the data of an image from one .wim file to another.

Example

#include <stdio.h>
#include <windows.h>
#include <wimgapi.h>

//
// Main function:
//
int __cdecl
wmain(DWORD argc, PWSTR argv[])
{
    BOOL bRet = TRUE;
    HANDLE hSrcWim = NULL,
           hDestWim = NULL;
    HANDLE hSrcImage = NULL;
    DWORD  dwCreationResult = 0,
           dwImageIndex = 0;
    DWORD  dwCreateFlags = 0;
    DWORD  dwError = 0;

    PWSTR pszSourceFile  = L"C:\\sample_image.wim";  // Source .wim file
    PWSTR pszTargetFile  = L"C:\\sample_exported.wim";  // Destination .wim file
    PWSTR pszTmpDir      = L"C:\\tmp";    // Temporary directory: OPTIONAL

    WIM_INFO WimInfo = {0};

    if ((argc != 2) ||
        !(dwImageIndex = _wtoi(argv[1])))
    {
        wprintf(L"need image Index (1-based)\n");
        dwError = ERROR_INVALID_PARAMETER;
        bRet = FALSE;
    }

    // Optionally, if the image was captured with WIM_FLAG_VERIFY, 
    // you can use whole-file verification.  Whole-file
    // verification performs extra hashing checks and generates extra hashes.
    // It detects file corruption due to a bad disk or a
    // faulty network connection, but it adds time to the operation due to
    // extra I/O operations and extra hash checks. 
    // To enable whole-file verification, use:
    //
    // dwCreateFlags |= WIM_FLAG_VERIFY;

    // Open source .wim file
    //
    if (bRet)
    {
        hSrcWim = WIMCreateFile(pszSourceFile,
                                WIM_GENERIC_READ,
                                WIM_OPEN_EXISTING,
                                dwCreateFlags,
                                0,
                                &dwCreationResult);

        if (!hSrcWim)
        {
            dwError = GetLastError();
            bRet = FALSE;

            wprintf(L"Cannot open src WIM file\n");
        }
    }

    //
    // Note: To attach split .wim files (SWM) to this session, use:
    //    WIMSetReferenceFile(hSrcWim, pszFileName, dwFlags)
    //

    if (bRet)
    {
        bRet = WIMGetAttributes(hSrcWim, &WimInfo, sizeof(WimInfo));
        if (!bRet)
        {
            dwError = GetLastError();
            wprintf(L"Cannot get WIM attr\n");
        }
    }

    if (bRet)
    {
        wprintf(L"\nWIM path: %s\n", WimInfo.WimPath);
        wprintf(L" image #: %d\n", WimInfo.ImageCount);
        wprintf(L" comp.  : %s\n", (WimInfo.CompressionType == WIM_COMPRESS_NONE)?"NO":"YES");
        wprintf(L" part  #: %d / %d\n\n", WimInfo.PartNumber, WimInfo.TotalParts);
    }

    // Image index and Image count are both 1-based integers.
    //
    if (bRet && dwImageIndex > WIMGetImageCount(hSrcWim))
    {
        dwError = ERROR_INVALID_PARAMETER;
        bRet = FALSE;

        wprintf(L"cannot find image index %d in src WIM file\n", dwImageIndex);
    }

    // Try to open the destination .wim file.
    //
    if (bRet)
    {
        DWORD dwTargetCompressionType = 0;

        dwTargetCompressionType = WIM_COMPRESS_XPRESS;

        hDestWim = WIMCreateFile(pszTargetFile,
                                 WIM_GENERIC_WRITE | WIM_GENERIC_READ,
                                 WIM_OPEN_ALWAYS,
                                 dwCreateFlags,
                                 dwTargetCompressionType,
                                 &dwCreationResult);

        if (!hDestWim)
        {
            dwError = GetLastError();
            bRet = FALSE;

            wprintf(L"Cannot open target WIM file\n");
        }
    }

    // Set a temporary working directory.
    //
    if (bRet)
    {
        bRet = (WIMSetTemporaryPath(hSrcWim, pszTmpDir) &&
                WIMSetTemporaryPath(hDestWim, pszTmpDir));

        if (!bRet)
        {
            dwError = GetLastError();
            wprintf(L"Cannot set temporary path\n");
        }
    }

    // Capture or append an image.
    //
    if (bRet)
    {
        hSrcImage = WIMLoadImage(hSrcWim, dwImageIndex);
        if (!hSrcImage)
        {
            dwError = GetLastError();
            bRet = FALSE;

            wprintf(L"Cannot load imagex %d from src WIM file\n", dwImageIndex);
        }
    }

    if (bRet)
    {
        DWORD dwExportFlags = 0;

        // Example export flags:
        // dwExportFlags |= WIM_EXPORT_ALLOW_DUPLICATES;

        //
        // Note: For progress reporting, you can use WIMRegisterMessageCallback. For more information,
        // see the Apply an Image from a .wim file code sample.
        //

        bRet = WIMExportImage(hSrcImage, hDestWim, dwExportFlags);
        if (!bRet)
        {
            dwError = GetLastError();

            wprintf(L"Export failed\n");

            if (dwError == ERROR_ALREADY_EXISTS)
            {
                wprintf(L"the same image already exist in the destination .wim file\n");
                // You can use WIM_EXPORT_ALLOW_DUPLICATES if you do not want to block duplicate images
                // from being added to a single .wim file.
            }
        }
    }

    // When you are finished, close the handles that you created in the previous steps..
    //
    if (hSrcImage) WIMCloseHandle(hSrcImage);
    if (hSrcWim)   WIMCloseHandle(hSrcWim);
    if (hDestWim)  WIMCloseHandle(hDestWim);

    wprintf(L"Returning status: 0x%x\n", dwError);

    return dwError;
}

See Also

Concepts

Imaging Reference