Compartir a través de


Ejemplo de funciones del controlador de la API de DISM

La API de Administración y mantenimiento de imágenes de implementación (DISM) permite crear soluciones personalizadas en la plataforma DISM. La API de DISM se puede usar para instalar, desinstalar, configurar y actualizar características, paquetes y controladores de Windows en una imagen de Windows.

Ejemplo de uso de funciones de controlador

En este ejemplo se muestra cómo montar, confirmar y desmontar una imagen WIM, usar la devolución de llamada de progreso, obtener y enumerar controladores, y abrir y cerrar una sesión.

#include "windows.h"
#include <stdio.h>
#include "DismApi.h"

void MountProgressCallback(
    __in UINT uiCurrent,
    __in UINT uiTotal,
    __in PVOID pUserData
    )
{
    UNREFERENCED_PARAMETER(pUserData);

    wprintf(L".");

    if (uiCurrent == uiTotal)
    {
        wprintf(L"\nMounting Complete\n");
    }
}

int _cdecl wmain()
{
    HRESULT hr = S_OK;
    HRESULT hrLocal = S_OK;
    DismSession session = DISM_SESSION_DEFAULT;
    BOOL bMounted = FALSE;
    UINT uiCount = 0;
    DismDriverPackage *driverPackage = NULL;
    DismDriverPackage *driverPackage2 = NULL;
    DismDriver *driversInDriverPackage = NULL;

    // Initialize the API
    hr = DismInitialize(DismLogErrorsWarningsInfo, L"C:\\MyLogFile.txt", NULL);
    if( FAILED(hr))
    {
        wprintf(L"DismInitialize Failed: %x\n", hr);
        goto Cleanup;
    }

    wprintf(L"Mounting: ");

    // Mount a WIM image
    hr = DismMountImage(L"C:\\install.wim",
                        L"C:\\MountPath",
                        1,
                        NULL,
                        DismImageIndex,
                        DISM_MOUNT_READWRITE,
                        NULL,
                        MountProgressCallback,
                        NULL);
    if( FAILED(hr))
    {
        wprintf(L"\nDismMountImage Failed: %x\n", hr);
        goto Cleanup;
    }

    bMounted = TRUE;

    // Open a session against the mounted image
    hr = DismOpenSession(L"C:\\MountPath",
                         NULL,
                         NULL,
                         &session);
    if( FAILED(hr))
    {
        wprintf(L"DismOpenSession Failed: %x\n", hr);
        goto Cleanup;
    }

    // Add a driver into the mounted image
    hr = DismAddDriver(session,
                       L"C:\\drivers\\MyDriver.inf",
                       FALSE );
    if( FAILED(hr))
    {
        wprintf(L"DismAddDriver Failed: %x\n", hr);
        goto Cleanup;
    }

    // Get the list of drivers in the mounted image
    hr = DismGetDrivers(session,
                        FALSE,
                        &driverPackage,
                        &uiCount );
    if( FAILED(hr))
    {
        wprintf(L"DismGetDrivers Failed: %x\n", hr);
        goto Cleanup;
    }

    // Print out the names of all of the drivers from the mounted image
    wprintf(L"\n\nThese are the third party drivers from the image:\n\n");
    for (UINT i = 0; i < uiCount; ++i)
    {
        wprintf(L"Driver Package %u: %s\n", i, driverPackage[i].PublishedName);
        wprintf(L"Original file name: %s\n\n", driverPackage[i].OriginalFileName);
    }

    // Get more extended information about the first driver from the mounted
    // image.  Note that the third party drivers are always named as oem%d.inf
    hr = DismGetDriverInfo(session,
                           L"oem0.inf",
                           &driversInDriverPackage,
                           &uiCount,
                           &driverPackage2);
    if( FAILED(hr))
    {
        wprintf(L"DismGetDriverInfo Failed: %x\n", hr);
        goto Cleanup;
    }

    // Print out the top level info for this driver
    wprintf(L"\n\nThis is more detailed information about the first driver package and the set of drivers contained inside\n\n");
    wprintf(L"Driver Package: %s\n", driverPackage->PublishedName);
    wprintf(L"Original file name: %s\n\n", driverPackage->OriginalFileName);

    // Print out some specific information for each driver in the driver package
    for (UINT i = 0; i < uiCount; ++i)
    {
        wprintf(L"Driver %u: %s\n", i, driversInDriverPackage[i].ManufacturerName);
        wprintf(L"Hardware Description: %s\n", driversInDriverPackage[i].HardwareDescription);
        wprintf(L"Hardware Id: %s\n", driversInDriverPackage[i].HardwareId);
        wprintf(L"Architecture: %u\n", driversInDriverPackage[i].Architecture);
        wprintf(L"Service Name: %s\n", driversInDriverPackage[i].ServiceName);
        wprintf(L"Compatible Ids: %s\n", driversInDriverPackage[i].CompatibleIds);
        wprintf(L"Exclude Ids: %s\n\n", driversInDriverPackage[i].ExcludeIds);
    }

    // Commit the changes that were made to the image
    hr = DismCommitImage(session,
                         0,
                         NULL,
                         NULL,
                         NULL);
    if( FAILED(hr))
    {
        wprintf(L"DismCommitImage failed: %x\n", hr);
        goto Cleanup;
    }

Cleanup:

    // Delete the memory associated with the objects that were returned
    hrLocal = DismDelete(driverPackage);
    if( FAILED(hrLocal))
    {
        wprintf(L"DismDelete Failed: %x\n", hrLocal);
    }

    hrLocal = DismDelete(driverPackage2);
    if( FAILED(hrLocal))
    {
        wprintf(L"DismDelete Failed: %x\n", hrLocal);
    }

    hrLocal = DismDelete(driversInDriverPackage);
    if( FAILED(hrLocal))
    {
        wprintf(L"DismDelete Failed: %x\n", hrLocal);
    }

    // Close the DismSession to free up resources tied to this image servicing session
    hrLocal = DismCloseSession (session);
    if( FAILED(hrLocal))
    {
        wprintf(L"DismCloseSession Failed: %x\n", hrLocal);
    }

    // Unmount the image if is wa mounted
    if (bMounted)
    {
        hrLocal = DismUnmountImage(L"C:\\MountPath",
                                   DISM_DISCARD_IMAGE,
                                   NULL,
                                   NULL,
                                   NULL);
        if( FAILED(hrLocal))
        {
            wprintf(L"DismUnmountImage Failed: %x\n", hrLocal);
        }
    }

    // Shutdown the DISM API to free up remaining resources
    hrLocal = DismShutdown();
    if( FAILED(hrLocal))
    {
        wprintf(L"DismShutdown Failed: %x\n", hr);
    }

    wprintf(L"Return code is: %x\n", hr);
    return hr;
}

Ejemplos de la API de DISM

Referencia de la API de DISM

Uso de la API de DISM

Creación de una aplicación DISM

Solución de problemas de la API de DISM