A system Backup tool made using Windows API+QT

Source: Internet
Author: User
Tags emit

A system Backup tool made using Windows API+QT

Because the company used a large number of installed equipment, the previous use of Ghost software to deal with, but since the UEFI mode has not been very good application, although there is now Acronis software replacement, but the resulting backup file does not have a good tool to modify, So there is a lot of trouble when you need to add mirror drivers and patches. The DISM tool that comes with the system does work, but each time you need to enter command line input, the efficiency is not improved. Therefore, according to the actual situation, qingming three days false even learn to do this window version of the Backup and Restore tool, the functionality is very simple, only the backup partition (folder) and restore partition function, the next step will add the driver add function. Nonsense not to say, on the code.

 //DismTools.h  qt widget  header files   #pragma  once#include <qwidget># include <qthread> #include   "ui_dismtools.h" #include  <QString> #include  < qlabel> #include   "BackUp.h" #include   "Restore.h" #include   "BcdBoot.h" class dismtools  :  public qwidget{q_objectpublic:dismtools (QWIDGET&NBSP;*PARENT&NBSP;=&NBSP;Q_NULLPTR); ~DismTools () { Thread.quit (); thread.wait ();} Private:ui::D ismtoolsclass ui;private slots:void on_backup_clicked (); void on_BackUp_ Finished (const qstring&); void on_restore_clicked (); Void on_restore_finished (const  qstring&);p rivate:qthread thread; BACKUP*BK; restore *restore; Bcdboot *boot;signals:void backsig (const qstring &,const qstring &); void  restoresig (const qstring &, const qstring &); Void bcdbootSig (const  QString &);//pwstr&nbSp;psztmpdir = l "c:\\tmp";}; 
dismtools.cpp  Code #pragma execution_character_set ("Utf-8") #include   "DismTools.h" #include   " BackUp.h "#include  <QFileInfo> #include  <qfiledialog>dismtools::D ismtools (qwidget * Parent):  qwidget (parent) {UI.SETUPUI (this); Connect (Ui.backup, &qpushbutton::clicked, this,  &dismtools::on_backup_clicked); Connect (ui.restore, &qpushbutton::clicked, this,  &dismtools::on_restore_clicked);} Void dismtools::on_backup_clicked () {qstring dir = qfiledialog::getexistingdirectory (this, &NBSP;TR ("Select Backup Partition"), "/Home", Qfiledialog::showdirsonly| qfiledialog::D ontresolvesymlinks); Qfileinfo fi (dir);if  (!fi.exists ()) {return;} Qstring filename = qfiledialog::getsavefilename (this, qstring ("Save image File"), "/home/", QString (" Image file   (*.wim))); QFILEINFO&NBSP;FI1 (FileName); Bk = new backup (); Bk->movetothread (&thread); Connect (& Thread, &qthread::finishEd, bk, &qobject::d eletelater); Connect (this, &dismtools::backsig, bk, & BackUp::d obackup); Connect (bk, &backup::resultready, this, &dismtools::on_backup_ finished); Thread.Start (); Ui.label->settext ("Mirror backup ..."); ui.backup->setdisabled (true); Emit backsig ( Filename, dir);} Void dismtools::on_backup_finished (const qstring &finished) {ui.label->setText (finished); Qmessagebox::warning (this,  "",  finished); ui.backup->setdisabled (false);} Void dismtools::on_restore_clicked () {qstring dir = qfiledialog::getexistingdirectory (this, &NBSP;TR ("Select Recovery Partition"), "/Home", Qfiledialog::showdirsonly| qfiledialog::D ontresolvesymlinks); Qfileinfo fi (dir);if  (!fi.exists ()) {return;} Qstring filename = qfiledialog::getopenfilename (this, qstring ("Select Image File"), "/home/", QString (" Image file   (*.wim))); QFILEINFO&NBSP;FI1 (FileName); Restore = new restore (); restore-&Gt;movetothread (&thread); Connect (&thread, &qthread::finished, restore, &qobject ::d eletelater); Connect (this, &dismtools::restoresig, restore, &restore::d orestore); Connect (restore, &restore::resultready, this, &dismtools::on_restore_finished); Thread.Start (); Ui.label->settext ("Mirror recovery ..."); ui.restore->setdisabled (true); Emit restoresig (fileName ,  dir);} Void dismtools::on_restore_finished (const qstring &finished) {Ui.label->setText (finished) ; Qmessagebox::warning (this,  "",  finished); ui.restore->setdisabled (false);int ret =  Qmessagebox::question (this,  "",  qstring ("Do you want to add a boot entry?") "),  qmessagebox::ok,qmessagebox::no);if  (ret = 0) {qstring souces =  Qfiledialog::getexistingdirectory (THIS,&NBSP;TR ("Select Windows system Root"), "/home",qfiledialog::showdirsonly|  Qfiledialog::D ontresolvesymlinks); Qstring bootpath = qfiLedialog::getexistingdirectory (THIS,&NBSP;TR ("Select Windows boot Partition"), "/home",qfiledialog::showdirsonly|  Qfiledialog::D ontresolvesymlinks); Boot = new bcdboot (); Boot->dobcdboot (Souces, bootPath Connect (boot, &bcdboot::resultready, this, [=] (const qstring &result) { Qmessagebox::information (this,  "",  result);}); return;}} Backup Code #pragma once#pragma execution_character_set ("Utf-8") #include  <qobject.h># include <qstring> #include  <QMessageBox> #include   "windows.h" #include   "wimgapi.h "Class backup:public qobject{q_objectpublic slots:void dobackup (const QString  &pszwimfile,const qstring &pszcapturedir); Signals:void resultready (const QString  &result);};

         

