QT案例 Qt调用WIMGAPI库,读取/修改/装载WIM、ESD格式的镜像文件

2024-01-28 13:52

本文主要是介绍QT案例 Qt调用WIMGAPI库,读取/修改/装载WIM、ESD格式的镜像文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

WIMGAPI库是 Windows 操作系统中的一个应用程序编程接口(API)可用于创建,编辑,提取浏览和部署 WIM、ESD格式的镜像文件。
这里只简单演示在Qt Creator, Mscv2017 Debug,WINDOWS 开发环境下的调用完整实例。

目录

    • WIMGAPI描述
      • 数据预处理
      • 获取镜像的相关信息
      • 导出指定系统中的文件
      • 获取镜像目录结构(装载/卸载镜像)
      • 提取镜像到指定目录

WIMGAPI描述

WIMGAPI (Windows Imaging API)是 Windows 操作系统中的一个应用程序编程接口(API),用于处理 Windows 映像(.wim)文件格式。Windows 映像是一种压缩的文件容器,可用于存储操作系统、应用程序和驱动程序等文件
注:https://www.cnblogs.com/suv789/p/17549323.html
WIMGAPI是适合WINDOWS 环境下处理WIM、ESD镜像格式的库,
常用方法说明:

  • WIMCreateFile 创建新的图像文件或打开现有的图像文件。
  • WIMApplyImage 将图像应用于Windows图像的目录路径(。wim)文件。
  • WIMCaptureImage 从目录路径捕获图像,并将其存储在图像文件中。
  • WIMCloseHandle 关闭打开的Windows imaging(。wim)文件或图像句柄。
  • WIMCommitImageHandle 将已装载映像中的更改保存回. wim文件。
  • WIMCopyFile 将现有文件复制到新文件中。通过回调函数通知应用程序它的进度。如果源文件包含验证数据,则在复制操作期间会验证文件的内容。
  • WIMDeleteImage 从. wim (Windows映像)文件中删除映像,使其无法被访问。但是,文件资源仍然可供WIMSetReferenceFile功能。
  • WIMDeleteImageMounts 从以前装载映像的所有目录中删除映像。
  • WIMExportImage 从一个Windows映像(.wim)文件复制到另一个文件。
  • WIMExtractImagePath 从Windows映像(.wim)文件复制到指定位置。
  • WIMGetAttributes 返回存储在映像文件中的卷映像的数量。
  • WIMGetImageCount 返回存储在Windows映像中的卷映像的数量。wim)文件。
  • WIMGetImageInformation 返回有关中图像的信息。wim (Windows映像)文件。
  • WIMGetMessageCallbackCount 返回图像库当前注册的回调例程的计数。
  • WIMGetMountedImageHandle 返回一个WIM句柄和一个镜像句柄,对应于一个挂载的镜像目录。
  • WIMGetMountedImageInfo 返回当前装载的映像列表。
  • WIMGetMountedImageInfoFromHandle 查询已装载图像句柄的状态。
  • WIMGetMountedImages 返回当前装载的映像列表。此功能已被取代WIMGetMountedImageInfo.
  • WIMLoadImage 从Windows映像加载卷映像(。wim)文件。
  • WIMMessageCallback 与一起使用的应用程序定义的函数WIMRegisterMessageCallback或者WIMUnregisterMessageCallback功能。
  • WIMMountImage 将映像装入Windows映像(。wim)文件复制到指定目录。
  • WIMMountImageHandle 将映像装入Windows映像(。wim)文件复制到指定目录。
  • WIMRegisterLogFile 将用于调试或跟踪目的的日志文件注册到当前WIMGAPI会话中。
  • WIMRegisterMessageCallback 用特定于图像的数据注册要调用的函数。有关可以处理的消息的信息,请参见信息.
  • WIMRemountImage 重新激活以前装载到指定目录的已装载映像。
  • WIMSetBootImage 将具有给定映像索引的映像标记为可启动。
  • WIMSetImageInformation 将有关图像的信息存储在Windows图像(。wim)文件。
  • WIMSetReferenceFile 启用WIMApplyImage和WIMCaptureImage使用替代的函数。文件资源的wim文件。当使用相似数据捕获多个图像时,这可以优化存储。
  • WIMSetTemporaryPath 设置临时图像文件的存储位置。
  • WIMSplitFile 启用大的Windows映像(。wim)文件分割成较小的部分,以便在较小形式的介质上复制或存储。
  • WIMUnmountImage 卸载Windows映像中已装载的映像(。wim)文件从指定的目录。
  • WIMUnmountImageHandle 从Windows映像中卸载映像(。wim ),它以前与一起安装WIMMountImageHandle功能。
  • WIMUnregisterLogFile 出于调试或跟踪目的,从当前WIMGAPI会话中注销日志文件。
  • WIMUnregisterMessageCallback 注销用特定于图像的数据调用的函数。

具体参数建议查看WIMGAPI 中的API说明
或者通过GItHub中获取wingapi的一些宏定义和类结构
https://github.com/VulpesSARL/MiniNT5-Tools/blob/b3bbc6c2c1af7207438a34b1546fa67ab7a038e2/FoxCWrapperWIM/wimgapi.h

数据预处理

WIMGAPI 是通过LoadLibraryExW 加载Wimgapi库获取各种Api的调用。
这里只包含部分使用到的api和宏定义,
完整的APi和宏定义建议查看GItHub上面的wimgapi.h文件内容

#define PF_INIT(proc, name)					if (pf##proc == NULL) pf##proc = \(proc##_t) GetProcAddress(GetLibraryHandle(#name), #proc)
#define PF_INIT_OR_OUT(proc, name)			do {PF_INIT(proc, name);         \if (pf##proc == NULL) {qDebug("Unable to locate %s() in '%s.dll': %s",  \
#proc, #name, GetLastError()); goto out;} } while(0)// From https://docs.microsoft.com/en-us/previous-versions/msdn10/dd834960(v=msdn.10)
// as well as https://msfn.org/board/topic/150700-wimgapi-wimmountimage-progressbar/
enum WIMMessage {WIM_MSG = WM_APP + 0x1476,WIM_MSG_TEXT,WIM_MSG_PROGRESS,	// Indicates an update in the progress of an image application.WIM_MSG_PROCESS,	// Enables the caller to prevent a file or a directory from being captured or applied.WIM_MSG_SCANNING,	// Indicates that volume information is being gathered during an image capture.WIM_MSG_SETRANGE,	// Indicates the number of files that will be captured or applied.WIM_MSG_SETPOS,		// Indicates the number of files that have been captured or applied.WIM_MSG_STEPIT,		// Indicates that a file has been either captured or applied.WIM_MSG_COMPRESS,	// Enables the caller to prevent a file resource from being compressed during a capture.WIM_MSG_ERROR,		// Alerts the caller that an error has occurred while capturing or applying an image.WIM_MSG_ALIGNMENT,	// Enables the caller to align a file resource on a particular alignment boundary.WIM_MSG_RETRY,		// Sent when the file is being reapplied because of a network timeout.WIM_MSG_SPLIT,		// Enables the caller to align a file resource on a particular alignment boundary.WIM_MSG_FILEINFO,	// Used in conjunction with WimApplyImages()'s WIM_FLAG_FILEINFO flag to provide detailed file info.WIM_MSG_INFO,		// Sent when an info message is available.WIM_MSG_WARNING,	// Sent when a warning message is available.WIM_MSG_CHK_PROCESS,WIM_MSG_SUCCESS = 0,WIM_MSG_ABORT_IMAGE = -1
};/* Action type, for progress bar breakdown */
enum action_type {OP_NOOP_WITH_TASKBAR = -3,OP_NOOP = -2,OP_INIT = -1,OP_ANALYZE_MBR = 0,OP_BADBLOCKS,OP_ZERO_MBR,OP_PARTITION,OP_FORMAT,OP_CREATE_FS,OP_FIX_MBR,OP_FILE_COPY,OP_PATCH,OP_FINALIZE,OP_MAX
};enum
{WIM_COMPRESS_NONE = 0,WIM_COMPRESS_XPRESS = 1,WIM_COMPRESS_LZX = 2
};#define WIM_GENERIC_READ					GENERIC_READ
#define WIM_GENERIC_WRITE                   GENERIC_WRITE
#define WIM_GENERIC_MOUNT                   GENERIC_EXECUTE
#define WIM_OPEN_EXISTING					OPEN_EXISTING
#define WIM_OPEN_ALWAYS                     OPEN_ALWAYS
#define WIM_UNDOCUMENTED_BULLSHIT			0x20000000#define WIM_FLAG_RESERVED					0x00000001
#define WIM_FLAG_VERIFY						0x00000002
#define WIM_FLAG_INDEX						0x00000004
#define WIM_FLAG_NO_APPLY					0x00000008
#define WIM_FLAG_NO_DIRACL					0x00000010
#define WIM_FLAG_NO_FILEACL					0x00000020
#define WIM_FLAG_SHARE_WRITE				0x00000040
#define WIM_FLAG_FILEINFO					0x00000080
#define WIM_FLAG_NO_RP_FIX					0x00000100
#define WIM_FLAG_MOUNT_READONLY            0x00000200
#define INVALID_CALLBACK_VALUE				0xFFFFFFFFstatic HMODULE GetLibraryHandle(const char* szLibraryName)
{HMODULE h = NULL;const wchar_t* wszLibraryName = NULL;int size;if (szLibraryName == NULL || szLibraryName[0] == 0)goto out;size = MultiByteToWideChar(CP_UTF8, 0, szLibraryName, -1, NULL, 0);if ((size <= 1) || ((wszLibraryName = (wchar_t*)calloc(size, sizeof(wchar_t))) == NULL) ||(MultiByteToWideChar(CP_UTF8, 0, szLibraryName, -1, (LPWSTR)wszLibraryName, size) != size))goto out;// If the library is already opened, just return a handle (that doesn't need to be freed)if ((h = GetModuleHandleW(wszLibraryName)) != NULL)goto out;h = LoadLibraryExW(wszLibraryName, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);if(h==NULL)qDebug("Unable to load '%S.dll': %s", wszLibraryName, GetLastError());
out:free((LPWSTR)wszLibraryName);return h;
}

静态实例:

typedef HANDLE (WINAPI *WIMCreateFile_t)(PWSTR, DWORD, DWORD, DWORD, DWORD, PDWORD);
static WIMCreateFile_t pfWIMCreateFile=NULL;typedef BOOL (WINAPI *WIMGetImageInformation_t)(HANDLE, PVOID , PDWORD);
static WIMGetImageInformation_t pfWIMGetImageInformation=NULL;typedef BOOL (WINAPI *WIMSetTemporaryPath_t)( HANDLE ,PWSTR );
static WIMSetTemporaryPath_t pfWIMSetTemporaryPath=NULL;typedef DWORD (WINAPI *WIMGetImageCount_t)( HANDLE );
static WIMGetImageCount_t pfWIMGetImageCount=NULL;typedef HANDLE (WINAPI *WIMLoadImage_t)(HANDLE ,DWORD );
static WIMLoadImage_t pfWIMLoadImage=NULL;typedef BOOL (WINAPI *WIMCloseHandle_t)(HANDLE);
static WIMCloseHandle_t pfWIMCloseHandle=NULL;typedef BOOL (WINAPI *WIMApplyImage_t)(HANDLE, PCWSTR, DWORD);
static WIMApplyImage_t pfWIMApplyImage=NULL;typedef BOOL (WINAPI *WIMSetBootImage_t)(HANDLE, DWORD);
static WIMSetBootImage_t pfWIMSetBootImage=NULL;typedef BOOL (WINAPI *WIMMountImage_t)(PCWSTR, PCWSTR, DWORD, PCWSTR);
static WIMMountImage_t pfWIMMountImage=NULL;typedef BOOL (WINAPI *WIMUnmountImage_t)(PCWSTR, PCWSTR, DWORD, BOOL);
static WIMUnmountImage_t pfWIMUnmountImage=NULL;typedef DWORD (WINAPI *WIMRegisterMessageCallback_t)(HANDLE, FARPROC, PVOID);
static WIMRegisterMessageCallback_t pfWIMRegisterMessageCallback=NULL;typedef DWORD (WINAPI *WIMUnregisterMessageCallback_t)(HANDLE, FARPROC);
static WIMUnregisterMessageCallback_t pfWIMUnregisterMessageCallback=NULL;typedef HANDLE (WINAPI *WIMCaptureImage_t)(HANDLE , PCWSTR , DWORD);
static WIMCaptureImage_t pfWIMCaptureImage=NULL;typedef BOOL (WINAPI *WIMMountImageHandle_t)(HANDLE, PCWSTR, DWORD);
static WIMMountImageHandle_t pfWIMMountImageHandle=NULL;typedef BOOL (WINAPI *WIMUnmountImageHandle_t)(HANDLE, DWORD);
static WIMUnmountImageHandle_t pfWIMUnmountImageHandle=NULL;typedef BOOL (WINAPI *WIMExtractImagePath_t) (HANDLE, PWSTR, PWSTR, DWORD);
static WIMExtractImagePath_t pfWIMExtractImagePath=NULL;

格式转换:
在涉及到windows api的接口调用时,
Qt开发环境下,这两个方法是绝对少不了的,毕竟传入路径的时候,都需要转换文本格式。

/** Converts an UTF8 string to UTF-16 (allocate returned string)* Returns NULL on error*/
static wchar_t* _utf8_to_wchar(const char* str)
{//    qDebug()<<"[_utf8_to_wchar] -->";int size = 0;wchar_t* wstr = NULL;if (str == NULL)return NULL;// Convert the empty string tooif (str[0] == 0)return (wchar_t*)calloc(1, sizeof(wchar_t));// Find out the size we need to allocate for our converted stringsize = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);if (size <= 1)	// An empty string would be size 1return NULL;if ((wstr = (wchar_t*)calloc(size, sizeof(wchar_t))) == NULL)return NULL;if (utf8_to_wchar_no_alloc(str, wstr, size) != size) {sfree(wstr);return NULL;}return wstr;
}/** Converts an UTF-16 string to UTF8 (allocate returned string)* Returns NULL on error*/
static char* _wchar_to_utf8(const wchar_t* wstr)
{int size = 0;char* str = NULL;/* Find out the size we need to allocate for our converted string */size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);if (size <= 1) /* An empty string would be size 1 */return NULL;if ((str = (char*)calloc(size, 1)) == NULL)return NULL;if (wchar_to_utf8_no_alloc(wstr, str, size) != size) {free(str);return NULL;}return str;
}

获取镜像的相关信息

WIM、ESD镜像版本信息实际上就是一个XML格式保存的,
通过WIMGetImageInformation 获取
示例中整个操作是将XML写入了一个临时文件,
值得注意的是pfWIMCreateFile 打开WIM时需要将dwFlagsAndAttributes设置为WIM_UNDOCUMENTED_BULLSHIT
如果设置为0,测试部分镜像会无法加载;

 	TempFilePath tempObj; //一个用于生成临时文件的类//临时文件夹wchar_t* wtemp=tempObj.GetTempDirU();wchar_t* wimage;///临时文件wchar_t* wdst=tempObj.GetTempFileNameU(1);///XML格式信息wchar_t* wim_XMLinfo;HANDLE hWim = NULL;HANDLE hFile = NULL;DWORD dw = 0;DWORD dwFlagsAndAttributes=WIM_UNDOCUMENTED_BULLSHIT;//Image_path 镜像文件路径///使用wimgapi库读取数据wimage=_utf8_to_wchar(Image_path.toStdString().c_str());//实例化API PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);PF_INIT_OR_OUT(WIMGetImageInformation, Wimgapi);PF_INIT_OR_OUT(WIMGetImageCount, Wimgapi);hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ, WIM_OPEN_EXISTING,dwFlagsAndAttributes , 0, NULL);if (hWim == NULL) {qDebug("  Could not access image: %d", GetLastError());goto out;}///设置临时文件夹路径if (!pfWIMSetTemporaryPath(hWim, wtemp)) {qDebug("  Could not set temp path: %d", GetLastError());goto out;}///获取镜像包含的系统数量int Image_Count=pfWIMGetImageCount(hWim);if(Image_Count==0)qDebug("  Could not get image count: %d", GetLastError());qDebug()<<"[Image_Count] "<<Image_Count;///获取 系统的xml信息if (!pfWIMGetImageInformation(hWim, &wim_XMLinfo, &dw) || (dw == 0)) {qDebug("  Could not access WIM info: %d", GetLastError());goto out;}///写入临时文件hFile = CreateFileW(wdst, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if ((hFile == INVALID_HANDLE_VALUE)|| (!WriteFile(hFile, wim_XMLinfo, dw, &dw, NULL))) {qDebug("  Could not extract file: %s", GetLastError());goto out;}CloseHandle(hFile);hFile=INVALID_HANDLE_VALUE;///打开xml读取数据//Parser_XML(QString::fromWCharArray(wdst));
out:if ( (hWim != NULL)) {qDebug("Closing: %s", Image_path.toStdString().c_str());if (hWim != NULL)pfWIMCloseHandle(hWim);}if(hFile != INVALID_HANDLE_VALUE)CloseHandle(hFile);safe_free(wtemp);safe_free(wimage);safe_free(wdst);

导出指定系统中的文件

一个WIM、ESD镜像文件中至少包含一个系统镜像,
pfWIMLoadImage加载镜像时都需要先设置一个临时文件夹;
默认镜像索引从1开始,小于等于镜像系统数量;

void Lib_Wimgapi::Export_File_by(int index,QString wimpath,QString outfile)
{TempFilePath tempObj;//临时文件夹wchar_t* wtemp=tempObj.GetTempDirU();///镜像路径wchar_t* wimage=_utf8_to_wchar(Image_path.toStdString().c_str());///WIM 镜像路径 如 windows\\boot.exewchar_t* wsrc=_utf8_to_wchar(wimpath.toStdString().c_str());///输出路径 如 C:\\Users\\admin\\Desktop\\boot.exewchar_t* wdst=_utf8_to_wchar(outfile.toStdString().c_str());HANDLE hWim = NULL;HANDLE hImage=NULL;DWORD dw=0;///实例化函数方法PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);PF_INIT_OR_OUT(WIMLoadImage, Wimgapi);PF_INIT_OR_OUT(WIMExtractImagePath, Wimgapi);hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ, WIM_OPEN_EXISTING,WIM_UNDOCUMENTED_BULLSHIT , 0, NULL);if (hWim == NULL) {qDebug("  Could not access image: %d", GetLastError());goto out;}///设置临时文件夹路径if (!pfWIMSetTemporaryPath(hWim, wtemp)) {qDebug("  Could not set temp path: %d", GetLastError());goto out;}///至少为1int WIM_INDEX=qMin(qMax(1,index),Image_Count);hImage = pfWIMLoadImage(hWim, WIM_INDEX);if (hImage == NULL) {qDebug("  Could not set index: %d", GetLastError());goto out;}///输出文件if (!pfWIMExtractImagePath(hImage, wsrc, wdst, dw)) {qDebug("  Could not extract file: %d", GetLastError());goto out;}out:if (hWim != NULL || hWim != hImage) {qDebug("Closing: %s", Image_path.toStdString().c_str());if (hWim != NULL)pfWIMCloseHandle(hWim);if (hImage != NULL)pfWIMCloseHandle(hImage);}safe_free(wtemp);safe_free(wimage);safe_free(wsrc);safe_free(wdst);
}

获取镜像目录结构(装载/卸载镜像)

WIMGAPI库中貌似没有可以直接获取目录结构的函数,写示例的时候我是先通过
加载镜像(pfWIMLoadImage)->
装载镜像(WIMMountImageHandle)->
读取目录结构(文件夹遍历)->
卸载镜像(WIMUnmountImageHandle)
这个流程来获取目录结构的;

 	TempFilePath tempObj;HANDLE hWim = NULL;HANDLE hImage = NULL;///Image_pathwchar_t* wimage = _utf8_to_wchar(Image_path.toStdString().c_str());//临时文件夹wchar_t* wtemp=tempObj.GetTempDirU();//用于装载的路径QDir tempMountDir(QString::fromWCharArray(wtemp)+"\\TempMount_WIMGAPI");if(!tempMountDir.exists())tempMountDir.mkpath(QString::fromWCharArray(wtemp)+"\\TempMount_WIMGAPI");wchar_t* wdst=_utf8_to_wchar(QString(QString::fromWCharArray(wtemp)+"\\TempMount_WIMGAPI").toStdString().c_str());QString TempFile=QString::fromWCharArray(wdst);qDebug()<<"临时装载路径: "<<TempFile;//PF_INIT_OR_OUT(WIMRegisterMessageCallback, Wimgapi);PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);PF_INIT_OR_OUT(WIMLoadImage, Wimgapi);PF_INIT_OR_OUT(WIMApplyImage, Wimgapi);PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);//PF_INIT_OR_OUT(WIMUnregisterMessageCallback, Wimgapi);PF_INIT_OR_OUT(WIMMountImageHandle, Wimgapi);PF_INIT_OR_OUT(WIMUnmountImageHandle, Wimgapi);// 调试信息
//    if (pfWIMRegisterMessageCallback(NULL, (FARPROC)WimProgressCallback, NULL) == INVALID_CALLBACK_VALUE) {
//        qDebug("  Could not set progress callback: %d", GetLastError());
//        goto out;
//    }hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ|WIM_GENERIC_MOUNT, WIM_OPEN_EXISTING,WIM_UNDOCUMENTED_BULLSHIT , 0, NULL);if (hWim == NULL) {qDebug("  Could not access image: %d", GetLastError());goto out;}if (!pfWIMSetTemporaryPath(hWim, wtemp)) {qDebug("  Could not set temp path: %d", GetLastError());goto out;}int WIM_INDEX=qMax(1,index.toInt());hImage = pfWIMLoadImage(hWim, WIM_INDEX);if (hImage == NULL) {qDebug("  Could not set index: %d", GetLastError());goto out;}///清除历史缓存//! 方案一 用这种方法可以检查文件目录 但是无法获取文件的属性
//    Fetch_Files.clear();
//    // Run a first pass using WIM_FLAG_NO_APPLY to count the files
//    if (!pfWIMApplyImage(hImage, wdst, WIM_FLAG_NO_APPLY)) {
//        qDebug("  Could not count the files to apply: %d", GetLastError());
//        goto out;
//    }//! 方案二 通过装载卸载的方式 获取文件目录结构if(!pfWIMMountImageHandle(hImage,wdst,WIM_FLAG_MOUNT_READONLY)){qDebug("Could not Mount Image: %d", GetLastError());goto out;}//遍历目录结构//Lib_Deploy_Environment::getInstance().EnumerateFiles(TempFile,Fetch_Files);//! 如果需要对镜像镜像修改 可以在此处操作文件目录/文件 然后提交保存//!WIMCommitImageHandle 保存装载后的更改!if(!pfWIMUnmountImageHandle(hImage,0)){//dism /Get-MountedWimInfo//dism /Cleanup-Wim//CMD_WRITE("dism /Cleanup-Wim");qDebug("Could not Unmount Image: %d", GetLastError());}
out:if ((hImage != NULL) || (hWim != NULL)) {qDebug("Closing: %s", Image_path.toStdString().c_str());if (hImage != NULL)pfWIMCloseHandle(hImage);if (hWim != NULL)pfWIMCloseHandle(hWim);}
//    if (pfWIMUnregisterMessageCallback != NULL)
//        pfWIMUnregisterMessageCallback(NULL, (FARPROC)WimProgressCallback);safe_free(wimage);safe_free(wdst);safe_free(wtemp);

其中需要注意的是:
如果因为各种原因卸载或者装载失败,都需要调用Windwos系统自带的Dism.exe删除不完整的记录
CMD执行 dism.exe /Cleanup-Wim

提取镜像到指定目录

通过pfWIMApplyImage 提取镜像时,可以加上WIMRegisterMessageCallback的回调函数,获取提取文件进度,也可以实现简单的进度条效果,

//!回调函数
static DWORD WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PVOID pvIgnored)
{//qDebug()<<"[dwMsgId] "<<dwMsgId;PBOOL pbCancel = NULL;PWIN32_FIND_DATA pFileData;const char* level = NULL;uint64_t size;switch (dwMsgId) {case WIM_MSG_PROGRESS:qDebug()<<"[WIM_MSG_PROGRESS] -->";// The default WIM progress is useless for apply (freezes at 95%, which is usually when// only half the files have been processed), so we only use it for mounting/unmounting.//UpdateProgressWithInfo(progress_op, progress_msg, progress_offset + wParam, progress_total);break;case WIM_MSG_PROCESS:{qDebug()<<"[WIM_MSG_PROCESS] -->";PWSTR pszFullPath=(PWSTR)wParam ;qDebug()<<"WIM_MSG_PROCESS pszFullPath : "<<QString::fromWCharArray(pszFullPath);// The amount of files processed is overwhelming (16k+ for a typical image),// and trying to display it *WILL* slow us down, so we don't.pbCancel = (PBOOL)lParam;*pbCancel = TRUE;break;}
//            break;case WIM_MSG_FILEINFO:{qDebug()<<"[WIM_MSG_FILEINFO] -->";pFileData = (PWIN32_FIND_DATA)lParam;if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {//!文件夹qDebug()<<"[Dir] "<<QString::fromWCharArray((PWSTR)pFileData->cFileName);qDebug()<<"[wParam] "<<QString::fromWCharArray((PWSTR)wParam);} else {//!文件qDebug()<<"[File] "<<QString::fromWCharArray(pFileData->cFileName);qDebug()<<"[wParam] "<<QString::fromWCharArray((PWSTR)wParam);}break;}case WIM_MSG_RETRY:qDebug()<<"[WIM_MSG_RETRY] -->";level = "retry";// fall throughcase WIM_MSG_INFO:if (level == NULL) level = "info";// fall throughcase WIM_MSG_WARNING:if (level == NULL) level = "warning";// fall throughcase WIM_MSG_ERROR:if (level == NULL) level = "error";SetLastError((DWORD)lParam);qDebug("WIM processing %s: %S [%d]\n", level, (PWSTR)wParam, GetLastError());break;}return WIM_MSG_SUCCESS;
}
	//!提取镜像TempFilePath tempObj;//临时文件夹wchar_t* wtemp=tempObj.GetTempDirU();//QString  Imagepath 镜像路径wchar_t* wimage=_utf8_to_wchar(Imagepath.toStdString().c_str());//QString descpath 提取到指定文件夹wchar_t* wdst=_utf8_to_wchar(descpath.toStdString().c_str());HANDLE hWim = NULL;HANDLE hImage=NULL;DWORD dw=0;PF_INIT_OR_OUT(WIMRegisterMessageCallback, Wimgapi);PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);PF_INIT_OR_OUT(WIMLoadImage, Wimgapi);PF_INIT_OR_OUT(WIMApplyImage, Wimgapi);PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);PF_INIT_OR_OUT(WIMUnregisterMessageCallback, Wimgapi);//设置回调函数if (pfWIMRegisterMessageCallback(NULL, (FARPROC)WimProgressCallback, NULL) == INVALID_CALLBACK_VALUE) {qDebug("  Could not set progress callback: %d", GetLastError());goto out;}hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ|WIM_GENERIC_MOUNT, WIM_OPEN_EXISTING,WIM_UNDOCUMENTED_BULLSHIT , 0, NULL);if (hWim == NULL) {qDebug("  Could not access image: %d", GetLastError());goto out;}if (!pfWIMSetTemporaryPath(hWim, wtemp)) {qDebug("  Could not set temp path: %d", GetLastError());goto out;}//指定镜像索引indexhImage = pfWIMLoadImage(hWim, index);if (hImage == NULL) {qDebug("  Could not set index: %d", GetLastError());goto out;}//! WIM_FLAG_FILEINFO 提取镜像if (!pfWIMApplyImage(hImage, wdst, WIM_FLAG_FILEINFO)) {qDebug("  Could not Export WIM Image: %d", GetLastError());goto out;}out:if ((hImage != NULL) || (hWim != NULL)) {qDebug("Closing: %s", Image_path.toStdString().c_str());if (hImage != NULL)pfWIMCloseHandle(hImage);if (hWim != NULL)pfWIMCloseHandle(hWim);}if (pfWIMUnregisterMessageCallback != NULL)pfWIMUnregisterMessageCallback(NULL, (FARPROC)WimProgressCallback);safe_free(wimage);safe_free(wdst);safe_free(wtemp);

提示:
pfWIMApplyImage 函数的

  • WIM_FLAG_NO_APPLY 属性不提取文件,只会在回调函数中返回WIM_MSG_PROCESS信号,同时可输出文件路径,可用于统计文件目录数量
  • WIM_FLAG_FILEINFO 属性会提取文件,按目录结构写入指定文件夹中,会在回调函数中返回WIM_MSG_FILEINFO信号。

这篇关于QT案例 Qt调用WIMGAPI库,读取/修改/装载WIM、ESD格式的镜像文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653817

相关文章

hevc和H.264格式的区别

HEVC(High Efficiency Video Coding)和H.264(也称为Advanced Video Coding,AVC)都是视频压缩标准,但它们之间存在一些显著的区别,主要集中在压缩效率、资源需求和兼容性方面。 压缩效率 HEVC,也被称为H.265,提供了比H.264更高的压缩效率。这意味着在相同的视频质量下,HEVC能够以大约一半的比特率进行编码,从而减少存储空间需求和

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

开启青龙 Ninja 扫码功能失效后修改成手动填写CK功能【修正Ninja拉库地址】

国内:进入容器docker exec -it qinglong bash #获取ninjagit clone -b main https://ghproxy.com/https://github.com/wjx0428/ninja.git /ql/ninja#安装cd /ql/ninja/backend && pnpm install cp .env.example .env

vscode-创建vue3项目-修改暗黑主题-常见错误-element插件标签-用法涉及问题

文章目录 1.vscode创建运行编译vue3项目2.添加项目资源3.添加element-plus元素4.修改为暗黑主题4.1.在main.js主文件中引入暗黑样式4.2.添加自定义样式文件4.3.html页面html标签添加样式 5.常见错误5.1.未使用变量5.2.关闭typescript检查5.3.调试器支持5.4.允许未到达代码和未定义代码 6.element常用标签6.1.下拉列表

BD错误集锦8——在集成Spring MVC + MyBtis编写mapper文件时需要注意格式 You have an error in your SQL syntax

报错的文件 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.yuan.dao.YuanUserDao"><!

QT 中ListView和ListWidget有什么区别

ListView和ListWidget在Qt框架中都是用于显示列表数据的控件,但它们在使用方法和特性上存在一些明显的差异。以下是关于它们用法不一样的地方的详细分析: 数据管理方式: ListView:使用QAbstractItemModel数据模型来管理和显示列表数据。QAbstractItemModel是一个抽象类,允许开发者自定义数据模型以适应特定的数据结构和需求。这使得ListView在处

ScrollView 非手动调用的方法

1. /**  *  非人为的时候调用这个方法  *  *  @param scrollView <#scrollView description#>  */ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {           } 2.判断控制器的view是否加载过 [willShowVC

ROS2从入门到精通4-4:局部控制插件开发案例(以PID算法为例)

目录 0 专栏介绍1 控制插件编写模板1.1 构造控制插件类1.2 注册并导出插件1.3 编译与使用插件 2 基于PID的路径跟踪原理3 控制插件开发案例(PID算法)常见问题 0 专栏介绍 本专栏旨在通过对ROS2的系统学习,掌握ROS2底层基本分布式原理,并具有机器人建模和应用ROS2进行实际项目的开发和调试的工程能力。 🚀详情:《ROS2从入门到精通》 1 控制插

el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

<div v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList" :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http:

如何使用Qt的PIMPL习惯用法(PIMPL Idiom

) PIMPL是指“Pointer to IMPLementation”(指向实现的指针),意味着将实现细节隐藏起来,用户类无需关注这些实现细节。在Qt中常用PIMPL习惯用法来清晰地区分接口与实现,尽管Qt官方文档并未详细说明该机制。本文将演示如何在Qt中使用PIMPL习惯用法,并以一个简单的坐标输入对话框作为实例。 原理与动机 PIMPL的核心在于将类的实现细节封装在一个私有类中,这个私