|
采用VC6编写,纯SDK,不用MFC,目的是实现能安装WIM封装的XP,当然也支持VISTA/WIN7/2008等,由于水平有限,很难将程序完善,现在把源码公布,让更多的人来帮忙修改,不过希望完善了能再分享改善的代码给我,前提是基于SDK修改的,如果改用MFC就不必了。也希望能对那些想写基于WIMGAPI程序的爱好者有一点帮助,这是开源的一个主要原因吧。
本程序实用到了WIMGAPI中的几个API,有WIMCreateFile,WIMGetImageCount,MyGetWimImageInfo,
WIMRegisterMessageCallback,WIMSetTemporaryPath,WIMLoadImage,WIMApplyImage,
WIMUnregisterMessageCallback,WIMCloseHandle这几个函数,另外还用到了管道通讯,写出资源到文件
等等。我也是通过网络参考组织起来编程的,并不是高手。
目前我遇到的问题是:
1.ListView控件的问题,我不知道怎么实现选中一行,而且事件处理好像还有点问题(MFC的话就好办,但
我喜欢用资源模板来实现界面,减少导入的dll库)
2.处理引导文件还不完善
3.没有条件多环境测试
4.暂时没有发现的问题
本想长篇大论的,但表达能力有限,就写那么多了,总之,希望这个程序有用!
附件请下载后把wiminstall - 20120402.z01.zip 和wiminstall - 20120402.z02.zip 后面的.zip去掉,这是论坛限制了扩展名
,图片上传不了,怪了
帖上主要代码,方便编程爱好者参考- // wiminstall.cpp :
- ///////////////////////////////////////////////////////////////////////
- // WimInstall 2012.4.2 //
- // by fly2sky QQ:659090695 //
- // //
- ///////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "resource.h"
- #include "wimgapi.h"
- #define WIM_APPLY_FINISH WM_USER+100
- #define WIM_PROGRESS_INFO WM_USER+101
- #define WIM_MSG_ERROR_INFO WM_USER+102
- /////////////////////定义结构////////////////////////
- //镜像列表信息
- typedef struct _listinfo
- {
- int index;//索引
- char szName[_MAX_PATH];//镜像名
- char szDiscript[_MAX_PATH];//描述
- }LISTINFO, *LPLISTINFO;
- //驱动器信息
- typedef struct _mydriverinfo
- {
- int index;//索引
- char szDrive[5];//驱动盘符
- char szVolume[20];//卷标
- char szFileSystem[10];//文件系统
- //DWORD64 szSize;
- //DWORD64 szFreeSize;
- float szSize;//容量
- float szFreeSize;//可用空间
- }MYDRIVERINFO, *LPMYDRIVERINFO;
- /////////////////定义结构结束//////////////////////
- ///////////////以下是全局变量定义//////////////////
- HINSTANCE hInst;
- HWND hDlgMainWnd;
- HWND m_progress=NULL;
- HWND hSelDrvWnd=NULL;
- HWND hInstallWnd=NULL;
- HWND m_list,m_listview;
- WCHAR *lpwimfile=new WCHAR[WCHAR_MAX];
- WCHAR *lpath=new WCHAR[WCHAR_MAX];
- char szBootSectFile[MAX_PATH]={0};
- char szBCDTool[MAX_PATH]={0};
- DWORD dwThreadID=0;
- HANDLE hThread;
- HANDLE hWim,hImg;
- DWORD nImageCount,nDriverIndex;
- DWORD nImageIndex; //镜像索引
- char szBootDrive[5]={0}; //启动分区
- char szDestDrive[5]={0}; //目标分区
- bool bFormatDestDrive=false; //是否格式化目标分区
- bool bNT60Sect=true;
- ///////////////////////////////////////////////////
- LISTINFO ListInfo[50]={0};//只处理最多50个镜像
- MYDRIVERINFO mdi[26]={0};//最多支持26个盘符
- ////////////////全局变量定义结束///////////////////
- /////////////////以下是过程函数///////////////////
- BOOL SaveResToFile(char *pFileName,int ResID,char *ResName)
- {
- //该子程序实现将本身程序中含有的资源写出到文件,其中pFileName是指定要写
- //的文件名,ResID是资源的ID号、ResName是资源名称。
- //1.取出资源
- HRSRC hSndResource = FindResource(NULL, MAKEINTRESOURCE(ResID),ResName);
- HGLOBAL hGlobalMem = LoadResource(NULL,hSndResource);
- DWORD dwSize = SizeofResource(NULL,hSndResource);//大小
- LPCTSTR lpMemMain=(LPCSTR)LockResource(hGlobalMem);
- //2.写出资源
- HANDLE hFile=CreateFile(pFileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
- if(INVALID_HANDLE_VALUE==hFile)
- {
- FreeResource(hGlobalMem);
- return FALSE;
- }
- DWORD nBytesWritten;
- int ret=WriteFile(hFile,lpMemMain,dwSize,&nBytesWritten,NULL);
- if (0==ret)
- {
- FreeResource(hGlobalMem);
- return FALSE;
- }
- CloseHandle(hFile);
- //3.释放资源
- FreeResource(hGlobalMem);
- return TRUE;
- }
- ///////////////////////////////////////////////////
- bool IsNT60Bootsect(char *szText)
- {
- if(strstr(szText,"Windows XP")!=NULL)
- {
- return false;
- }
- if(strstr(szText,"Windows 2003")!=NULL)
- {
- return false;
- }
- //返回真表示是NT60的引导,否则就是NT52
- return true;
- }
- ///////////////////////////////////////////////////
- //取磁盘信息
- int GetDiver_Information(LPMYDRIVERINFO mydriverinfo)
- {
- CHAR szLogicalDriveStrings[MAX_PATH];
- PCHAR szDrive;//用来指向字符数组
- //将上面申请的CHar数组用O填充
- ZeroMemory(szLogicalDriveStrings,MAX_PATH);
- //获取逻辑驱动器卷标名 写入数组
- GetLogicalDriveStrings(MAX_PATH-1,szLogicalDriveStrings);
- //指向字符数组
- szDrive=(PCHAR)szLogicalDriveStrings;
- int i=0;
- //循环每个卷
- do
- {
- if(DRIVE_FIXED == GetDriveType(szDrive))//只显示固定盘
- {
- strcpy(mydriverinfo[i].szDrive,szDrive);
- mydriverinfo[i].szDrive[2]='\x0';
- mydriverinfo[i].index=i;
- DWORD aa,bb,cc,dd;
- GetDiskFreeSpace( mydriverinfo[i].szDrive,&aa,&bb,&cc,&dd );
- //mydriverinfo[i].szFreeSize =(DWORD64)aa * (DWORD64)bb * (DWORD64)cc/(1024*1024*1024);
- //mydriverinfo[i].szSize =(DWORD64)aa * (DWORD64)bb * (DWORD64)dd/(1024*1024*1024) ;
- mydriverinfo[i].szFreeSize =(float)aa * bb * cc/(1024*1024*1024);
- mydriverinfo[i].szSize =(float)aa * bb * dd/(1024*1024*1024) ;
- //char Volumelabel[20];
- DWORD SerialNumber;
- DWORD MaxCLength;
- DWORD FileSysFlag;
- //char FileSysName[10];
- GetVolumeInformation( mydriverinfo[i].szDrive,mydriverinfo[i].szVolume,255,&SerialNumber,&MaxCLength,&FileSysFlag,mydriverinfo[i].szFileSystem,255);
- i++;
- }
- szDrive +=(lstrlen(szDrive)+1);
- }
- while(*szDrive!='\x00');
- return i;
- }
- ///////////////////////////////////////////////////
- //宽字节wchar_t* 转换为 单字节char* WCHAR *unicodestr,
- char* Unicode2Ansi(const wchar_t* unicodestr, UINT codepage=CP_ACP)
- {
- int nLen = wcstombs(NULL,unicodestr,0);
- if (nLen == 0)
- {
- return NULL;
- }
- char* pResult = new char[nLen];
- WideCharToMultiByte( codepage, 0, unicodestr, -1, pResult, nLen+1, NULL, NULL );
- return pResult;
- };
- ///////////////////////////////////////////////////
- wchar_t * Ansi2Unicode(char *ansistr )
- {
- int result = 0;
- int needlen = mbstowcs(NULL,ansistr,0);
- if( needlen < 0 )
- {
- return NULL;
- }
- wchar_t *unicodestr=new wchar_t [needlen];
- MultiByteToWideChar(CP_ACP,0, ansistr, -1,unicodestr, needlen+1 );
- return unicodestr;
- }
- ///////////////////////////////////////////////////
- //打开文件对话框
- char * ChooseFile(char szFile[_MAX_PATH])
- {
- OPENFILENAME ofn;
- char strFile[MAX_PATH];
- memset(&ofn,0,sizeof(OPENFILENAME));
- memset(strFile,0,sizeof(char)*MAX_PATH);
- ofn.lStructSize=sizeof(OPENFILENAME);
- ofn.lpstrFilter="WIM镜像文件(*.wim)\0*.wim\0所有文件(*.*)\0*.*\0\0";
- ofn.lpstrFile=szFile;
- ofn.nMaxFile=MAX_PATH;
- ofn.Flags=OFN_FILEMUSTEXIST;
- if(GetOpenFileName(&ofn))
- {
- return szFile;
- }
- else
- {
- return NULL;
- }
- }
- ///////////////////////////////////////////////////
- //实现从XML取镜像信息
- BOOL MyGetWimImageInfo(const WCHAR *ImgInfoBuf,DWORD nIndex,char * szImageName,char * szImageDiscript)
- {
- char *pXML =new char[wcslen(ImgInfoBuf)+1];
- char tmp[260]={0};
- sprintf(tmp,"<IMAGE INDEX="%d">",nIndex);
- pXML=Unicode2Ansi(ImgInfoBuf);
- //取镜像名称、描述等
- char * strpos1=strstr(pXML,tmp);
- if (strpos1==NULL) return FALSE;
- char * strpos2=strstr(strpos1,"<NAME>");
- if(strpos2!=NULL)
- {
- char * strpos3=strstr(strpos1,"</NAME>");
- int len1=strpos3-strpos2-6;
- memcpy(szImageName,strpos2+6,len1);
- memcpy(szImageName+len1,"\x0",1);
- }
- else
- {
- return FALSE;
- }
- //================================
- char * strpos4=strstr(strpos1,"<DESCRIPTION>");
- if(strpos4!=NULL)
- {
- char * strpos5=strstr(strpos4,"</DESCRIPTION>");
- int len2=strpos5-strpos4-13;
- memcpy(szImageDiscript,strpos4+13,len2);
- memcpy(szImageDiscript+len2,"\x0",1);
- }
- else
- {
- szImageDiscript=NULL;
- }
- return TRUE;
- }
- ///////////////////////////////////////////////////
- int MyShell(const char * cmdline,char *strOutput)
- {
- //返回0表示执行成功
- SECURITY_ATTRIBUTES sa;
- HANDLE hRead,hWrite;
-
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.lpSecurityDescriptor = NULL; //使用系统默认的安全描述符
- sa.bInheritHandle = TRUE; //创建的进程继承句柄
- if (!CreatePipe(&hRead,&hWrite,&sa,0)) //创建匿名管道
- {
- return 1;
- }
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si,sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- GetStartupInfo(&si);
- si.hStdError = hWrite;
- si.hStdOutput = hWrite; //新创建进程的标准输出连在写管道一端
- si.wShowWindow = SW_HIDE; //隐藏窗口
- si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
- if (!CreateProcess(NULL,(char *)cmdline,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) //创建子进程
- {
- //MessageBox("执行程序失败!","提示",MB_OK | MB_ICONWARNING);
- return 2;
- }
- CloseHandle(hWrite); //关闭管道句柄
- char buffer[4096] = {0};
- DWORD bytesRead;
-
- while (true)
- {
- if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL) //读取管道
- break;
-
- strcat(strOutput,buffer);
- Sleep(100);
- }
- CloseHandle(hRead);
- return 0;
- }
- ////////////////////////////////////////////////////
- void MyExitProcess()
- {
- WIMCloseHandle (hWim);
- ExitProcess(0);
- }
- ///////////////////////////////////////////////////
- //WIM回调消息
- DWORD CALLBACK MyWIMMessageCallback(DWORD dwMessageId,WPARAM wParam,LPARAM lParam,LPVOID lpvUserData)
- {
- HWND hWnd=(HWND)lpvUserData;
- switch (dwMessageId)
- {
- case WIM_MSG_ALIGNMENT:
- break;
- case WIM_MSG_COMPRESS:
- break;
- case WIM_MSG_ERROR:
- PostMessage(hWnd,WIM_MSG_ERROR_INFO,wParam,lParam);
- break;
- case WIM_MSG_PROCESS:
- break;
- case WIM_MSG_SCANNING:
- break;
- case WIM_MSG_SETPOS:
- break;
- case WIM_MSG_SETRANGE:
- break;
- case WIM_MSG_PROGRESS:
- {
- //UINT dwPercent =(UINT)wParam;
- //UINT dwTicksRemaining=(UINT)lParam;
- PostMessage(hWnd,WIM_PROGRESS_INFO,wParam,lParam);
- break;
- }
- default:
- break;
- }
- return WIM_MSG_SUCCESS;
- }
- ///////////////////////////////////////////////////
- //工作线程
- DWORD WINAPI MyThread(PVOID lParam)
- {
- char szApplyPath[MAX_PATH]={0};
- strcpy(szApplyPath,szDestDrive);
- strcat(szApplyPath,"\");
- if(WIMApplyImage(hImg,Ansi2Unicode(szApplyPath),WIM_FLAG_VERIFY))
- {
- WIMCloseHandle(hImg);
- PostMessage(hInstallWnd,WIM_APPLY_FINISH,0,0);
- }
- WIMUnregisterMessageCallback(hWim,(FARPROC)MyWIMMessageCallback);
- return 0;
- }
- ///////////////////////////////////////////////////
- BOOL CALLBACK DlgInstallProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
- {
- hInstallWnd=hDlg;
- WORD wmId,wmEvent;
- switch(message)
- {
- case WIM_MSG_ERROR_INFO:
- MessageBox(hDlg,"解压出现错误","错误",MB_OK|MB_ICONERROR);
- return FALSE;
- case WIM_PROGRESS_INFO:
- {
- UINT dwPercent =(UINT)wParam;
- UINT dwTicksRemaining=(UINT)lParam;
- PostMessage(m_progress,PBM_SETPOS,wParam,0);
- char szInfo[200]={0};
- int MN,SC;
- MN=dwTicksRemaining/1000/60;
- SC=dwTicksRemaining/1000%60;
- if (!MN && !SC && dwPercent < 100)
- sprintf(szInfo,"解压进度%3d%% ,剩余时间:计算中",dwPercent);
- else
- sprintf(szInfo,"解压进度%3d%% ,剩余时间:%d分%d秒",dwPercent,MN,SC);
- SetDlgItemText(hDlg,IDC_LBPROGRESSINFO,szInfo);
- return FALSE;
- }
- case WIM_APPLY_FINISH:
- {
- char szBuffer[4096]={0};
- char szCmdLine[200];
- CloseHandle(hThread);
- SetDlgItemText(hDlg,IDC_LBSTEP2,"2. 展开数据到分区 OK");
- //修复引导扇区
- SetDlgItemText(hDlg,IDC_LBSTEP3,"3. 修复引导信息 ...");
-
- if (bNT60Sect)
- sprintf(szCmdLine,"cmd /c "%s /nt60 %s"",szBootSectFile,szBootDrive);
- else
- sprintf(szCmdLine,"cmd /c "%s /nt52 %s"",szBootSectFile,szBootDrive);
-
- //MessageBox(hDlg,szCmdLine,"",0);
- if(0==MyShell((const char *)szCmdLine,szBuffer))
- {
- //MessageBox(hDlg,szBuffer,"",0);
- if(strstr(szBuffer,"Bootcode was successfully updated on all targeted volumes."))
- SetDlgItemText(hDlg,IDC_LBSTEP3,"3. 修复引导信息 OK");
- else
- MessageBox(hDlg,"修复引导信息失败","错误",MB_OK|MB_ICONERROR);
- }
- else
- {
- MessageBox(hDlg,"修复引导信息失败","错误",MB_OK|MB_ICONERROR);
- }
- //修复引导文件
- //NT60则用BCDBOOT修复
-
- if (bNT60Sect)
- {
- SetDlgItemText(hDlg,IDC_LBSTEP4,"4. 修复引导文件 ...");
- sprintf(szCmdLine,"cmd /c "%s %s\\WINDOWS /s %s /l zh-CN"",szBCDTool,szDestDrive,szBootDrive);
- if(0==MyShell((const char *)szCmdLine,szBuffer))
- {
- if(strstr(szBuffer,"已成功创建启动文件"))
- SetDlgItemText(hDlg,IDC_LBSTEP4,"4. 修复引导文件 OK");
- else
- MessageBox(hDlg,"修复引导文件失败","错误",MB_OK|MB_ICONERROR);
- }
- else
- {
- MessageBox(hDlg,"修复引导文件失败","错误",MB_OK|MB_ICONERROR);
- }
- }
- else
- {
- SetDlgItemText(hDlg,IDC_LBSTEP4,"4. 修复引导文件 NG");
- }
- SetDlgItemText(hDlg,IDC_LBSTEP5,"-> 5.准备重启");
- if(IDYES==MessageBox(hDlg,"解压文件完成,是否要重启进入安装过程?","提示",MB_YESNO|MB_ICONQUESTION))
- {
- WinExec("shutdown -r -t 0",SW_HIDE);
- }
- SendMessage(hDlg,WM_CLOSE,0,0);
- return FALSE;
- }
- case WM_SHOWWINDOW:
- {
- char szTmp[4096]={0};
- char szInfo[4096]={0};
- sprintf(szTmp,"镜像文件:%s\n\n",Unicode2Ansi(lpwimfile));
- strcat(szInfo,szTmp);
- sprintf(szTmp,"镜像编号:%d\n\n",nImageIndex);
- strcat(szInfo,szTmp);
- sprintf(szTmp,"镜像名称:%s\n\n",ListInfo[nImageIndex].szName);
- strcat(szInfo,szTmp);
- sprintf(szTmp,"镜像描述:%s\n\n",ListInfo[nImageIndex].szDiscript);
- strcat(szInfo,szTmp);
- sprintf(szTmp,"目标分区:%s\n\n",szDestDrive);
- strcat(szInfo,szTmp);
- if(bFormatDestDrive)
- strcat(szInfo,"格式化目标分区:是\n\n");
- else
- strcat(szInfo,"格式化目标分区:否\n\n");
- sprintf(szTmp,"启动分区:%s\n\n",szBootDrive);
- strcat(szInfo,szTmp);
- bNT60Sect=IsNT60Bootsect(ListInfo[nImageIndex].szName);
- if(bNT60Sect)
- strcat(szInfo,"引导类型:NT60\n\n");
- else
- strcat(szInfo,"引导类型:NT52\n\n");
- SetDlgItemText(hDlg,IDC_LBINFORMATION,szInfo);
- return FALSE;
- }
- case WM_INITDIALOG:
- {
- m_progress=GetDlgItem(hDlg,IDC_PROGRESS1);
- SendMessage(m_progress,PBM_SETRANGE,0,MAKELPARAM(0,100));
- SendMessage(m_progress,PBM_SETSTEP,1,0);
-
- return FALSE;
- }
-
- case WM_COMMAND:
- {
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- switch (wmId)
- {
- case IDC_BTNNEXT3:
- if(IDYES==MessageBox(hDlg,"是否开始解压文件?","提示",MB_YESNO|MB_ICONQUESTION))
- {
- if(bFormatDestDrive) SHFormatDrive(hDlg,szDestDrive[0]-'A',SHFMT_ID_DEFAULT,SHFMT_OPT_FULL);
- SetDlgItemText(hDlg,IDC_LBSTEP1,"1. 准备环境 ...");
- //设置回调消息过程
- if(INVALID_CALLBACK_VALUE!=WIMRegisterMessageCallback(hWim,(FARPROC)MyWIMMessageCallback,(LPVOID)hInstallWnd))
- {
- //MessageBox(hDlg,"注册成功","",MB_OK);
- SetDlgItemText(hDlg,IDC_LBSTEP1,"1. 准备环境 OK");
- char szTemp[MAX_PATH]={0};
- GetTempPath(MAX_PATH,szTemp);
- WIMSetTemporaryPath(hWim,Ansi2Unicode(szTemp));
- hImg=WIMLoadImage(hWim,nImageIndex);
- if(hImg)
- {
- SECURITY_ATTRIBUTES sa={0};
- hThread=CreateThread(&sa,NULL,(LPTHREAD_START_ROUTINE)MyThread,NULL,0,&dwThreadID);
- EnableWindow(GetDlgItem(hDlg,IDC_BTNNEXT3),FALSE);
- EnableWindow(GetDlgItem(hDlg,IDC_BTNPREP2),FALSE);
- SetDlgItemText(hDlg,IDC_LBSTEP2,"2. 展开数据到分区 ...");
- SetDlgItemText(hDlg,IDC_LBPROGRESSINFO,"解压进度 0% ,剩余时间:计算中");
- }
- else
- {
- MessageBox(hDlg,"载入镜像失败","提示",MB_OK|MB_ICONERROR);
- //GetLastError();
- }
- }
- else
- {
- MessageBox(hDlg,"注册回调消息失败","提示",MB_OK|MB_ICONERROR);
- }
- }
- return FALSE;
- case IDC_BTNCLOSE3:
- SendMessage(hDlg,WM_CLOSE,0,0);
- return FALSE;
- case IDC_BTNPREP2:
- ShowWindow(hDlg,SW_HIDE);
- ShowWindow(hSelDrvWnd,SW_NORMAL);
- SetFocus (hSelDrvWnd);
- return FALSE;
- }
- }
- case WM_CLOSE:
- {
- DWORD code;
- GetExitCodeThread(hThread,&code);
- if(code==STILL_ACTIVE)
- {
- if(IDYES==MessageBox(hDlg,"解压正在进行中,是否要退出?","提示",MB_YESNO|MB_ICONQUESTION))
- {
- DestroyWindow(hDlg);
- MyExitProcess();
- }
- }
- else
- {
- DestroyWindow(hDlg);
- MyExitProcess();
- }
- return FALSE;
- }
- default:
- return FALSE;
- }
- return FALSE;
- }
- ///////////////////////////////////////////////////
- BOOL CALLBACK DlgSelDriveProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
- {
- hSelDrvWnd=hDlg;
- WORD wmId,wmEvent;
- DWORD nItem=0;
- switch(message)
- {
- case WM_SHOWWINDOW:
- {
-
- return FALSE;
- }
- case WM_INITDIALOG:
- {
- m_listview=GetDlgItem(hDlg,IDC_LISTVIEW);
- //加入驱动器列表
- ListView_DeleteAllItems(m_listview);
- LV_ITEM item; // 项
- LV_COLUMN colmn; // 列
- ZeroMemory(&item, sizeof(LV_ITEM));
- ZeroMemory(&colmn, sizeof(LV_COLUMN));
- colmn.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // 风格
- colmn.cx = 0x60;
- colmn.pszText = "盘符"; // 文字
- colmn.iOrder=0;
- // 后面列
- SendMessage(m_listview, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);
- colmn.cx = 0x80;
- colmn.pszText = "卷标";
- SendMessage(m_listview, LVM_INSERTCOLUMN, 1, (LPARAM)&colmn);
- colmn.cx = 0x80;
- colmn.pszText = "文件系统";
- SendMessage(m_listview, LVM_INSERTCOLUMN, 2, (LPARAM)&colmn);
- colmn.cx = 0x80;
- colmn.pszText = "容量";
- SendMessage(m_listview, LVM_INSERTCOLUMN, 3, (LPARAM)&colmn);
- colmn.cx = 0x80;
- colmn.pszText = "可用空间";
- SendMessage(m_listview, LVM_INSERTCOLUMN, 4, (LPARAM)&colmn);
-
- int nDrvCount=GetDiver_Information(mdi);
- for (int i=0;i<nDrvCount;i++)
- {
- SendMessage(m_listview, LVM_GETITEMCOUNT, 0, (LPARAM)&colmn);
- item.mask = LVIF_TEXT; // 文字
- item.cchTextMax = MAX_PATH; // 文字长度
- item.iItem = ListView_GetItemCount(m_listview);
- item.iSubItem = 0;
- item.pszText = mdi[i].szDrive ;
- //插入行
- ListView_InsertItem(m_listview, &item);
- //第二列文本
- ListView_SetItemText(m_listview,i,1,mdi[i].szVolume);
- //第三列文本
- ListView_SetItemText(m_listview,i,2,mdi[i].szFileSystem);
- char tmp[64];
- sprintf(tmp,"%10.1fG",mdi[i].szSize);
- //第四列文本
- ListView_SetItemText(m_listview,i,3,tmp);
- sprintf(tmp,"%10.1fG",mdi[i].szFreeSize);
- //第五列文本
- ListView_SetItemText(m_listview,i,4,tmp);
- }
- return FALSE;
- }
-
- case WM_COMMAND:
- {
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- switch (wmId)
- {
- case IDC_BTNNEXT2:
- {
- bFormatDestDrive=SendMessage(GetDlgItem(hDlg,IDC_CHKFORMAT),BM_GETCHECK,0,0);
- if(!hInstallWnd)
- {
-
- //MessageBox(hDlg,szDestDrive,"",0);
- ShowWindow(hDlg,SW_HIDE);
- DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_INSTALLFORM),NULL,(DLGPROC)DlgInstallProc,NULL);
- }
- else
- {
- ShowWindow(hDlg,SW_HIDE);
- ShowWindow(hInstallWnd,SW_SHOW);
- SetFocus (hInstallWnd);
- return FALSE;
- }
- }
- case IDC_BTNPREP1:
- {
- ShowWindow(hDlg,SW_HIDE);
- ShowWindow(hDlgMainWnd,SW_SHOW);
- SetFocus (hDlgMainWnd);
- return FALSE;
- }
- case IDC_BTNSETBOOT:
- {
-
- strcpy(szBootDrive,mdi[nDriverIndex].szDrive);
- char szInfo[100]={0};
- wsprintf(szInfo,"当前选择的目标盘是%s ,当前引导盘为%s",szDestDrive,szBootDrive);
- SetDlgItemText(hDlg,IDC_LBBOOTINFO,szInfo);
- return FALSE;
- }
- case IDC_BTNCLOSE2:
- {
- SendMessage(hDlg,WM_CLOSE,0,0);
- return FALSE;
- }
- default:
- return FALSE;
- }
- }
- case WM_NOTIFY:
- switch (((LPNMHDR)lParam)->code)
- {
- case LVN_ITEMCHANGED:
- {
- NMLISTVIEW *pnmv = (NMLISTVIEW*)lParam;
- if (pnmv->uNewState & LVIS_SELECTED)
- {
- nDriverIndex=pnmv->iItem;
- strcpy(szDestDrive,mdi[nDriverIndex].szDrive);
- char szInfo[100]={0};
- wsprintf(szInfo,"当前选择的目标盘是%s ,当前引导盘为%s",szDestDrive,szBootDrive);
- SetDlgItemText(hDlg,IDC_LBBOOTINFO,szInfo);
- if(nItem!=-1)
- {
- EnableWindow(GetDlgItem(hDlg,IDC_BTNNEXT2),TRUE);
- EnableWindow(GetDlgItem(hDlg,IDC_BTNSETBOOT),TRUE);
- }
- }
- return FALSE;
- }
- default:
- return FALSE;
- }
- case WM_CLOSE:
- DestroyWindow(hDlg);
- MyExitProcess();
- return FALSE;
- default:
- return FALSE;
- }
- return FALSE;
- }
- ///////////////////////////////////////////////////
- BOOL CALLBACK DlgMainProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
- {
- hDlgMainWnd=hDlg;
- DWORD dwCreationResult,nItem;
- WORD wmId,wmEvent;
- switch(message)
- {
- case WM_INITDIALOG:
- {
- m_list=GetDlgItem(hDlg,IDC_LIST1);
- char szTmp[MAX_PATH]={0};
- GetTempPath(MAX_PATH,szTmp);
- strcpy(szBootSectFile,szTmp);
- strcat(szBootSectFile,"bootsect.exe");
- strcpy(szBCDTool,szTmp);
- strcat(szBCDTool,"bcdboot.exe");
- //MessageBox(hDlg,szBootSectFile,"",0);
- //MessageBox(hDlg,szBCDTool,"",0);
- SaveResToFile(szBCDTool,IDR_BCDTOOL,"TOOLS");
- SaveResToFile(szBootSectFile,IDR_BOOTSECT,"TOOLS");
- return FALSE;
- }
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- switch (wmId)
- {
- case IDC_BTNNEXT1:
- {
- nImageIndex=SendMessage(m_list,LB_GETCURSEL,0,0)+1;
- //显示下一个选择驱动窗口
- if(!hSelDrvWnd)
- {
- ShowWindow(hDlg,SW_HIDE);
- DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_SELECTDRIVEFORM),NULL,(DLGPROC)DlgSelDriveProc,NULL);
- }
- else
- {
- ShowWindow(hDlg,SW_HIDE);
- ShowWindow(hSelDrvWnd,SW_SHOW);
- SetFocus (hSelDrvWnd);
- }
- return FALSE;
- }
- case IDC_BTNCHOOSE:
- {
- char szPathName[_MAX_PATH]={0};
- if(NULL!=ChooseFile(szPathName))//打开文件
- {
- if(hWim)
- {
- WIMCloseHandle(hWim);
- hWim=NULL;
- }
- lpwimfile=Ansi2Unicode(szPathName);
- SetDlgItemText(hDlg,IDC_EDIT1,szPathName);
- WCHAR * ImgInfoBuf = new WCHAR(WCHAR_MAX);
- DWORD ImgInfoBufSize;
- hWim=WIMCreateFile(lpwimfile,WIM_GENERIC_READ,WIM_OPEN_EXISTING,WIM_FLAG_VERIFY,WIM_COMPRESS_XPRESS,&dwCreationResult);
- if(hWim==0)
- {
- MessageBox(hDlg,"打开WIM文件失败!","错误",MB_OK|MB_ICONERROR);
- return FALSE;
- }
- SendMessage(m_list,LB_RESETCONTENT,0,0);
- nImageCount=WIMGetImageCount(hWim);//取得镜像数
- if(nImageCount>50) nImageCount=50;//最多只显示50个镜像
- WIMGetImageInformation(hWim, (LPVOID *)&ImgInfoBuf,&ImgInfoBufSize);//取WIM信息
- char *szName=new char[260];
- char *szDiscript=new char[260];
- memset (ListInfo,0,sizeof(ListInfo));
- //MessageBoxW(hDlg,ImgInfoBuf,L"",MB_OK);
- for(DWORD index=1;index<=nImageCount;index++)
- {
- MyGetWimImageInfo(ImgInfoBuf,index,szName,szDiscript);
- ListInfo[index].index=index;
- strcpy(ListInfo[index].szName,szName);
- strcpy(ListInfo[index].szDiscript,szDiscript);
- SendMessage(m_list,LB_ADDSTRING,0,(LPARAM)szName);
- }
- }
- return FALSE;
- }
- case IDC_BTNCLOSE1:
- {
- SendMessage(hDlg,WM_CLOSE,0,0);
- return FALSE;
- }
- case IDC_LIST1:
- {
- switch (wmEvent)
- {
- case LBN_SELCHANGE:
- {
- nItem=SendMessage(m_list,LB_GETCURSEL,0,0);
- //显示描述
- SetWindowText(GetDlgItem(hDlg,IDC_STATIC1),ListInfo[nItem+1].szDiscript);
- //下一步可用
- if(nItem!=-1)
- {
- nImageIndex=nItem;
- EnableWindow(GetDlgItem(hDlg,IDC_BTNNEXT1),TRUE);
- }
- return FALSE;
- }
- default:
- return FALSE;
- }
- }
- default:
- return FALSE;
- }
- case WM_CLOSE:
- DestroyWindow(hDlg);
- MyExitProcess();
- return FALSE;
- default:
- return FALSE;
- }
- return FALSE;
- }
- ///////////////////////////////////////////////////
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- hInst=hInstance;
- strcpy(szBootDrive,"C:");
- DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_MAINFORM),NULL,(DLGPROC)DlgMainProc,NULL);
- return 0;
- }
复制代码
[ 本帖最后由 fly2sky 于 2012-4-2 21:11 编辑 ] |
|