backup.cpp#include  "BackUp.h" #include  <qmessagebox>void backup::d obackup (const  Qstring &pszwimfile, const qstring &pszcapturedir) {bool sucess =  True;const wchar_t * wimfile = reinterpret_cast<const wchar_t *> ( PSZWIMFILE.UTF16 ()); Const wchar_t * capturedir = reinterpret_cast<const wchar _t *> (Pszcapturedir.utf16 ());D Word dwflags = 0,dwdisposition = wim_create_ always,dwdesiredaccess = wim_generic_write,dwcreateflags = 0,dwcaptureflags =  wim_flag_verify,dwcompressiontype = wim_compress_lzx,dwcreationresult = 0,dwerror =  0; Handle    hwim = wimcreatefile (Wimfile,dwdesiredaccess,dwdisposition, Dwcreateflags,dwcompressiontype,&dwcreationresult);sucess = hwim;if  (!sucess) {//QMessageBox :: Critical (this, qstring("error!"),  qstring ("Backup error with code create fail"));//QMessageBox:: Critical (null, qstring ("error!"),  qstring ("Create image fail"); Emit resultready ( QString ("File creation failed"); return;} Wimsettemporarypath (Hwim, psztmpdir); Handle himage = wimcaptureimage (hwim, capturedir, dwcaptureflags);sucess =  himage;if  (!sucess) {//qmessagebox::critical (null, qstring ("error!"),  qstring ("BackUp Error  with code backup status ")); Emit resultready (QString (" Backup Failed "); return;} Wimclosehandle (Himage); Wimclosehandle (Hwim); Emit resultready (QString ("Backup succeeded");}
restore.h  Code #pragma once#pragma execution_character_set ("Utf-8") #include  <QObject> #include  <QString> #include   "Windows.h" #include   "wimgapi.h" class restore :  Public qobject{q_objectpublic slots:void dorestore (Const qstring &pszwimfile,  const qstring &pszcapturedir); Signals:void resultready (const QString & result);};/ /restore.cpp#include  "Restore.h" Void restore::d orestore (Const qstring &pszwimfile,  const qstring &pszcapturedir) {bool sucess = true;const wchar_t *  WimFile = reinterpret_cast<const wchar_t *> (Pszwimfile.utf16 ());const  Wchar_t * pszapplydir = reinterpret_cast<const wchar_t *> ( PSZCAPTUREDIR.UTF16 ()); Handle hwim = null,himage = null;dword  dwcreateflags = 0, Dwcreateresult = 0,dwimageindex = 1,dwerror = 0; Wim_info wiminfo = { 0 };hwim = wimcreatefile (WimFile,           // existing .wim file to append the  image toWIM_GENERIC_READ,    // Access modeWIM_OPEN_EXISTING,    // Open dispositiondwCreateFlags,0,                    // Compression type is  Ignored for wim_open_existing.&dwcreateresult);sucess = hwim;if  (!sucess) {// Qmessagebox::critical (this, qstring ("error!"),  qstring ("backup error with code  Create fail ")//qmessagebox::critical (null, qstring (" error! "),  qstring (" Create Image  fail ")); Emit resultready (QString (" Mirror read failed "); return;} Wimgetattributes(Hwim, &wiminfo, sizeof (Wiminfo));if  (Wiminfo.imagecount < dwimageindex) {Emit  resultready (QString ("Mirror read failed"); return;} Wimsettemporarypath (Hwim, pszapplydir); Himage = wimloadimage (hwim, dwimageindex); sucess  = hImage;if  (!sucess) {//qmessagebox::critical (null, qstring ("error!"),  qstring (" Backup error with code backup status ")); Emit resultready (QString (" Cannot get Mirror Index ")); return;} Wimapplyimage (Himage, pszapplydir, wim_flag_fileinfo);//sucess = wimapplyimage (HImage,  pszapplydir, wim_flag_no_apply);if  (!sucess) {//qmessagebox::critical (NULL, QString ("Error ! "),  qstring (" Backup error with code backup status ")); Emit resultready ( QString ("Mirror restore Failed"); return;} Wimclosehandle (Himage); Wimclosehandle (Hwim); Emit resultready (QString ("Mirror Restore Succeeded");}

BCDboot code is not affixed, simple are not embarrassed ...


Accessories have detailed code test environment VS2017+QT msvc2017 +WADK Environment, because of the production of internal use, so the interface is ugly, there is time to optimize it


Run


Attachment Link: Code

A system Backup tool made using Windows API+QT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.