WetCode

Untitled

Dec 4th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.16 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.     /*
  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.     HMODULE hExe = LoadLibraryEx("stubpe.exe", NULL, LOAD_LIBRARY_AS_DATAFILE );
  80.     DWORD dwIconGrpSize = 0;
  81.     HRSRC hRsrc = FindResource( hExe, MAKEINTRESOURCE(1), RT_GROUP_ICON );
  82.     // Load and Lock to get a pointer to a GRPICONDIR
  83.     HGLOBAL hGlobal = LoadResource( hExe, hRsrc );
  84.     dwIconGrpSize = SizeofResource( hExe, hRsrc );
  85.     GRPICONDIR *lpIconGroup = new GRPICONDIR[ dwIconGrpSize ];
  86.     RtlZeroMemory( lpIconGroup, dwIconGrpSize );
  87.     lpIconGroup = (GRPICONDIR*)LockResource( hGlobal );
  88.     // We now have the header of the icon file now we need all the icons.
  89.    
  90.     cout << "Icon Count: " << lpIconGroup->idCount << endl;
  91.     cout << "Type: " << lpIconGroup->idType  << endl;
  92.     cout << "idReserved: " << lpIconGroup->idReserved << endl;
  93.     for ( unsigned i = 0; i < lpIconGroup->idCount; i++ )
  94.     {
  95.         cout << "\tEntry ID: " << lpIconGroup->idEntries[i].nID << endl;
  96.         cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bHeight << endl;
  97.         cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bWidth << endl;
  98.         cout << "\tEntry Size: " << lpIconGroup->idEntries[i].dwBytesInRes << endl << endl;
  99.     }
  100.  
  101.     vector<ICONIMAGE*> vec_ICONIMAGE;
  102.     for ( int i = 0; i < lpIconGroup->idCount; i++ )
  103.     {
  104.         HRSRC hrSrc = FindResource( hExe, MAKEINTRESOURCE( lpIconGroup->idEntries[i].nID ), RT_ICON );
  105.         HGLOBAL hGlobEntry = LoadResource( hExe, hrSrc );
  106.         ICONIMAGE *lpIconImage = new ICONIMAGE[ lpIconGroup->idEntries[i].dwBytesInRes ];
  107.         RtlZeroMemory( lpIconImage,  lpIconGroup->idEntries[i].dwBytesInRes );
  108.         lpIconImage = (ICONIMAGE*)LockResource( hGlobEntry );
  109.  
  110.         vec_ICONIMAGE.push_back( lpIconImage );
  111.     }
  112.  
  113.     HANDLE hFile = CreateFile("test.ico", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  114.     if (hFile == INVALID_HANDLE_VALUE)
  115.     {
  116.         cout << "Error Code: " << GetLastError() << " > Failed to create output file aborting." << endl;
  117.         CloseHandle(hFile);
  118.     } else {
  119.         cout << "Created " << "test.ico please wait..." << endl;
  120.         }
  121.     DWORD bytesWriten = 0;
  122.     WriteFile(hFile, &(lpIconGroup->idReserved), sizeof( WORD ), &bytesWriten, NULL);
  123.  
  124.     WriteFile(hFile, &(lpIconGroup->idType), sizeof( WORD ), &bytesWriten, NULL );
  125.    
  126.     WriteFile( hFile, &(lpIconGroup->idCount), sizeof( WORD ), &bytesWriten, NULL );
  127.  
  128.     WriteFile( hFile, lpIconGroup->idEntries, lpIconGroup->idCount * sizeof(ICONDIRENTRY), &bytesWriten, NULL );
  129.  
  130.     for (int i =0;i<lpIconGroup->idCount;i++)
  131.     {
  132.         ReadFile( hFile, vec_ICONIMAGE[i], lpIconGroup->idEntries[i].wBitCount,
  133.                 &bytesWriten, NULL );
  134.       // Here, pIconImage is an ICONIMAGE structure. Party on it :)
  135.     }
  136.  
  137.     CloseHandle(hFile);
  138.  
  139.     system("PAUSE");
  140.     return 0;
  141. }
  142.  
  143. // Credit to my friend ***  thanks for your help and explination :)
  144. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  145. {
  146.     if (!IS_INTRESOURCE(lpType))
  147.     {
  148.         if( lpType == (RT_ICON + 11))
  149.         {
  150.             std::cout<<"Icon Group"<< lpType << "\n";
  151.             IconGrpName = lpType;
  152.         }
  153.     } else {
  154.         if( lpType == (RT_ICON + 11))
  155.         {
  156.             std::cout<<"Icon Group (short): "<< (short)lpType << "\n";    
  157.             IconGrpName = lpType;
  158.         }
  159.     }
  160.  
  161.     if( lpType == RT_ICON) // Icon Resource Type
  162.     {
  163.         EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  164.     }
  165.     return true;
  166. }
  167.  
  168. // Credit to my friend ***  thanks for your help and explination :)
  169. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  170. {
  171.     if (!IS_INTRESOURCE(lpName))
  172.     {
  173.         if( lpType == RT_ICON)
  174.             std::cout<<"Icon: "<< lpType << "\n";
  175.  
  176.     } else {
  177.         if( lpType == RT_ICON)
  178.             std::cout<<"Icon (short): "<< (short)lpType << "\n";
  179.     }
  180.  
  181.     return true;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment