次の方法で共有


DISM API ドライバー関数のサンプル

展開イメージのサービスと管理 (DISM) API を使用すると、DISM プラットフォームでカスタマイズされたソリューションを構築できます。 DISM API を使用して、Windows イメージ内の Windows 機能、パッケージ、およびドライバーをインストール、アンインストール、構成、および更新できます。

ドライバー関数を使用したサンプル

この例では、WIM イメージのマウント、コミット、マウント解除、進行状況コールバックの使用、ドライバーの取得と列挙、セッションを開く、または閉じる方法について説明します。

#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;
}

DISM API サンプル

DISM API リファレンス

DISM API の使用

DISM アプリケーションの作成

DISM API トラブルシューティング