WetCode

Untitled

Dec 3rd, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 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. typedef struct
  30. {
  31.     BYTE        bWidth;          // Width, in pixels, of the image
  32.     BYTE        bHeight;         // Height, in pixels, of the image
  33.     BYTE        bColorCount;     // Number of colors in image (0 if >=8bpp)
  34.     BYTE        bReserved;       // Reserved ( must be 0)
  35.     WORD        wPlanes;         // Color Planes
  36.     WORD        wBitCount;       // Bits per pixel
  37.     DWORD       dwBytesInRes;    // How many bytes in this resource?
  38.     DWORD       dwImageOffset;   // Where in the file is this image?
  39. } ICONDIRENTRY, *LPICONDIRENTRY;
  40.  
  41. // #pragmas are used here to insure that the structure's
  42. // packing in memory matches the packing of the EXE or DLL.
  43. #pragma pack( push )
  44. #pragma pack( 2 )
  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.     GRPICONDIRENTRY   idEntries[1]; // The entries for each image
  51. } GRPICONDIR, *LPGRPICONDIR;
  52. #pragma pack( pop )
  53.  
  54.  
  55. typedef struct {
  56.     WORD           idReserved;   // Reserved (must be 0)
  57.     WORD           idType;       // Resource Type (1 for icons)
  58.     WORD           idCount;      // How many images?
  59.     ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
  60. } ICONDIR, *LPICONDIR;
  61.  
  62. typedef struct
  63. {
  64.     BITMAPINFOHEADER   icHeader;   // DIB header
  65.     RGBQUAD         icColors[1];   // Color table
  66.     BYTE            icXOR[1];      // DIB bits for XOR mask
  67.     BYTE            icAND[1];      // DIB bits for AND mask
  68. } ICONIMAGE, *LPICONIMAGE;
  69.  
  70. typedef BOOL (CALLBACK *EnumResNameProc)( HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam);
  71.  
  72. int main(int argc, char* argv[])
  73. {
  74.    
  75.     LPCSTR IconGrpName = NULL;
  76.     // Credit to my friend *** thanks for you help and explination :)
  77.     HMODULE hExe = LoadLibrary(TEXT("TestRes.exe"));
  78.     EnumResourceTypes(hExe, (ENUMRESTYPEPROC)ResTypes, 0);
  79.  
  80.  
  81.     // Find the group resource which lists its images
  82.     HRSRC hRsrc = FindResource( hExe, MAKEINTRESOURCE(1), RT_GROUP_ICON );
  83.     // Load and Lock to get a pointer to a GRPICONDIR
  84.     HGLOBAL hGlobal = LoadResource( hExe, hRsrc );
  85.     GRPICONDIR *lpGrpIconDir = (GRPICONDIR*)LockResource( hGlobal );
  86.    
  87.     vector<ICONIMAGE*> vec_lpIconImage;
  88.  
  89.     for ( int i = 0; i < lpGrpIconDir->idCount; i++ )
  90.     {
  91.         // Using an ID from the group, Find, Load and Lock the RT_ICON
  92.         hRsrc = FindResource( hExe, MAKEINTRESOURCE( lpGrpIconDir->idEntries[i].nID ), RT_ICON );
  93.         hGlobal = LoadResource( hExe, hRsrc );
  94.         ICONIMAGE *lpIconImage = (ICONIMAGE*)LockResource( hGlobal );
  95.         vec_lpIconImage.push_back( lpIconImage );
  96.         // Here, lpIconImage points to an ICONIMAGE structure
  97.     }
  98.  
  99.     for ( unsigned i = 0; i < vec_lpIconImage.size(); i++ )
  100.     {
  101.         cout << "Width x Height: " << vec_lpIconImage[i]->icHeader.biWidth << "x" << vec_lpIconImage[i]->icHeader.biHeight << endl;
  102.         cout << "Size: " << vec_lpIconImage[i]->icHeader.biSizeImage << endl;
  103.     }
  104.  
  105.     system("PAUSE");
  106.     return 0;
  107. }
  108.  
  109. // Credit to my friend *** thanks for you help and explination :)
  110. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  111. {
  112.     if (!IS_INTRESOURCE(lpType))
  113.     {
  114.         if( lpType == (RT_ICON + 11))
  115.         {
  116.             std::cout<<"Icon Group"<< lpType << "\n";
  117.         }
  118.     } else {
  119.         if( lpType == (RT_ICON + 11))
  120.         {
  121.             std::cout<<"Icon Group (short): "<< (short)lpType << "\n";    
  122.         }
  123.     }
  124.  
  125.     if( lpType == RT_ICON) // Icon Resource Type
  126.     {
  127.         EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  128.     }
  129.     return true;
  130. }
  131.  
  132. // Credit to my friend *** thanks for you elp and explination :)
  133. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  134. {
  135.     if (!IS_INTRESOURCE(lpName))
  136.     {
  137.         if( lpType == RT_ICON)
  138.             std::cout<<"Icon: "<< lpType << "\n";
  139.  
  140.     } else {
  141.         if( lpType == RT_ICON)
  142.             std::cout<<"Icon (short): "<< (short)lpType << "\n";
  143.     }
  144.  
  145.     return true;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment