WetCode

Untitled

Dec 5th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. /*------------------------------------------------------------------------
  2. * (The MIT License)
  3. *
  4. * Copyright (c) 2008-2011 Rhomobile, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * http://rhomobile.com
  25. *------------------------------------------------------------------------*/
  26.  
  27. #include "stdafx.h"
  28. #include <Windows.h>
  29.  
  30. #pragma pack(2)
  31.  
  32. // For each 1..idCount:
  33. typedef struct
  34. {
  35.     BYTE bWidth; // Width, in pixels, of the image
  36.     BYTE bHeight; // Height, in pixels, of the image
  37.     BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  38.     BYTE bReserved; // Reserved
  39.     WORD wPlanes; // Color Planes
  40.     WORD wBitCount; // Bits per pixel
  41.     DWORD dwBytesInRes; // how many bytes in this resource?
  42.     DWORD dwImageOffeset; // the ID
  43. } ICONDIRENTRY;
  44.  
  45. typedef struct
  46. {
  47.     WORD idReserved; // Reserved (must be 0)
  48.     WORD idType; // Resource type (1 for icons)
  49.     WORD idCount; // How many images?
  50.     ICONDIRENTRY idEntries[1]; // The entries for each image
  51. } ICONDIR;
  52.  
  53.  
  54. typedef struct
  55. {
  56.     BYTE bWidth; // Width, in pixels, of the image
  57.     BYTE bHeight; // Height, in pixels, of the image
  58.     BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  59.     BYTE bReserved; // Reserved
  60.     WORD wPlanes; // Color Planes
  61.     WORD wBitCount; // Bits per pixel
  62.     DWORD dwBytesInRes; // how many bytes in this resource?
  63.     WORD nID; // the ID
  64. } GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
  65.  
  66. typedef struct
  67. {
  68.     WORD idReserved; // Reserved (must be 0)
  69.     WORD idType; // Resource type (1 for icons)
  70.     WORD idCount; // How many images?
  71.     GRPICONDIRENTRY idEntries[1]; // The entries for each image
  72. } GRPICONDIR, *LPGRPICONDIR;
  73.  
  74. BOOL ReplaceResource(HANDLE hUpdate, LPCTSTR lpType, LPCTSTR lpName, LPVOID lpData, DWORD cbData);
  75.  
  76. int _tmain(int argc, _TCHAR* argv[])
  77. {
  78.     HANDLE hUpdateRes = NULL;  // update resource handle
  79.     LPVOID lpResLock = NULL;   // pointer to resource data
  80.     int result = 0;
  81.     LPTSTR type = NULL;
  82.     LPTSTR name = NULL;
  83.     BYTE* temp = NULL;
  84.     GRPICONDIR* newDir = NULL;
  85.  
  86.     if ( argc < 4 )
  87.     {
  88.         printf("Invalid number of arguments!\n");
  89.         printf("Usage:\n\treplaceicon <path to executable> <path to the new ico file> <IDI_ICON to replace>\n\n"
  90.                "For example:\n\treplaceicon C:\rhodes.exe C:\newicon.ico 114");
  91.         result = -1;
  92.         goto cleanup;
  93.     }
  94.  
  95.     //read  IDI_ICON
  96.     int IDI_ICON = atoi(argv[3]);
  97.  
  98.     HANDLE inFile = CreateFile(argv[2], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  99.     if (inFile == INVALID_HANDLE_VALUE)
  100.     {
  101.         printf("Error cannot open ico file: %s\n", argv[2]);
  102.         result = -1;
  103.         goto cleanup;
  104.     }
  105.  
  106.     // Open the file to which we want to change icon
  107.     hUpdateRes = BeginUpdateResource(argv[1], FALSE);
  108.     if (hUpdateRes == NULL)
  109.     {
  110.         printf("Could not open file for writing: %s\n", argv[1]);
  111.         result = -1;
  112.         goto cleanup;
  113.     }
  114.  
  115.     DWORD read;
  116.     DWORD size = GetFileSize(inFile, NULL);
  117.     temp = (BYTE*)GlobalAlloc(GMEM_FIXED, size);
  118.     ReadFile(inFile, temp, size, &read, NULL);
  119.  
  120.     // Change the icon group
  121.     ICONDIR* ig = (ICONDIR*)(temp);
  122.  
  123.     DWORD newSize = sizeof(GRPICONDIR) + (sizeof(GRPICONDIRENTRY)*(ig->idCount - 1));
  124.     newDir = (GRPICONDIR*)GlobalAlloc(GMEM_FIXED, newSize);
  125.     newDir->idReserved = ig->idReserved;
  126.     newDir->idType = ig->idType;
  127.     newDir->idCount = ig->idCount;
  128.  
  129.     DWORD rv;
  130.     for (int i = 0; i < ig->idCount; i++)
  131.     {
  132.         BYTE* temp1 = temp + ig->idEntries[i].dwImageOffeset;
  133.         DWORD size1 = ig->idEntries[i].dwBytesInRes;
  134.  
  135.         newDir->idEntries[i].bWidth = ig->idEntries[i].bWidth;
  136.         newDir->idEntries[i].bHeight = ig->idEntries[i].bHeight;
  137.         newDir->idEntries[i].bColorCount = ig->idEntries[i].bColorCount;
  138.         newDir->idEntries[i].bReserved = ig->idEntries[i].bReserved;
  139.         newDir->idEntries[i].wPlanes = ig->idEntries[i].wPlanes;
  140.         newDir->idEntries[i].wBitCount = ig->idEntries[i].wBitCount;
  141.         newDir->idEntries[i].dwBytesInRes = ig->idEntries[i].dwBytesInRes;
  142.         newDir->idEntries[i].nID = i + 1;
  143.  
  144.         type = RT_ICON;
  145.         name = MAKEINTRESOURCE(i + 1);
  146.         if (TRUE == (rv = ReplaceResource(hUpdateRes, type, name, (LPVOID)temp1, size1 )))
  147.         {
  148.             printf("Icon %d replaced OK!\n", i + 1);
  149.         }
  150.         else
  151.         {
  152.             printf("Replacing the icon %d group FAILED!\n", i + 1);
  153.         }
  154.     }
  155.  
  156.     type = RT_GROUP_ICON;
  157.     name = MAKEINTRESOURCE(IDI_ICON);
  158.  
  159.     if (TRUE == (rv = ReplaceResource(hUpdateRes, type, name, (LPVOID)newDir, newSize)))
  160.     {
  161.         printf("Icon group replaced OK!\n");
  162.     } else
  163.     {
  164.         printf("Replacing the icon group FAILED!\n");
  165.     }
  166.  
  167.     // Write changes to EXE and then close it.
  168.     if (!EndUpdateResource(hUpdateRes, FALSE))
  169.         printf("Could not write changes to file.");
  170.  
  171. cleanup:
  172.     if ( inFile != NULL && inFile != INVALID_HANDLE_VALUE)
  173.         CloseHandle(inFile);
  174.  
  175.     if ( newDir != NULL )
  176.         GlobalFree(newDir);
  177.  
  178.     if ( temp != NULL )
  179.         GlobalFree(temp);
  180.  
  181.     return result;
  182. }
  183.  
  184. BOOL ReplaceResource(HANDLE hUpdate, LPCTSTR lpType, LPCTSTR lpName, LPVOID lpData, DWORD cbData)
  185. {
  186.  
  187.     return UpdateResource(hUpdate,                          // update resource handle
  188.                lpType,                                      // change dialog box resource
  189.                lpName,                                      // dialog box id
  190.                MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),   // neutral language
  191.                lpData,                                      // ptr to resource data
  192.                cbData);                                     // size of resource data
  193.  
  194. };
Advertisement
Add Comment
Please, Sign In to add comment