WetCode

Icon Changer (Vista Icons Supported)

Dec 13th, 2013
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.14 KB | None | 0 0
  1. /* =====================================================================================                        
  2.                                Icon Changer Project 2013
  3.                                           by
  4.                                    dkwhite & WetCode
  5.                           Copyright (c) 2013 dkwhite & WetCode
  6.  
  7. Permission to use, copy, modify and distribute this software for personal and educational use is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of dkwhite & WetCode authors are not used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. dkwhite & WetCode the authors make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.
  8.  
  9. dkwhite & WetCode THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL dkwhite & WetCode THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  10.  
  11. ======================================================================================================== */
  12.  
  13. // IconChanger.h
  14. #ifndef ICONCHANGER_H
  15. #define ICONCHANGER_H
  16.  
  17. #include <windows.h>
  18. #include <tchar.h>
  19. #include <string>
  20. #include <iostream>
  21. #include <vector>
  22.  
  23.  typedef struct tagHEADER
  24.  {
  25.      WORD idReserved;
  26.      WORD idType;
  27.      WORD idCount;
  28.  }HEADER, *LPHEADER;
  29.  
  30.  typedef struct tagICONDIRENTRY
  31.  {
  32.      BYTE bWidth;
  33.      BYTE bHeight;
  34.      BYTE bColorCount;
  35.      BYTE bReserved;
  36.      WORD wPlanes;
  37.      WORD wBitCount;
  38.      DWORD dwBytesInRes;
  39.      DWORD dwImageOffset;
  40.  }ICONDIRENTRY, *LPICONDIRENTRY;
  41.  
  42.  
  43. #pragma pack( push )
  44. #pragma pack( 2 )
  45. typedef struct
  46. {
  47.    BYTE   bWidth;               // Width, in pixels, of the image
  48.    BYTE   bHeight;              // Height, in pixels, of the image
  49.    BYTE   bColorCount;          // Number of colors in image (0 if >=8bpp)
  50.    BYTE   bReserved;            // Reserved
  51.    WORD   wPlanes;              // Color Planes
  52.    WORD   wBitCount;            // Bits per pixel
  53.    DWORD  dwBytesInRes;         // how many bytes in this resource?
  54.    WORD   nID;                  // the ID
  55. } GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
  56.  
  57. typedef struct
  58. {
  59.    WORD            idReserved;   // Reserved (must be 0)
  60.    WORD            idType;       // Resource type (1 for icons)
  61.    WORD            idCount;      // How many images?
  62.    GRPICONDIRENTRY idEntries[1]; // The entries for each image
  63. } GRPICONDIR, *LPGRPICONDIR;
  64.  
  65. typedef struct tagEXERESINFO
  66. {
  67.     std::vector<LPTSTR> vecIconNames;
  68.     std::vector<LPTSTR> vecGroupNames;
  69.     std::vector<WORD> wLangIcon;
  70.     std::vector<WORD> wLangGroup;
  71.     int nIconCount;
  72.     int nGroupCount;
  73. } EXERESINFO, *LPEXERESINFO;
  74.  
  75. class IconChanger {
  76.  
  77. public:
  78.  
  79.     // Members
  80.     HRSRC hrResSource;
  81.     HMODULE hExeSource;
  82.     HGLOBAL hGlobSource;
  83.  
  84.     char *cFilePathSource;
  85.     DWORD dwSizeOfSource;
  86.     char* lpIconBuffer;
  87.  
  88.     int nIconCount;
  89.  
  90.     // Methods
  91.     IconChanger();
  92.     ~IconChanger();
  93.  
  94.     void GetIconFromSource(std::string sIconPath, std::string sExePath);
  95.     void GetIconFromSourceVista( std::string sExeFile, std::string sIconFile );
  96.  
  97. private:
  98.  
  99. };
  100.  
  101. #endif // ICONCHANGER_H
  102.  
  103. // IconCHanger.cpp
  104.  
  105. //a global struct for storing lpName's
  106. // and yes I know this is evil
  107. EXERESINFO oldExeResInfo;
  108. ///////////////////////////////////
  109.  
  110. BOOL ResLang( HMODULE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, LONG_PTR lParam)
  111. {
  112.     if (!IS_INTRESOURCE(lpszName))
  113.     {
  114.         if ( lpszType == RT_ICON )
  115.         {
  116.             oldExeResInfo.wLangIcon.push_back( wIDLanguage );
  117.         } else if ( lpszType == (RT_ICON + 11) )
  118.         {
  119.             oldExeResInfo.wLangGroup.push_back( wIDLanguage );
  120.         } else {
  121.             // ....
  122.         }
  123.  
  124.     } else if ( IS_INTRESOURCE(lpszName) ) {
  125.  
  126.         if ( lpszType == RT_ICON )
  127.         {
  128.             oldExeResInfo.wLangIcon.push_back( wIDLanguage );
  129.         } else if ( lpszType == (RT_ICON + 11) )
  130.         {
  131.             oldExeResInfo.wLangGroup.push_back( wIDLanguage );
  132.         } else {
  133.             // ....
  134.         }
  135.  
  136.     } else {
  137.         return FALSE;
  138.     }
  139.  
  140.     return TRUE;
  141. }
  142.  
  143. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  144. {
  145.  
  146.     if (!IS_INTRESOURCE(lpName))
  147.     {
  148.         std::cout << "lpType in Names: " << lpType << std::endl;
  149.         std::cout << "lpType in Names: " << lpName << std::endl;
  150.  
  151.         if ( lpType == RT_ICON )
  152.         {
  153.             oldExeResInfo.nIconCount++;
  154.             oldExeResInfo.vecIconNames.push_back( lpName );
  155.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  156.         }
  157.  
  158.         if ( lpType == (RT_ICON + 11))
  159.         {
  160.             oldExeResInfo.nGroupCount++;
  161.             oldExeResInfo.vecGroupNames.push_back( lpName );
  162.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  163.         }
  164.  
  165.     } else {
  166.         std::cout <<GetLastError()<< "lpType in Names: " << (short)lpType << std::endl;
  167.  
  168.         if ( lpType == RT_ICON )
  169.         {
  170.             oldExeResInfo.nIconCount++;
  171.             oldExeResInfo.vecIconNames.push_back( lpName );
  172.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  173.         }
  174.  
  175.         if ( lpType == (RT_ICON + 11))
  176.         {
  177.             oldExeResInfo.nGroupCount++;
  178.             oldExeResInfo.vecGroupNames.push_back( lpName );
  179.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  180.         }
  181.     }
  182.  
  183.    return TRUE;
  184. }
  185.  
  186. BOOL ResTypes(HMODULE hModule, LPCSTR lpType, LONG lParam)
  187. {
  188.  
  189.     if (!IS_INTRESOURCE(lpType))
  190.     {
  191.         if ( lpType == (RT_ICON +11) )
  192.         {
  193.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  194.         }
  195.  
  196.         std::cout << "lpType:" << lpType << std::endl;
  197.         if ( lpType == (RT_ICON ) )
  198.         {
  199.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  200.         }
  201.  
  202.     } else {
  203.         std::cout << "lpType:" << (short)lpType << std::endl;
  204.        // qDebug()  << "lpType:" << (short)lpType;
  205.         if ( lpType == (RT_ICON +11) )
  206.         {
  207.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  208.         }
  209.  
  210.         if ( lpType == (RT_ICON ) )
  211.         {
  212.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  213.         }
  214.     }
  215.  
  216.    return true;
  217. }
  218.  
  219.  
  220. IconChanger::IconChanger() // Constructor
  221. {
  222.     hExeSource = NULL;
  223.     hrResSource = NULL;
  224.     hGlobSource = NULL;
  225.     lpIconBuffer = NULL;
  226. }
  227.                                                             // Retrive Icon Buffer
  228. void IconChanger::GetIconFromSource(std::string sIconFile, std::string sExeFile) // from source Exe/DLL
  229. {
  230.     oldExeResInfo.nGroupCount = 0;
  231.     oldExeResInfo.nIconCount = 0;
  232.     //////////////////////////////
  233.     HMODULE hExe1 = LoadLibrary(sExeFile.c_str());
  234.     EnumResourceTypes(hExe1, (ENUMRESTYPEPROC)ResTypes, 0);
  235.  
  236.     FreeLibrary(hExe1);
  237.     ////////////////////////////
  238.  
  239.     LPICONDIRENTRY pIconDirEntry(NULL);
  240.     LPGRPICONDIR pGrpIconDir(NULL);
  241.     HEADER header;
  242.     LPBYTE pIconBytes(NULL);
  243.     HANDLE hIconFile(NULL);
  244.     DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  245.     HANDLE hUpdate(NULL);
  246.     BOOL ret(FALSE);
  247.      WORD i(0);
  248.  
  249.      //Open the Icon file
  250.      hIconFile = CreateFile(sIconFile.c_str(), GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  251.      if (hIconFile == INVALID_HANDLE_VALUE)
  252.      {
  253.          return;
  254.      }
  255.      //read the file head pointer
  256.      ret=ReadFile(hIconFile, &header, sizeof(HEADER), &dwReserved, NULL);
  257.      if (!ret)
  258.      {
  259.          CloseHandle(hIconFile);
  260.          return;
  261.      }
  262.  
  263.      pIconDirEntry = (LPICONDIRENTRY)new BYTE[header.idCount*sizeof(ICONDIRENTRY)];
  264.      if (pIconDirEntry==NULL)
  265.      {
  266.          CloseHandle(hIconFile);
  267.          return;
  268.      }
  269.  
  270.      ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
  271.      if (!ret)
  272.      {
  273.          delete[] pIconDirEntry;
  274.          CloseHandle(hIconFile);
  275.          return;
  276.      }
  277.  
  278.      nGSize=sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
  279.      pGrpIconDir=(LPGRPICONDIR)new BYTE[nGSize];
  280.      ZeroMemory(pGrpIconDir, nSize);
  281.  
  282.      pGrpIconDir->idReserved=header.idReserved;
  283.      pGrpIconDir->idType=header.idType;
  284.      pGrpIconDir->idCount=header.idCount;
  285.  
  286.      for(i=0;i<header.idCount;i++)
  287.      {
  288.          pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
  289.          pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
  290.          pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
  291.          pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
  292.          pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
  293.          pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
  294.          pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
  295.          pGrpIconDir->idEntries[i].nID=i+1;
  296.      }
  297.  
  298.      /// CLEANUP FIRST THEN WE PLAY /////
  299.  
  300.      hUpdate = BeginUpdateResourceA( sExeFile.c_str(), FALSE );
  301.  
  302.      for ( unsigned i = 0; i < oldExeResInfo.nIconCount; i++ )
  303.      {
  304.          if (!UpdateResource( hUpdate, RT_ICON, MAKEINTRESOURCE((long)oldExeResInfo.vecIconNames[i]),
  305.                               oldExeResInfo.wLangIcon[i], NULL, 0) )
  306.          {
  307.              std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
  308.          }
  309.      }
  310.      EndUpdateResource( hUpdate, FALSE );
  311.  
  312.  
  313.      hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
  314.  
  315.      for ( unsigned i = 0; i < oldExeResInfo.nGroupCount; i++ )
  316.      {
  317.          if (!UpdateResource( hUpdate, (RT_ICON + 11), MAKEINTRESOURCE((long)oldExeResInfo.vecGroupNames[i]),
  318.                               oldExeResInfo.wLangIcon[i], NULL, 0) )
  319.          {
  320.              std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
  321.          }
  322.      }
  323.      EndUpdateResource( hUpdate, FALSE );
  324.  
  325.      /// SO NICE WE HAVE A CLEAN FLOOR TO PLAY ON :p //////
  326.  
  327.      hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
  328.      if ( hUpdate == NULL )
  329.      {
  330.          std::cout << "Begin Update New FAILED! - Error Code: " << GetLastError() << std::endl;
  331.      }
  332.  
  333.      if ( !UpdateResource( hUpdate, (RT_GROUP_ICON), MAKEINTRESOURCE(1),
  334.                            oldExeResInfo.wLangGroup[0], (LPVOID)pGrpIconDir, nGSize) )
  335.      {
  336.          std::cout << "FAILED To upload new Icon Group! - Error Code: " << GetLastError() << std::endl;
  337.      }
  338.  
  339.      for ( unsigned i = 0; i <header.idCount; i++ )
  340.      {
  341.           nSize = pIconDirEntry[i].dwBytesInRes;
  342.  
  343.           dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
  344.           if (dwRet==INVALID_SET_FILE_POINTER)
  345.           {
  346.               break;
  347.           }
  348.  
  349.  
  350.           delete[] pIconBytes;
  351.  
  352.           pIconBytes = new BYTE[nSize];
  353.           ZeroMemory(pIconBytes, nSize);
  354.           ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
  355.           if(!ret)
  356.           {
  357.               break;
  358.           }
  359.  
  360.           ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID),
  361.                                oldExeResInfo.wLangIcon[0], (LPVOID)pIconBytes, nSize);
  362.           if(!ret)
  363.           {
  364.               break;
  365.           }
  366.       }
  367.  
  368.       if (pIconBytes!=NULL)
  369.       {
  370.          delete[] pIconBytes;
  371.       }
  372.  
  373.       EndUpdateResource(hUpdate, false);
  374.  
  375.       if (pGrpIconDir){
  376.           delete[] pGrpIconDir;
  377.           pGrpIconDir = NULL;
  378.       }
  379.  
  380.       if(pIconDirEntry){
  381.           delete[] pIconDirEntry;
  382.           pIconDirEntry = NULL;
  383.       }
  384.  
  385.       CloseHandle(hIconFile);
  386. }
Advertisement
Add Comment
Please, Sign In to add comment