The following code example demonstrates how to split an image into multiple parts, by using the WIMSplitFile function.

Example

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

#define IN
#define INOUT

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

	//Second parameter: error code or pointer to a large integer specifying chunk size
	DWORD errorCode = param2;
	LARGE_INTEGER *chunkSize = (LARGE_INTEGER *) param2;

	switch ( msgId )
	{
		case WIM_MSG_SPLIT:

			//This message is sent when each split .wim file is successful.
			//You can change a split .wim file name or size with this message.
			//
			//printf("changing split chunk size from %I64d ", chunkSize->QuadPart);
			//chunkSize->QuadPart = 5 * 1024 * 1024; //change chunk size to 5MB
			//printf("to %I64d bytes", chunkSize->QuadPart);
			break;

		case WIM_MSG_ERROR:

			//This message is sent upon failure error case.
			//
			printf("ERROR: %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
SampleSplitCleanup( HANDLE hWim, FARPROC callback)
{
	if ( hWim )
	{
		WIMCloseHandle( hWim );
}
}

//
//Main Function:
//
__cdecl
wmain(DWORD argc, TCHAR *argv[])
{
	HANDLE sWim = NULL;
	DWORD  sCreated = 0;
	LARGE_INTEGER chunkSize = {0};

	FARPROC callback = (FARPROC) SampleSplitCallback;

	TCHAR *source  = TEXT("C:\\capture.wim");  //source .wim file
	TCHAR *target  = TEXT("C:\\sample_split.swm");  //destination split .wim file
	TCHAR *tmpDir  = TEXT("C:\\tmp"); 		 //temporary directory: OPTIONAL

	WIM_INFO sWimInfo = {0};

	//specify split chunk size in bytes
	//
	if ((argc != 2) ||
		((chunkSize.QuadPart = _tstol(argv[1])) == 0)) {
		printf("need split chunk size in bytes\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 split call sequence
	//
	//w = WIMCreateFile()
	//WIMSplitFile (w, targetname, chunksize, )
	//WIMCloseHandle(w)

	//Call Sample*Cleanup() upon exit from here
	//

	//Open source .wim file
	//
	sWim = WIMCreateFile ( source, 		//source .wim file to split
						 WIM_GENERIC_READ, //access mode
						 WIM_OPEN_EXISTING,   //open flag
						 WIM_FLAG_VERIFY,  //recommened flag
						 0, 			 //no need to set compression
						 &sCreated );

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

		SampleSplitCleanup(sWim, callback);
		return 3;
}

	if (!WIMGetAttributes( sWim, &sWimInfo, sizeof(sWimInfo))) {
		printf ("WIMGetAttributes failed\n");

		SampleSplitCleanup(sWim, callback);
		return 4;
}

	//Verify if it has been already split
	//
	if (sWimInfo.TotalParts != 1) {
		printf ("The .wim file has already been splitted. Cannot split it again.");

		SampleSplitCleanup(sWim, callback);
		SetLastError(ERROR_INVALID_PARAMETER);
		return 5;
}

	//Now split
	//
	if (!WIMSplitFile ( sWim,
						target,  //destination split .wim file template
						&chunkSize,
						0) ) {  // reserved. Must be 0
		printf("Split failed\n");
		if (GetLastError() == ERROR_MORE_DATA) {
			printf("Need bigger chunk size to split\n");
	}
		SampleSplitCleanup(sWim, callback);
		return 6;
}

	//Now we are done
	//
	SampleSplitCleanup(sWim, callback);

	return 0;
}

See Also