/// <summary> /// Loads the DISM API library associated with the provided DismGeneration. /// NOTE: This must be called before calling DismApi.Initialize(), as the initialization takes precedence based on Dynamic Link Library loading. /// If a DismGeneration library has already been loaded when initialize() is called, that version of the DISM generation library is utilized. If no DISM API library /// is loaded when initialization is called, it will attempt to load the DISM API installed on the local system (System32), if available. /// Only a single DISM generation library may be loaded at a given time. To switch versions, the caller can use LoadDismGenerationLibrary() and UnloadDismGenerationLibrary() /// to switch between DISM generations (WAIK and/or WADK) and/or what's natively available on the local system (System32). /// </summary> /// <param name="generation"> /// The DismGeneration to be loaded. /// </param> /// <returns> /// TRUE if successful, otherwise FALSE. /// </returns> public static bool LoadDismGenerationLibrary(DismGeneration generation) { if (_hDismApi != IntPtr.Zero) { return(false); } string dismApiPath = string.Empty; switch (generation) { case DismGeneration.Win10: dismApiPath = WADK10DismApiPath; break; case DismGeneration.Win8_1: dismApiPath = WADK81DISMAPIPath; break; case DismGeneration.Win8: dismApiPath = WADK80DISMAPIPath; break; case DismGeneration.Win7: dismApiPath = WAIKDISMAPIPath; break; default: return(false); } return((_hDismApi = NativeMethods.LoadLibrary(dismApiPath)) != IntPtr.Zero); }
/// <summary> /// Initializes DISM API, using the specified DISM Generation. Initialize must be called once per process before calling any other DISM API functions. /// </summary> /// <param name="logLevel">Indicates the level of logging.</param> /// <param name="logFilePath">A relative or absolute path to a log file. All messages generated will be logged to this path. If NULL, the default log path, %windir%\Logs\DISM\dism.log, will be used.</param> /// <param name="scratchDirectory">A relative or absolute path to a scratch directory. DISM API will use this directory for internal operations. If null, the default temp directory, \Windows\%Temp%, will be used.</param> /// <param name="dismGeneration">The DISM Generational Library to be used.</param> /// /// <exception cref="Exception">If an error occurs loading the latest DISM Generational Library.</exception> /// <exception cref="DismException">When a failure occurs.</exception> public static void InitializeEx(DismLogLevel logLevel, string logFilePath, string scratchDirectory, DismGeneration dismGeneration) { if (CurrentDismGeneration != DismGeneration.NotFound) { throw new Exception($"A DISM Generation library is already loaded ({dismGeneration}). Please call Shutdown() first to release the existing library."); } if (dismGeneration != DismGeneration.NotFound && !DismUtilities.LoadDismGenerationLibrary(dismGeneration)) { throw new Exception($"Loading the latest DISM Generation library ({dismGeneration}) failed."); } DismApi.Initialize(logLevel, logFilePath, scratchDirectory); CurrentDismGeneration = dismGeneration; }