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 <tchar.h>
#include <wimgapi.h>

#define IN
#define OUT
#define INOUT

//
//Callback function:
//
DWORD
WINAPI
SampleDeleteCallback(
	IN	DWORD msgId, //message ID
	IN	WPARAM param1,   //usually file name
	INOUT   LPARAM param2,   //usually error code
	IN	void  *unused
	)
{
	//First parameter: message string
	TCHAR *message  = (TCHAR *) param1;

	//Second parameter: error code
	DWORD errorCode = param2;

	switch ( msgId )
	{
		case WIM_MSG_ERROR:

			//This message is sent upon failure error case.
			//
			printf("ERROR: %s [err = %d]\n", message, errorCode);
			break;

		case WIM_MSG_RETRY:

			//This message is sent when file is being reapplied because of a
			//network timeout. Retry is done up to five times.
			//
			printf("RETRY: %s [err = %d]\n", message, errorCode);
			break;

		case WIM_MSG_INFO:

			//This message is sent when informational message is available.
			//
			printf("INFO: %s [err = %d]\n", message, errorCode);
			break;

		case WIM_MSG_WARNING:

			//This message is sent when warning message is available.
			//
			printf("WARNING: %s [err = %d]\n", message, errorCode);
			break;
}

	return WIM_MSG_SUCCESS;
}


void
SampleDeleteCleanup ( HANDLE hWim, FARPROC callback )
{
	WIMCloseHandle (hWim);
	WIMUnregisterMessageCallback( NULL, callback );
}

//
//Main Function:
//
__cdecl
wmain(DWORD argc, TCHAR *argv[])
{
	HANDLE hWim = NULL;
	DWORD  created = 0;
	FARPROC callback = (FARPROC) SampleDeleteCallback;

	TCHAR *wimFile  = TEXT("C:\\capture.wim");  //source .wim file
	TCHAR *tmpDir   = TEXT("C:\\tmp"); 	//temporary directory

	DWORD imgIndex = 0;
	WIM_INFO hWimInfo = {0};

	//Need image index to delete or to display.
	//
	if (argc != 2 || 
		((imgIndex = _tstoi(argv[1])) == 0)) {
		printf("need image Index (1-based)\n");

		SetLastError(ERROR_INVALID_PARAMETER);
		return 1;
}

	//Register callback
	//
	if (WIMRegisterMessageCallback( NULL,
									callback,
									NULL ) == INVALID_CALLBACK_VALUE) {
		printf ("Cannot set callback\n");
		return 2;
}

	//generic delete call sequence
	//
	//w = WIMCreateFile()
	//WIMDeleteImage (index)
	//WIMCloseHandle(w)
	//

	//Set up access mode and open flag
	//

	//Call SampleCleanup() upon exit from here
	//
	hWim = WIMCreateFile ( wimFile,  // existing .wim file to append to
	WIM_GENERIC_READ | WIM_GENERIC_WRITE,  //access mode
	WIM_OPEN_EXISTING, 				 //open flag
	0, // or WIM_FLAG_VERIFY flag for file corruption check
	0,
	&created );

	if ( !hWim ) {
		printf ("Cannot open WIM file\n");

		SampleDeleteCleanup(hWim, callback);
		return 2;
}

	if (!WIMGetAttributes( hWim, &hWimInfo, sizeof(hWimInfo))) {
		printf ("WIMGetAttributes failed\n");

		SampleDeleteCleanup(hWim, callback);
		return 3;
}

	if (hWimInfo.ImageCount < 2) {
		printf("Only one image in the .wim file. Cannot delete it\n");

		SampleDeleteCleanup(hWim, callback);
		SetLastError(ERROR_INVALID_PARAMETER);
		return 4;
}

	if (hWimInfo.ImageCount < imgIndex) {
		printf("There is no image # %d in the WIM file\n", imgIndex);

		SampleDeleteCleanup(hWim, callback);
		SetLastError(ERROR_INVALID_PARAMETER);
		return 5;
}

	//Set temporary directory to work in
	//REQUIRED for delete
	//
	if (!WIMSetTemporaryPath (hWim, tmpDir)) {
		printf("cannot set temp path to work in\n");

		SampleDeleteCleanup(hWim, callback);
		return 6;
}

	//Now delete the image
	//
	if (!WIMDeleteImage ( hWim,
						imgIndex )) {   //imagex index to delete
		printf("deleting image failed\n");
		SampleDeleteCleanup (hWim, callback);
		return 7;
}

	//Now we are done.
	//
	SampleDeleteCleanup (hWim, callback);

	return 0;
}

See Also