Compartilhar via


Delete an Image from a .wim File

The following code example demonstrates how to delete an image from a .wim file, by using the WIMDeleteImage function. The WIMDeleteImage function removes an image from within a .wim file so it cannot be accessed. However, the file resources are still available for use by the WIMSetReferenceFile function.

Example

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

//
// Main Function:
//
int __cdecl
wmain(DWORD argc, PWSTR argv[])
{
    HANDLE hWim = NULL;

    BOOL bRet = TRUE;

    PWSTR pszWimFile  = L"C:\\capture.wim";  // Source .wim file
    PWSTR pszTmpDir   = L"C:\\tmp";          // Temporary directory

    DWORD dwCreateFlags = 0;
    DWORD dwCreationDisposition = 0;
    DWORD dwImageIndex = 0;
    DWORD dwError = 0;

    WIM_INFO WimInfo = {0};

    // An image index number is required to delete the image or to display the image info.
    //
    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 hash values.
    // 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 the WIM.
    //
    if (bRet)
    {
        hWim = WIMCreateFile(pszWimFile,
                             WIM_GENERIC_READ | WIM_GENERIC_WRITE,
                             WIM_OPEN_EXISTING,
                             dwCreateFlags,
                             0,
                             &dwCreationDisposition);

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

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

    if (bRet)        
    {
        bRet = WIMGetAttributes(hWim, &WimInfo, sizeof(WimInfo));

        if (!bRet)
        {
            dwError = GetLastError();
            wprintf(L"WIMGetAttributes failed\n");
        }
    }

    if (bRet && WimInfo.ImageCount < 2)
    {
        dwError = ERROR_INVALID_PARAMETER;
        bRet = FALSE;

        wprintf(L"Only one image in the .wim file. Cannot delete it\n");
    }

    if (bRet && WimInfo.ImageCount < dwImageIndex)
    {
        dwError = ERROR_INVALID_PARAMETER;
        bRet = FALSE;

        wprintf(L"There is no image # %d in the WIM file\n", dwImageIndex);
    }

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

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

            wprintf(L"cannot set temp path to work in\n");
        }
    }

    // Delete the image.
    //
    if (bRet)
    {
        bRet = WIMDeleteImage(hWim, dwImageIndex);

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

            wprintf(L"deleting image failed\n");
        }
    }

    // When you are finished, close the handle that you created in the previous steps.
    //
    if (hWim)
    {
        WIMCloseHandle(hWim);
    }

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

    return dwError;
}

See Also

Concepts

Imaging Reference