WetCode

Untitled

Dec 6th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.22 KB | None | 0 0
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2013-11-28T16:52:43
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT       += core gui
  8. QT       += webkit
  9. QT += webkitwidgets
  10.  
  11. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  12.  
  13. TARGET = Iconchange
  14. TEMPLATE = app
  15.  
  16. SOURCES += main.cpp\
  17.         mainwindow.cpp \
  18.     IconChanger.cpp
  19.  
  20. HEADERS  += mainwindow.h \
  21.             IconChanger.h
  22.  
  23. FORMS    += mainwindow.ui
  24.  
  25. RESOURCES += \
  26.     res.qrc
  27.  
  28. DEFINES -= UNICODE
  29. DEFINES -= _UNICODE
  30. DEFINES += _MBCS
  31. DEFINES += MBCS
  32.  
  33. CONFIG += gui
  34. CONFIG += console
  35.  
  36.  
  37.  
  38. IconChanger.cpp
  39.  
  40. #include "IconChanger.h"
  41. #include <iostream>
  42. using namespace std;
  43. #include <QMessageBox>
  44. #include <QDebug>
  45.  
  46. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  47. {
  48.     if (!IS_INTRESOURCE(lpName))
  49.     {
  50.  
  51.         cout << "lpType in Names: " << lpType << endl;
  52.         cout << "lpType in Names: " << lpName << endl;
  53.  
  54.     } else {
  55.         if ( lpType == RT_ICON )
  56.         {
  57.             char *cFilePath = new char[MAX_PATH];
  58.             GetModuleFileName( hModule, cFilePath, MAX_PATH);
  59.  
  60.             cout << "lpType in Names: " << lpType << endl;
  61.             cout << "lpType in Names: " << lpName << endl;
  62.  
  63.             HANDLE hCleanExe = NULL;
  64.             hCleanExe = BeginUpdateResource(cFilePath, FALSE);
  65.             if ( hCleanExe == INVALID_HANDLE_VALUE )
  66.             {
  67.                 QMessageBox mess;
  68.                 mess.setText(QString("Begin Clean Up FAILED! - ").append(QString::number(GetLastError())));
  69.                 mess.exec();
  70.                 return FALSE;
  71.             }
  72.  
  73.             if ( !UpdateResource( hCleanExe, lpType, lpName, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL), NULL, 0) )
  74.             {
  75.                 QMessageBox mess;
  76.                 mess.setText(QString("Cleaning Update FAILED! - ").append(QString::number(GetLastError())));
  77.                 mess.exec();
  78.                 return FALSE;
  79.             }
  80.  
  81.             if ( !EndUpdateResource( hCleanExe, FALSE) )
  82.             {
  83.                 QMessageBox mess;
  84.                 mess.setText(QString("End Cleanup FAILED! - ").append(QString::number(GetLastError())));
  85.                 mess.exec();
  86.                 return FALSE;
  87.             }
  88.         }
  89.  
  90.     }
  91.  
  92.    return TRUE;
  93. }
  94.  
  95. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  96. {
  97.     if (!IS_INTRESOURCE(lpType))
  98.     {
  99.         cout << "lpType:" << lpType << endl;
  100.         if ( lpType == (RT_ICON + 11) )
  101.         {
  102.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  103.         }
  104.  
  105.     } else {
  106.         cout << "lpType:" << lpType << endl;
  107.         if ( lpType == (RT_ICON + 11) )
  108.         {
  109.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  110.         }
  111.     }
  112.  
  113.    return true;
  114. }
  115.  
  116.  
  117. IconChanger::IconChanger() // Constructor
  118. {
  119.     hExeSource = NULL;
  120.     hrResSource = NULL;
  121.     hGlobSource = NULL;
  122.     lpIconBuffer = NULL;
  123. }
  124.                                                             // Retrive Icon Buffer
  125. void IconChanger::GetIconFromSource(QString sIconPath, QString sExePath) // from source Exe/DLL
  126. {
  127.     LPICONDIRENTRY pIconDirEntry(NULL);
  128.     LPGRPICONDIR pGrpIconDir(NULL);
  129.     HEADER header;
  130.     LPBYTE pIconBytes(NULL);
  131.     HANDLE hIconFile(NULL);
  132.     DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  133.     HANDLE hUpdate(NULL);
  134.     BOOL ret(FALSE);
  135.     WORD i(0);
  136.     //sIconPath.replace("\\", "\\\\", Qt::CaseSensitive);
  137.    // sExePath.replace("\\", "\\\\", Qt::CaseSensitive);
  138.  
  139.     hExeSource = LoadLibrary( sExePath.toStdString().c_str() );
  140.     if ( hExeSource == NULL )
  141.     {
  142.         QMessageBox mess; mess.setText("Unable to get handle to EXE/DLL\n" + sExePath );
  143.         mess.exec();
  144.     }
  145.  
  146.     hIconFile = CreateFile( sIconPath.toStdString().c_str(), GENERIC_READ,
  147.                             0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  148.     if ( hIconFile == INVALID_HANDLE_VALUE )
  149.     {
  150.         QMessageBox mess; mess.setText("Unable to open file.\n" + sIconPath );
  151.         mess.exec();
  152.     }
  153.  
  154.     ret = ReadFile( hIconFile, &header, sizeof(HEADER), &dwReserved, NULL );
  155.     if (!ret)
  156.     {
  157.         CloseHandle(hExeSource);
  158.         QMessageBox mess; mess.setText("Unable to Read Icon File " + sIconPath );
  159.         mess.exec();
  160.         return;
  161.     }
  162.  
  163.     pIconDirEntry = (LPICONDIRENTRY)new BYTE[ header.idCount * sizeof(ICONDIRENTRY) ];
  164.     if ( pIconDirEntry == NULL )
  165.     {
  166.         CloseHandle(hIconFile);
  167.         QMessageBox mess; mess.setText("Unable to allocate memory for Icon Group Header" + sIconPath );
  168.         mess.exec();
  169.         return;
  170.     }
  171.  
  172.     ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
  173.     if (!ret)
  174.     {
  175.         delete[] pIconDirEntry;
  176.         CloseHandle(hIconFile);
  177.         QMessageBox mess; mess.setText("Unable to read Icon Dir Entrys" + sIconPath );
  178.         mess.exec();
  179.         return;
  180.     }
  181.  
  182.     nGSize = sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
  183.     pGrpIconDir = (LPGRPICONDIR)new BYTE[nGSize];
  184.     ZeroMemory( pGrpIconDir, nSize );
  185.  
  186.     pGrpIconDir->idReserved = header.idReserved;
  187.     pGrpIconDir->idType = header.idType;
  188.     pGrpIconDir->idCount = header.idCount;
  189.  
  190.    // nIconCount = 0;
  191.  
  192.     for ( i = 0; i < header.idCount; i++ )
  193.     {
  194.         pGrpIconDir->idEntries[i].bWidth = pIconDirEntry[i].bWidth;
  195.         pGrpIconDir->idEntries[i].bHeight = pIconDirEntry[i].bHeight;
  196.         pGrpIconDir->idEntries[i].bColorCount = pIconDirEntry[i].bColorCount;
  197.         pGrpIconDir->idEntries[i].bReserved = pIconDirEntry[i].bReserved;
  198.         pGrpIconDir->idEntries[i].wPlanes = pIconDirEntry[i].wPlanes;
  199.         pGrpIconDir->idEntries[i].wBitCount = pIconDirEntry[i].wBitCount;
  200.         pGrpIconDir->idEntries[i].dwBytesInRes = pIconDirEntry[i].dwBytesInRes;
  201.         pGrpIconDir->idEntries[i].nID = i+1;
  202.     }
  203.  
  204.     EnumResourceTypesA( hExeSource, (ENUMRESTYPEPROCA)ResTypes, 0);
  205.  
  206.     CloseHandle(hIconFile);
  207. }
  208.  
  209. IconChanger::~IconChanger() // De-Constructor
  210. {
  211.  
  212. }
  213.  
  214. void IconChanger::ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName)
  215. {
  216.     /*
  217.      LPICONDIRENTRY pIconDirEntry(NULL);
  218.      LPGRPICONDIR pGrpIconDir(NULL);
  219.      HEADER header;
  220.      LPBYTE pIconBytes(NULL);
  221.      HANDLE hIconFile(NULL);
  222.      DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  223.      HANDLE hUpdate(NULL);
  224.      BOOL ret(FALSE);
  225.      WORD i(0);
  226.  
  227.      pGrpIconDir->idReserved=header.idReserved;
  228.      pGrpIconDir->idType=header.idType;
  229.      pGrpIconDir->idCount=header.idCount;
  230.  
  231.      for(i=0;i<header.idCount;i++)
  232.      {
  233.          pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
  234.          pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
  235.          pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
  236.          pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
  237.          pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
  238.          pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
  239.          pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
  240.          pGrpIconDir->idEntries[i].nID=i+1;        }
  241.  
  242.      hUpdate = BeginUpdateResource(lpExeName, false);
  243.      if (hUpdate!=NULL)
  244.      {
  245.          HMODULE hMe= 0; // means load from this module
  246.  
  247.          if (FindResource( hMe, lpExeName, RT_GROUP_ICON))
  248.          {
  249.  
  250.              ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(0), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pGrpIconDir, nGSize);
  251.              if (!ret)
  252.              {
  253.                  delete[] pIconDirEntry;
  254.                  delete[] pGrpIconDir;
  255.                  CloseHandle(hIconFile);
  256.                  return;
  257.              }
  258.          }
  259.  
  260.  
  261.          for(i=0;i<header.idCount;i++)
  262.          {
  263.  
  264.              nSize = pIconDirEntry[i].dwBytesInRes;
  265.  
  266.              dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
  267.              if (dwRet==INVALID_SET_FILE_POINTER)
  268.              {
  269.                  break;
  270.              }
  271.  
  272.              delete[] pIconBytes;
  273.  
  274.              pIconBytes = new BYTE[nSize];
  275.              ZeroMemory(pIconBytes, nSize);
  276.              ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
  277.              if(!ret)
  278.              {
  279.                  break;
  280.              }
  281.  
  282.              ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize);
  283.              if(!ret)
  284.              {
  285.                  break;
  286.              }
  287.          }
  288.  
  289.          if (pIconBytes!=NULL)
  290.          {
  291.             delete[] pIconBytes;
  292.          }
  293.  
  294.          EndUpdateResource(hUpdate, false);
  295.      }
  296.  
  297.  
  298.      //SAFE_ARRAY_DELETE(pGrpIconDir);
  299.      //SAFE_ARRAY_DELETE(pIconDirEntry);
  300.      CloseHandle(hIconFile);
  301.  
  302.      */
  303.  }
  304.  
  305.  
  306. // IconChanger.h
  307.  
  308. #ifndef ICONCHANGER_H
  309. #define ICONCHANGER_H
  310.  
  311. #include <windows.h>
  312. #include <tchar.h>
  313. #include <QString>
  314.  
  315.  typedef struct tagHEADER
  316.  {
  317.      WORD idReserved;
  318.      WORD idType;
  319.      WORD idCount;
  320.  }HEADER, *LPHEADER;
  321.  
  322.  typedef struct tagICONDIRENTRY
  323.  {
  324.      BYTE bWidth;
  325.      BYTE bHeight;
  326.      BYTE bColorCount;
  327.      BYTE bReserved;
  328.      WORD wPlanes;
  329.      WORD wBitCount;
  330.      DWORD dwBytesInRes;
  331.      DWORD dwImageOffset;
  332.  }ICONDIRENTRY, *LPICONDIRENTRY;
  333.  
  334.  typedef struct tagGRPICONDIRENTRY
  335.  {
  336.      BYTE bWidth;
  337.      BYTE bHeight;
  338.      BYTE bColorCount;
  339.      BYTE bReserved;
  340.      WORD wPlanes;
  341.      WORD wBitCount;
  342.      DWORD dwBytesInRes;
  343.      WORD nID;
  344.  }GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
  345.  
  346.  typedef struct tagGRPICONDIR
  347.  {
  348.      WORD idReserved;
  349.      WORD idType;
  350.      WORD idCount;
  351.      GRPICONDIRENTRY idEntries[1];
  352.  }GRPICONDIR, *LPGRPICONDIR;
  353.  
  354. class IconChanger {
  355.  
  356. public:
  357.  
  358.     // Members
  359.     HRSRC hrResSource;
  360.     HMODULE hExeSource;
  361.     HGLOBAL hGlobSource;
  362.  
  363.     char *cFilePathSource;
  364.     DWORD dwSizeOfSource;
  365.     char* lpIconBuffer;
  366.  
  367.     int nIconCount;
  368.  
  369.     // Methods
  370.     IconChanger();
  371.     ~IconChanger();
  372.  
  373.     void GetIconFromSource(QString sIconPath, QString sExePath);
  374.     void ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName);
  375. private:
  376.  
  377.  
  378. };
  379.  
  380. #endif // ICONCHANGER_H
Advertisement
Add Comment
Please, Sign In to add comment