The following code example demonstrates how to mount and to unmount an image, by using the WIMMountImage and WIMUnmountImage functions.

Example

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


#define MOUNT_DIRECTORY TEXT("c:\\mount")
#define WIM_FILE		TEXT("c:\\sample_image.wim")
#define IMAGE_INDEX	 1

DWORD MountImage()
{
	BOOL  bRet	= FALSE;
	DWORD dwError = ERROR_SUCCESS;
	TCHAR szTempPath[MAX_PATH] = {0};

	//Get a temporary directory for Wimgapi to work in.
	//
	bRet = GetTempPath(ARRAYSIZE(szTempPath), szTempPath);
	if (FALSE == bRet)
	{
		dwError = GetLastError();
		_tprintf(TEXT("\nError 0x%08x while attempting to determine temporary path.\n"), 
				 dwError);
		return dwError;
}

	//Mount the image, by specifying the temporary directory retrieved above so
	//that this image will be mounted for read/write access.
	//
	bRet = WIMMountImage(MOUNT_DIRECTORY,
						 WIM_FILE,
						 IMAGE_INDEX,
						 szTempPath);
	if (FALSE == bRet)
	{
		dwError = GetLastError();
		_tprintf(TEXT("\nError 0x%08x while attempting to mount image.\n"), 
				 dwError);
		return dwError; 
}

	return dwError;
}

DWORD UnmountImage(BOOL bCommitChanges)
{
	BOOL  bRet	= FALSE;
	DWORD dwError = ERROR_SUCCESS;

	//Unmount the image.
	//
	bRet = WIMUnmountImage(MOUNT_DIRECTORY,
						 NULL, // Unused in unmount
						 0,  // Unused in unmount
						 bCommitChanges);
	if (FALSE == bRet)
	{
		dwError = GetLastError();
		_tprintf(TEXT("\nError 0x%08x while attempting to unmount image.\n"), 
				 dwError);
		return dwError; 
}

	return dwError;
}

//Main function
//
__cdecl wmain(int argc, LPWSTR argv[])
{
	DWORD dwError = ERROR_SUCCESS;

	//Mount the image.
	//
	dwError = MountImage();
	if (ERROR_SUCCESS != dwError)
	{
		return dwError;
}

	//Unmount the image, by commiting changes.
	//
	dwError = UnmountImage(TRUE);
	if (ERROR_SUCCESS != dwError)
	{
		return dwError;
}

	return dwError;
}

Remarks

  • When mounting an image, you must ensure that the temporary path does not lie within the mount path.

  • If you intend to mount an image for read-only access, do not specify a temporary path.

See Also