WetCode

Untitled

Dec 5th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.66 KB | None | 0 0
  1. // .PRO
  2. #-------------------------------------------------
  3. #
  4. # Project created by QtCreator 2013-11-28T16:52:43
  5. #
  6. #-------------------------------------------------
  7.  
  8. QT       += core gui
  9. QT       += webkit
  10. QT += webkitwidgets
  11.  
  12. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  13.  
  14. TARGET = Clandestine
  15. TEMPLATE = app
  16.  
  17.  
  18. SOURCES += main.cpp\
  19.         mainwindow.cpp \
  20.     IconChanger.cpp
  21.  
  22. HEADERS  += mainwindow.h \
  23.             IconChanger.h \
  24.             Windows.h
  25.  
  26. FORMS    += mainwindow.ui
  27.  
  28. RESOURCES += \
  29.     res.qrc
  30.  
  31.  
  32.  
  33. // IconChanger.h
  34.  
  35. #ifndef ICONCHANGER_H
  36. #define ICONCHANGER_H
  37.  
  38. #include <windows.h>
  39. #include <tchar.h>
  40. #include <QString>
  41.  
  42.  typedef struct tagHEADER
  43.  {
  44.      WORD idReserved;
  45.      WORD idType;
  46.      WORD idCount;
  47.  }HEADER, *LPHEADER;
  48.  
  49.  typedef struct tagICONDIRENTRY
  50.  {
  51.      BYTE bWidth;
  52.      BYTE bHeight;
  53.      BYTE bColorCount;
  54.      BYTE bReserved;
  55.      WORD wPlanes;
  56.      WORD wBitCount;
  57.      DWORD dwBytesInRes;
  58.      DWORD dwImageOffset;
  59.  }ICONDIRENTRY, *LPICONDIRENTRY;
  60.  
  61.  typedef struct tagGRPICONDIRENTRY
  62.  {
  63.      BYTE bWidth;
  64.      BYTE bHeight;
  65.      BYTE bColorCount;
  66.      BYTE bReserved;
  67.      WORD wPlanes;
  68.      WORD wBitCount;
  69.      DWORD dwBytesInRes;
  70.      WORD nID;
  71.  }GRPICONDIRENTRY, *LPGRPICONDIRENTRY;;
  72.  
  73.  typedef struct tagGRPICONDIR
  74.  {
  75.      WORD idReserved;
  76.      WORD idType;
  77.      WORD idCount;
  78.      GRPICONDIRENTRY idEntries[1];
  79.  }GRPICONDIR, *LPGRPICONDIR;
  80.  
  81. class IconChanger {
  82.  
  83. public:
  84.  
  85.     // Members
  86.     HRSRC hrResSource;
  87.     HMODULE hExeSource;
  88.     HGLOBAL hGlobSource;
  89.  
  90.     char *cFilePathSource;
  91.     DWORD dwSizeOfSource;
  92.     char* lpIconBuffer;
  93.  
  94.     // Methods
  95.     IconChanger();
  96.     ~IconChanger();
  97.  
  98.     void GetIconFromSource(QString sIconPath, QString sExePath);
  99.     void ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName);
  100.  
  101. private:
  102.  
  103.  
  104. };
  105.  
  106. #endif // ICONCHANGER_H
  107.  
  108.  
  109. // IconChanger.h //
  110. // IconChanger.cpp
  111.  
  112. #include "IconChanger.h"
  113.  
  114. #include <QMessageBox>
  115.  
  116. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam);
  117. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam);
  118.  
  119. IconChanger::IconChanger() // Constructor
  120. {
  121.     hExeSource = NULL;
  122.     hrResSource = NULL;
  123.     hGlobSource = NULL;
  124.     lpIconBuffer = NULL;
  125. }
  126.                                                             // Retrive Icon Buffer
  127. void IconChanger::GetIconFromSource(QString sIconPath, QString sExePath) // from source Exe/DLL
  128. {
  129.     LPICONDIRENTRY pIconDirEntry(NULL);
  130.     LPGRPICONDIR pGrpIconDir(NULL);
  131.     HEADER header;
  132.     LPBYTE pIconBytes(NULL);
  133.     HANDLE hIconFile(NULL);
  134.     DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  135.     HANDLE hUpdate(NULL);
  136.     BOOL ret(FALSE);
  137.     WORD i(0);
  138.  
  139.     LoadLibraryExA( sExePath.toStdString().c_str(), &hExeSource, LOAD_LIBRARY_AS_DATAFILE );
  140.     if ( hExeSource == NULL )
  141.     {
  142.         QMessageBox mess; mess.setText("Unable to get handle to EXE/DLL" + sExePath );
  143.         mess.exec();
  144.     }
  145.  
  146.     hIconFile = CreateFileA( 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 " + sIconPath );
  151.         mess.exec();
  152.     }
  153.  
  154.     ret = ReadFile( hExeSource, &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.     for ( i = 0; i < header.idCount; i++ )
  191.     {
  192.         pGrpIconDir->idEntries[i].bWidth = pIconDirEntry[i].bWidth;
  193.         pGrpIconDir->idEntries[i].bHeight = pIconDirEntry[i].bHeight;
  194.         pGrpIconDir->idEntries[i].bColorCount = pIconDirEntry[i].bColorCount;
  195.         pGrpIconDir->idEntries[i].bReserved = pIconDirEntry[i].bReserved;
  196.         pGrpIconDir->idEntries[i].wPlanes = pIconDirEntry[i].wPlanes;
  197.         pGrpIconDir->idEntries[i].wBitCount = pIconDirEntry[i].wBitCount;
  198.         pGrpIconDir->idEntries[i].dwBytesInRes = pIconDirEntry[i].dwBytesInRes;
  199.         pGrpIconDir->idEntries[i].nID = i+1;
  200.  
  201.         EnumResourceTypesW( hExeSource, (ENUMRESTYPEPROC)ResTypes, 0);
  202.     }
  203.  
  204.     CloseHandle(hIconFile);
  205. }
  206.  
  207. IconChanger::~IconChanger() // De-Constructor
  208. {
  209.  
  210. }
  211.  
  212. void IconChanger::ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName)
  213. {
  214.     /*
  215.      LPICONDIRENTRY pIconDirEntry(NULL);
  216.      LPGRPICONDIR pGrpIconDir(NULL);
  217.      HEADER header;
  218.      LPBYTE pIconBytes(NULL);
  219.      HANDLE hIconFile(NULL);
  220.      DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  221.      HANDLE hUpdate(NULL);
  222.      BOOL ret(FALSE);
  223.      WORD i(0);
  224.  
  225.      pGrpIconDir->idReserved=header.idReserved;
  226.      pGrpIconDir->idType=header.idType;
  227.      pGrpIconDir->idCount=header.idCount;
  228.  
  229.      for(i=0;i<header.idCount;i++)
  230.      {
  231.          pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
  232.          pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
  233.          pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
  234.          pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
  235.          pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
  236.          pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
  237.          pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
  238.          pGrpIconDir->idEntries[i].nID=i+1;        }
  239.  
  240.      hUpdate = BeginUpdateResource(lpExeName, false);
  241.      if (hUpdate!=NULL)
  242.      {
  243.          HMODULE hMe= 0; // means load from this module
  244.  
  245.          if (FindResource( hMe, lpExeName, RT_GROUP_ICON))
  246.          {
  247.  
  248.              ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(0), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pGrpIconDir, nGSize);
  249.              if (!ret)
  250.              {
  251.                  delete[] pIconDirEntry;
  252.                  delete[] pGrpIconDir;
  253.                  CloseHandle(hIconFile);
  254.                  return;
  255.              }
  256.          }
  257.  
  258.  
  259.          for(i=0;i<header.idCount;i++)
  260.          {
  261.  
  262.              nSize = pIconDirEntry[i].dwBytesInRes;
  263.  
  264.              dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
  265.              if (dwRet==INVALID_SET_FILE_POINTER)
  266.              {
  267.                  break;
  268.              }
  269.  
  270.              delete[] pIconBytes;
  271.  
  272.              pIconBytes = new BYTE[nSize];
  273.              ZeroMemory(pIconBytes, nSize);
  274.              ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
  275.              if(!ret)
  276.              {
  277.                  break;
  278.              }
  279.  
  280.              ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize);
  281.              if(!ret)
  282.              {
  283.                  break;
  284.              }
  285.          }
  286.  
  287.          if (pIconBytes!=NULL)
  288.          {
  289.             delete[] pIconBytes;
  290.          }
  291.  
  292.          EndUpdateResource(hUpdate, false);
  293.      }
  294.  
  295.  
  296.      //SAFE_ARRAY_DELETE(pGrpIconDir);
  297.      //SAFE_ARRAY_DELETE(pIconDirEntry);
  298.      CloseHandle(hIconFile);
  299.  
  300.      */
  301.  }
  302.  
  303. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  304. {
  305.     if (!IS_INTRESOURCE(lpType))
  306.     {
  307.         if ( lpType == RT_GROUP_ICON )
  308.         {
  309.             EnumResourceNames(hModule, RT_ICON, (ENUMRESNAMEPROC)ResNames, 0);
  310.             return true;
  311.         }
  312.  
  313.     } else {
  314.         if ( lpType == RT_GROUP_ICON )
  315.         {
  316.             EnumResourceNames(hModule, RT_ICON, (ENUMRESNAMEPROC)ResNames, 0);
  317.             return true;
  318.         }
  319.     }
  320.  
  321.    return true;
  322. }
  323.  
  324. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  325. {
  326.     if (!IS_INTRESOURCE(lpName))
  327.     {
  328.  
  329.     } else {
  330.  
  331.     }
  332.  
  333.    return true;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment