WetCode

Untitled

Dec 9th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.82 KB | None | 0 0
  1. // IconChanger.h
  2.  
  3. #ifndef ICONCHANGER_H
  4. #define ICONCHANGER_H
  5.  
  6. #include <windows.h>
  7. #include <tchar.h>
  8. #include <string>
  9. #include <iostream>
  10. #include <vector>
  11.  
  12.  typedef struct tagHEADER
  13.  {
  14.      WORD idReserved;
  15.      WORD idType;
  16.      WORD idCount;
  17.  }HEADER, *LPHEADER;
  18.  
  19.  typedef struct tagICONDIRENTRY
  20.  {
  21.      BYTE bWidth;
  22.      BYTE bHeight;
  23.      BYTE bColorCount;
  24.      BYTE bReserved;
  25.      WORD wPlanes;
  26.      WORD wBitCount;
  27.      DWORD dwBytesInRes;
  28.      DWORD dwImageOffset;
  29.  }ICONDIRENTRY, *LPICONDIRENTRY;
  30.  
  31.  typedef struct tagGRPICONDIRENTRY
  32.  {
  33.      BYTE bWidth;
  34.      BYTE bHeight;
  35.      BYTE bColorCount;
  36.      BYTE bReserved;
  37.      WORD wPlanes;
  38.      WORD wBitCount;
  39.      DWORD dwBytesInRes;
  40.      WORD nID;
  41.  }GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
  42.  
  43.  typedef struct tagGRPICONDIR
  44.  {
  45.      WORD idReserved;
  46.      WORD idType;
  47.      WORD idCount;
  48.      GRPICONDIRENTRY idEntries[1];
  49.  }GRPICONDIR, *LPGRPICONDIR;
  50.  
  51. typedef struct tagEXERESINFO
  52. {
  53.     std::vector<LPTSTR> vecIconNames;
  54.     std::vector<LPTSTR> vecGroupNames;
  55.     std::vector<WORD> wLangIcon;
  56.     std::vector<WORD> wLangGroup;
  57.     int nIconCount;
  58.     int nGroupCount;
  59. } EXERESINFO, *LPEXERESINFO;
  60.  
  61. class IconChanger {
  62.  
  63. public:
  64.  
  65.     // Members
  66.     HRSRC hrResSource;
  67.     HMODULE hExeSource;
  68.     HGLOBAL hGlobSource;
  69.  
  70.     char *cFilePathSource;
  71.     DWORD dwSizeOfSource;
  72.     char* lpIconBuffer;
  73.  
  74.     int nIconCount;
  75.  
  76.     // Methods
  77.     IconChanger();
  78.     ~IconChanger();
  79.  
  80.     void GetIconFromSource(std::string sIconPath, std::string sExePath);
  81.     void ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName);
  82.  
  83.  
  84. private:
  85.  
  86. };
  87.  
  88.  
  89.  
  90. #endif // ICONCHANGER_H
  91.  
  92. // IconChanger.cpp
  93.  
  94.  
  95. #include "IconChanger.h"
  96.  
  97. //a global vector for storing lpName's
  98. // and yes I know this is evil
  99. EXERESINFO oldExeResInfo;
  100. ///////////////////////////////////
  101.  
  102. BOOL ResLang( HMODULE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, LONG_PTR lParam)
  103. {
  104.     if (!IS_INTRESOURCE(lpszName))
  105.     {
  106.         if ( lpszType == RT_ICON )
  107.         {
  108.             oldExeResInfo.wLangIcon.push_back( wIDLanguage );
  109.         } else if ( lpszType == (RT_ICON + 11) )
  110.         {
  111.             oldExeResInfo.wLangGroup.push_back( wIDLanguage );
  112.         } else {
  113.             // ....
  114.         }
  115.  
  116.     } else if ( IS_INTRESOURCE(lpszName) ) {
  117.  
  118.         if ( lpszType == RT_ICON )
  119.         {
  120.             oldExeResInfo.wLangIcon.push_back( wIDLanguage );
  121.         } else if ( lpszType == (RT_ICON + 11) )
  122.         {
  123.             oldExeResInfo.wLangGroup.push_back( wIDLanguage );
  124.         } else {
  125.             // ....
  126.         }
  127.  
  128.     } else {
  129.         return FALSE;
  130.     }
  131.  
  132.     return TRUE;
  133. }
  134.  
  135. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  136. {
  137.  
  138.     if (!IS_INTRESOURCE(lpName))
  139.     {
  140.  
  141.         std::cout << "lpType in Names: " << lpType << std::endl;
  142.         std::cout << "lpType in Names: " << lpName << std::endl;
  143.  
  144.         if ( lpType == RT_ICON )
  145.         {
  146.             oldExeResInfo.nIconCount++;
  147.             oldExeResInfo.vecIconNames.push_back( lpName );
  148.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  149.         }
  150.  
  151.         if ( lpType == (RT_ICON + 11))
  152.         {
  153.             oldExeResInfo.nGroupCount++;
  154.             oldExeResInfo.vecGroupNames.push_back( lpName );
  155.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  156.         }
  157.  
  158.     } else {
  159.         std::cout <<GetLastError()<< "lpType in Names: " << (short)lpType << std::endl;
  160.  
  161.         if ( lpType == RT_ICON )
  162.         {
  163.             oldExeResInfo.nIconCount++;
  164.             oldExeResInfo.vecIconNames.push_back( lpName );
  165.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  166.         }
  167.  
  168.         if ( lpType == (RT_ICON + 11))
  169.         {
  170.             oldExeResInfo.nGroupCount++;
  171.             oldExeResInfo.vecGroupNames.push_back( lpName );
  172.             EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  173.         }
  174.     }
  175.  
  176.    return TRUE;
  177. }
  178.  
  179. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  180. {
  181.  
  182.     if (!IS_INTRESOURCE(lpType))
  183.     {
  184.         if ( lpType == (RT_ICON +11) )
  185.         {
  186.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  187.         }
  188.  
  189.         std::cout << "lpType:" << lpType << std::endl;
  190.         if ( lpType == (RT_ICON ) )
  191.         {
  192.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  193.         }
  194.  
  195.     } else {
  196.         std::cout << "lpType:" << (short)lpType << std::endl;
  197.        // qDebug()  << "lpType:" << (short)lpType;
  198.         if ( lpType == (RT_ICON +11) )
  199.         {
  200.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  201.         }
  202.  
  203.         if ( lpType == (RT_ICON ) )
  204.         {
  205.             EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
  206.         }
  207.     }
  208.  
  209.    return true;
  210. }
  211.  
  212.  
  213. IconChanger::IconChanger() // Constructor
  214. {
  215.     hExeSource = NULL;
  216.     hrResSource = NULL;
  217.     hGlobSource = NULL;
  218.     lpIconBuffer = NULL;
  219. }
  220.                                                             // Retrive Icon Buffer
  221. void IconChanger::GetIconFromSource(std::string sIconFile, std::string sExeFile) // from source Exe/DLL
  222. {
  223.     oldExeResInfo.nGroupCount = 0;
  224.     oldExeResInfo.nIconCount = 0;
  225.     //////////////////////////////
  226.     HMODULE hExe1 = LoadLibrary(sExeFile.c_str());
  227.     EnumResourceTypes(hExe1, (ENUMRESTYPEPROC)ResTypes, 0);
  228.  
  229.        FreeLibrary(hExe1);
  230.        ////////////////////////////
  231.  
  232.      LPICONDIRENTRY pIconDirEntry(NULL);
  233.      LPGRPICONDIR pGrpIconDir(NULL);
  234.      HEADER header;
  235.      LPBYTE pIconBytes(NULL);
  236.      HANDLE hIconFile(NULL);
  237.      DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  238.      HANDLE hUpdate(NULL);
  239.      BOOL ret(FALSE);
  240.      WORD i(0);
  241.  
  242.      //Open the Icon file
  243.      hIconFile = CreateFile(sIconFile.c_str(), GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  244.      if (hIconFile == INVALID_HANDLE_VALUE)
  245.      {
  246.          return;
  247.      }
  248.      //read the file head pointer
  249.      ret=ReadFile(hIconFile, &header, sizeof(HEADER), &dwReserved, NULL);
  250.      if (!ret)
  251.      {
  252.          CloseHandle(hIconFile);
  253.          return;
  254.      }
  255.  
  256.      pIconDirEntry = (LPICONDIRENTRY)new BYTE[header.idCount*sizeof(ICONDIRENTRY)];
  257.      if (pIconDirEntry==NULL)
  258.      {
  259.          CloseHandle(hIconFile);
  260.          return;
  261.      }
  262.  
  263.      ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
  264.      if (!ret)
  265.      {
  266.          delete[] pIconDirEntry;
  267.          CloseHandle(hIconFile);
  268.          return;
  269.      }
  270.  
  271.      nGSize=sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
  272.      pGrpIconDir=(LPGRPICONDIR)new BYTE[nGSize];
  273.      ZeroMemory(pGrpIconDir, nSize);
  274.  
  275.      pGrpIconDir->idReserved=header.idReserved;
  276.      pGrpIconDir->idType=header.idType;
  277.      pGrpIconDir->idCount=header.idCount;
  278.  
  279.      for(i=0;i<header.idCount;i++)
  280.      {
  281.          pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
  282.          pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
  283.          pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
  284.          pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
  285.          pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
  286.          pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
  287.          pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
  288.          pGrpIconDir->idEntries[i].nID=i+1;
  289.      }
  290.  
  291.      /// CLEANUP FIRST THEN WE PLAY /////
  292.  
  293.      hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
  294.  
  295.      for ( unsigned i = 0; i < oldExeResInfo.nIconCount; i++ )
  296.      {
  297.          if (!UpdateResource( hUpdate, RT_ICON, MAKEINTRESOURCE((long)oldExeResInfo.vecIconNames[i]),
  298.                               oldExeResInfo.wLangIcon[i], NULL, 0) )
  299.          {
  300.              std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
  301.          }
  302.      }
  303.      EndUpdateResource( hUpdate, FALSE );
  304.  
  305.  
  306.      hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
  307.  
  308.      for ( unsigned i = 0; i < oldExeResInfo.nGroupCount; i++ )
  309.      {
  310.          if (!UpdateResource( hUpdate, (RT_ICON + 11), MAKEINTRESOURCE((long)oldExeResInfo.vecGroupNames[i]),
  311.                               oldExeResInfo.wLangIcon[i], NULL, 0) )
  312.          {
  313.              std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
  314.          }
  315.      }
  316.      EndUpdateResource( hUpdate, FALSE );
  317.  
  318.      /// SO NICE WE HAVE A CLEAN FLOOR TO PLAY ON :p //////
  319.  
  320.      hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
  321.      if ( hUpdate == NULL )
  322.      {
  323.          std::cout << "Begin Update New FAILED! - Error Code: " << GetLastError() << std::endl;
  324.      }
  325.  
  326.      if ( !UpdateResource( hUpdate, (RT_ICON + 11), MAKEINTRESOURCE(1),
  327.                            MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (LPVOID)pGrpIconDir, nGSize) )
  328.      {
  329.          std::cout << "FAILED To upload new Icon Group! - Error Code: " << GetLastError() << std::endl;
  330.      }
  331.  
  332.      for ( unsigned i = 0; i <header.idCount; i++ )
  333.      {
  334.          nSize = pIconDirEntry[i].dwBytesInRes;
  335.  
  336.           dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
  337.           if (dwRet==INVALID_SET_FILE_POINTER)
  338.           {
  339.               break;
  340.           }
  341.  
  342.           delete[] pIconBytes;
  343.  
  344.           pIconBytes = new BYTE[nSize];
  345.           ZeroMemory(pIconBytes, nSize);
  346.           ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
  347.           if(!ret)
  348.           {
  349.               break;
  350.           }
  351.  
  352.           ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize);
  353.           if(!ret)
  354.           {
  355.               break;
  356.           }
  357.       }
  358.  
  359.       if (pIconBytes!=NULL)
  360.       {
  361.          delete[] pIconBytes;
  362.       }
  363.  
  364.       EndUpdateResource(hUpdate, false);
  365.  
  366. }
  367.  
  368. IconChanger::~IconChanger() // De-Constructor
  369. {
  370.  
  371. }
  372.  
  373. void IconChanger::ChangeExeIcon(LPCTSTR lpIconFile, LPCTSTR lpExeName)
  374. {
  375. /*
  376.      LPICONDIRENTRY pIconDirEntry(NULL);
  377.      LPGRPICONDIR pGrpIconDir(NULL);
  378.      HEADER header;
  379.      LPBYTE pIconBytes(NULL);
  380.      HANDLE hIconFile(NULL);
  381.      DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  382.      HANDLE hUpdate(NULL);
  383.      BOOL ret(FALSE);
  384.      WORD i(0);
  385.  
  386.      pGrpIconDir->idReserved=header.idReserved;
  387.      pGrpIconDir->idType=header.idType;
  388.      pGrpIconDir->idCount=header.idCount;
  389.  
  390.      for(i=0;i<header.idCount;i++)
  391.      {
  392.          pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
  393.          pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
  394.          pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
  395.          pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
  396.          pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
  397.          pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
  398.          pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
  399.          pGrpIconDir->idEntries[i].nID=i+1;        }
  400.  
  401.      hUpdate = BeginUpdateResource(lpExeName, false);
  402.      if (hUpdate!=NULL)
  403.      {
  404.          HMODULE hMe= 0; // means load from this module
  405.  
  406.          if (FindResource( hMe, lpExeName, RT_GROUP_ICON))
  407.          {
  408.  
  409.              ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(0), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pGrpIconDir, nGSize);
  410.              if (!ret)
  411.              {
  412.                  delete[] pIconDirEntry;
  413.                  delete[] pGrpIconDir;
  414.                  CloseHandle(hIconFile);
  415.                  return;
  416.              }
  417.          }
  418.  
  419.  
  420.          for(i=0;i<header.idCount;i++)
  421.          {
  422.  
  423.              nSize = pIconDirEntry[i].dwBytesInRes;
  424.  
  425.              dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
  426.              if (dwRet==INVALID_SET_FILE_POINTER)
  427.              {
  428.                  break;
  429.              }
  430.  
  431.              delete[] pIconBytes;
  432.  
  433.              pIconBytes = new BYTE[nSize];
  434.              ZeroMemory(pIconBytes, nSize);
  435.              ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
  436.              if(!ret)
  437.              {
  438.                  break;
  439.              }
  440.  
  441.              ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize);
  442.              if(!ret)
  443.              {
  444.                  break;
  445.              }
  446.          }
  447.  
  448.          if (pIconBytes!=NULL)
  449.          {
  450.             delete[] pIconBytes;
  451.          }
  452.  
  453.          EndUpdateResource(hUpdate, false);
  454.      }
  455.  
  456.  
  457.      //SAFE_ARRAY_DELETE(pGrpIconDir);
  458.      //SAFE_ARRAY_DELETE(pIconDirEntry);
  459.      CloseHandle(hIconFile);
  460. */
  461.  
  462.  }
Advertisement
Add Comment
Please, Sign In to add comment