Advertisement
WetCode

Untitled

Dec 4th, 2013
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.35 KB | None | 0 0
  1. // Resources.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <Windows.h>
  8.  
  9. using namespace std;
  10.  
  11. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam);
  12. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam);
  13.  
  14. #pragma pack( push )
  15. #pragma pack( 2 )
  16. typedef struct
  17. {
  18.     BYTE   bWidth;               // Width, in pixels, of the image
  19.     BYTE   bHeight;              // Height, in pixels, of the image
  20.     BYTE   bColorCount;          // Number of colors in image (0 if >=8bpp)
  21.     BYTE   bReserved;            // Reserved
  22.     WORD   wPlanes;              // Color Planes
  23.     WORD   wBitCount;            // Bits per pixel
  24.     DWORD   dwBytesInRes;        // how many bytes in this resource?
  25.     WORD   nID;                  // the ID
  26. } GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
  27. #pragma pack( pop )
  28.  
  29. // #pragmas are used here to insure that the structure's
  30. // packing in memory matches the packing of the EXE or DLL.
  31. #pragma pack( push )
  32. #pragma pack( 2 )
  33. typedef struct
  34. {
  35.     WORD            idReserved;     // Reserved (must be 0)
  36.     WORD            idType;         // Resource type (1 for icons)
  37.     WORD            idCount;            // How many images?
  38.     GRPICONDIRENTRY   idEntries[1]; // The entries for each image
  39. } GRPICONDIR, *LPGRPICONDIR;
  40. #pragma pack( pop )
  41.  
  42. typedef struct
  43. {
  44.     BYTE        bWidth;          // Width, in pixels, of the image
  45.     BYTE        bHeight;         // Height, in pixels, of the image
  46.     BYTE        bColorCount;     // Number of colors in image (0 if >=8bpp)
  47.     BYTE        bReserved;       // Reserved ( must be 0)
  48.     WORD        wPlanes;         // Color Planes
  49.     WORD        wBitCount;       // Bits per pixel
  50.     DWORD       dwBytesInRes;    // How many bytes in this resource?
  51.     DWORD       dwImageOffset;   // Where in the file is this image?
  52. } ICONDIRENTRY, *LPICONDIRENTRY;
  53.  
  54. typedef struct {
  55.     WORD           idReserved;   // Reserved (must be 0)
  56.     WORD           idType;       // Resource Type (1 for icons)
  57.     WORD           idCount;      // How many images?
  58.     ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
  59. } ICONDIR, *LPICONDIR;
  60.  
  61. typedef struct
  62. {
  63.     BITMAPINFOHEADER   icHeader;   // DIB header
  64.     RGBQUAD         icColors[1];   // Color table
  65.     BYTE            icXOR[1];      // DIB bits for XOR mask
  66.     BYTE            icAND[1];      // DIB bits for AND mask
  67. } ICONIMAGE, *LPICONIMAGE;
  68.  
  69. LPTSTR IconGrpName = NULL;
  70.  
  71. int main(int argc, char* argv[])
  72. {
  73.     HMODULE hExe = LoadLibrary("TestRes.exe");
  74.     EnumResourceTypes(hExe, (ENUMRESTYPEPROC)ResTypes, 0);
  75.  
  76.     // Find the group resource which lists its images
  77.                                 /* Need to set "IconGrpName" this from ResTypee */
  78.  
  79.     DWORD dwIconGrpSize = 0;
  80.     HRSRC hRsrc = FindResource( hExe, MAKEINTRESOURCE(1), RT_GROUP_ICON );
  81.     // Load and Lock to get a pointer to a GRPICONDIR
  82.     HGLOBAL hGlobal = LoadResource( hExe, hRsrc );
  83.     dwIconGrpSize = SizeofResource( hExe, hRsrc );
  84.     GRPICONDIR *lpIconGroup = new GRPICONDIR[ dwIconGrpSize ];
  85.     RtlZeroMemory( lpIconGroup, dwIconGrpSize );
  86.     lpIconGroup = (GRPICONDIR*)LockResource( hGlobal );
  87.     // We now have the header of the icon file now we need all the icons.
  88.    
  89.     cout << "Icon Count: " << lpIconGroup->idCount << endl;
  90.     cout << "Type: " << lpIconGroup->idType  << endl;
  91.     cout << "idReserved: " << lpIconGroup->idReserved << endl;
  92.     for ( unsigned i = 0; i < lpIconGroup->idCount; i++ )
  93.     {
  94.         cout << "\tEntry ID: " << lpIconGroup->idEntries[i].nID << endl;
  95.         cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bHeight << endl;
  96.         cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bWidth << endl;
  97.         cout << "\tEntry Size: " << lpIconGroup->idEntries[i].dwBytesInRes << endl << endl;
  98.     }
  99.  
  100.     vector<ICONIMAGE*> vec_ICONIMAGE;
  101.     for ( int i = 0; i < lpIconGroup->idCount; i++ )
  102.     {
  103.         HRSRC hrSrc = FindResource( hExe, MAKEINTRESOURCE( lpIconGroup->idEntries[i].nID ), RT_ICON );
  104.         HGLOBAL hGlobEntry = LoadResource( hExe, hrSrc );
  105.         ICONIMAGE *lpIconImage = new ICONIMAGE[ SizeofResource( hExe, hrSrc ) ];
  106.         RtlZeroMemory( lpIconImage, SizeofResource( hExe, hrSrc ) );
  107.         lpIconImage = (ICONIMAGE*)LockResource( hGlobEntry );
  108.  
  109.         vec_ICONIMAGE.push_back( lpIconImage );
  110.     }
  111.  
  112.     ICONDIR *lpIconDir = new ICONDIR[ sizeof( ICONDIR ) ];
  113.     RtlZeroMemory( lpIconDir,  sizeof( ICONDIR ) );
  114.  
  115.     lpIconDir->idCount = lpIconGroup->idCount;
  116.     lpIconDir->idType = lpIconGroup->idType;
  117.     lpIconDir->idReserved = 0;
  118.  
  119.     DWORD dwTotalSize = 0;
  120.     DWORD dwTotalOffset = 0;
  121.  
  122.     for ( int i = 0; i < lpIconDir->idCount; i++ )
  123.     {
  124.         lpIconDir->idEntries[i].bColorCount = lpIconGroup->idEntries[i].bColorCount;
  125.         lpIconDir->idEntries[i].bHeight = lpIconGroup->idEntries[i].bHeight;
  126.         lpIconDir->idEntries[i].bReserved = lpIconGroup->idEntries[i].bReserved;
  127.         lpIconDir->idEntries[i].bWidth = lpIconGroup->idEntries[i].bWidth;
  128.         lpIconDir->idEntries[i].dwBytesInRes = lpIconGroup->idEntries[i].dwBytesInRes;
  129.         lpIconDir->idEntries[i].dwImageOffset = lpIconGroup->idEntries[i].dwBytesInRes;
  130.         lpIconDir->idEntries[i].wBitCount = lpIconGroup->idEntries[i].wBitCount;
  131.         lpIconDir->idEntries[i].wPlanes = lpIconGroup->idEntries[i].wPlanes;
  132.  
  133.         dwTotalSize += lpIconGroup->idEntries[i].dwBytesInRes;
  134.         dwTotalOffset += lpIconDir->idEntries[i].dwBytesInRes;
  135.  
  136.     }
  137.     DWORD dwPos = 0;
  138.     cout << "Total Size of .ICO from Resource.EXE: " << dwTotalSize << endl;
  139.     cout << "Total Offset: " << dwTotalOffset << endl;
  140.  
  141.     char *lpIconFileBuffer = new char[ dwTotalSize ];
  142.     CopyMemory( lpIconFileBuffer, lpIconDir, sizeof( ICONDIR ) );
  143.     for ( int i = 0; i < lpIconDir->idCount; i++ )
  144.     {
  145.         cout << "Writen: " << dwPos << endl;
  146.         CopyMemory( &lpIconFileBuffer[ dwPos ], vec_ICONIMAGE[i], lpIconDir->idEntries[i].dwBytesInRes );
  147.         dwPos += lpIconDir->idEntries[i].dwImageOffset;
  148.     }
  149.     cout << "Bytes Writen: " << dwPos << endl;
  150.  
  151.     HANDLE hFile = CreateFile("test.ico", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  152.     if (hFile == INVALID_HANDLE_VALUE)
  153.     {
  154.         cout << "Error Code: " << GetLastError() << " > Failed to create output file aborting." << endl;
  155.         CloseHandle(hFile);
  156.     } else {
  157.         cout << "Created " << "test.ico please wait..." << endl;
  158.         }
  159.     DWORD bytesWriten = 0;
  160.     if (!WriteFile(hFile, lpIconFileBuffer, dwPos, &bytesWriten, NULL))
  161.     {
  162.         cout << "Error Code: " << GetLastError() << " > An error acured when writing file to disk abort." << endl;
  163.         CloseHandle(hFile);
  164.     } else {
  165.         cout << ".";
  166.         }
  167.  
  168.     CloseHandle(hFile);
  169.  
  170.     system("PAUSE");
  171.     return 0;
  172. }
  173.  
  174. // Credit to my friend ***  thanks for your help and explination :)
  175. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  176. {
  177.     if (!IS_INTRESOURCE(lpType))
  178.     {
  179.         if( lpType == (RT_ICON + 11))
  180.         {
  181.             std::cout<<"Icon Group"<< lpType << "\n";
  182.             IconGrpName = lpType;
  183.         }
  184.     } else {
  185.         if( lpType == (RT_ICON + 11))
  186.         {
  187.             std::cout<<"Icon Group (short): "<< (short)lpType << "\n";    
  188.             IconGrpName = lpType;
  189.         }
  190.     }
  191.  
  192.     if( lpType == RT_ICON) // Icon Resource Type
  193.     {
  194.         EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  195.     }
  196.     return true;
  197. }
  198.  
  199. // Credit to my friend ***  thanks for your help and explination :)
  200. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  201. {
  202.     if (!IS_INTRESOURCE(lpName))
  203.     {
  204.         if( lpType == RT_ICON)
  205.             std::cout<<"Icon: "<< lpType << "\n";
  206.  
  207.     } else {
  208.         if( lpType == RT_ICON)
  209.             std::cout<<"Icon (short): "<< (short)lpType << "\n";
  210.     }
  211.  
  212.     return true;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement