Here is a sample on how to get progress and other messages during an Apply operation. You must register a method to be called by WIMGAPI and then execute the operation. You callback method is called for all kinds of messages so you'll need to determine the message type first and then cast the message as one of the message types.

using Microsoft.Wim;
using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Open a handle to the .wim file
            //
            using (WimHandle wimHandle = WimgApi.CreateFile(@"C:\test.wim", 
                WimFileAccess.Read,
                WimCreationDisposition.OpenExisting, 
                WimCreateFileOptions.None,
                WimCompressionType.None))
            {
                // Always set a temporary path
                //
                WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP"));

                // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                //
                WimgApi.RegisterMessageCallback(wimHandle, MyCallbackMethod);

                try
                {
                    // Get a handle to the first image in the .wim file
                    //
                    using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1))
                    {
                        // Apply the image contents to C:\Apply
                        // This call is blocking but WIMGAPI will be calling MyCallbackMethod() during the process
                        //
                        WimgApi.ApplyImage(imageHandle, @"C:\Apply", WimApplyImageOptions.None);
                    }
                }
                finally
                {
                    // Be sure to unregister the callback method
                    //
                    WimgApi.UnregisterMessageCallback(wimHandle, MyCallbackMethod);
                }
            }
        }

        // A callback method to be called by the WIMGAPI.  Any method registered has to have this signature:
        //    WimMessageResult FUNC(WimMessageType, object, object)
        //
        private static WimMessageResult MyCallbackMethod(WimMessageType messageType, object message, object userData)
        {
            // This method is called for every single action during the process being executed.
            // In the case of apply, you'll get Progress, Info, Warnings, Errors, etc
            //
            // The trick is to determine the message type and cast the "message" param to the corresponding type
            //

            switch (messageType)
            {
                case WimMessageType.Progress:  // Some progress is being sent

                    // Get the message as a WimMessageProgress object
                    //
                    WimMessageProgress progressMessage = (WimMessageProgress)message;

                    // Print the progress
                    //
                    Console.WriteLine("Apply progress: {0}", progressMessage.PercentComplete);

                    break;

                case WimMessageType.Warning:  // A warning is being sent

                    // Get the message as a WimMessageProgress object
                    //
                    WimMessageWarning warningMessage = (WimMessageWarning)message;

                    // Print the file and error code
                    //
                    Console.WriteLine("Warning: {0} ({1})", warningMessage.Path, warningMessage.Win32ErrorCode);

                    break;

                case WimMessageType.Error:  // An error is being sent

                    // Get the message as a WimMessageError object
                    //
                    WimMessageError errorMessage = (WimMessageError)message;

                    // Print the file and error code
                    //
                    Console.WriteLine("Error: {0} ({1})", errorMessage.Path, errorMessage.Win32ErrorCode);

                    break;
            }

            // Depending on what this method returns, the WIMGAPI will continue or cancel.
            //
            // Return WimMessageResult.Abort to cancel.  In this case we return Success so WIMGAPI keeps going

            return WimMessageResult.Success;
        }
    }
}

Last edited Aug 25, 2013 at 12:12 AM by josemesona, version 2

Comments

Samutz May 8, 2014 at 4:08 PM 
Is it possible to make ApplyImage() non-blocking? I want to update a progress bar on my main window, but the window gets completely frozen during the apply process.