WetCode

Untitled

Dec 3rd, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 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; // I need to pass this to the BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam) so i can set the name of the icon
  76.     // So i can use it FindResource( hExe, MAKEINTRESOURCE(1 /* IconGrpName */  ), RT_GROUP_ICON );
  77.  
  78.     // Credit to my friend ***  thanks for your help and explination :)
  79.     HMODULE hExe = LoadLibrary(TEXT("TestRes.exe"));
  80.     EnumResourceTypes(hExe, (ENUMRESTYPEPROC)ResTypes, 0);
  81.  
  82.     // Find the group resource which lists its images
  83.                                 /* Need to set "IconGrpName" this from ResTypee */
  84.     HRSRC hRsrc = FindResource( hExe, MAKEINTRESOURCE(1), RT_GROUP_ICON );
  85.     // Load and Lock to get a pointer to a GRPICONDIR
  86.     HGLOBAL hGlobal = LoadResource( hExe, hRsrc );
  87.     GRPICONDIR *lpGrpIconDir = (GRPICONDIR*)LockResource( hGlobal );
  88.     // We now have the header of the icon file now we need all the icons.
  89.     vector<ICONIMAGE*> vec_lpIconImage; // vector for storage.
  90.                          // The Number of icons.
  91.     for ( int i = 0; i < lpGrpIconDir->idCount; i++ )
  92.     {
  93.         // Using an ID from the group, Find, Load and Lock the RT_ICON
  94.         hRsrc = FindResource( hExe, MAKEINTRESOURCE( lpGrpIconDir->idEntries[i].nID ), RT_ICON );
  95.         hGlobal = LoadResource( hExe, hRsrc );
  96.         ICONIMAGE *lpIconImage = (ICONIMAGE*)LockResource( hGlobal );
  97.         vec_lpIconImage.push_back( lpIconImage ); // Place it in the vector.
  98.         // Here, lpIconImage points to an ICONIMAGE structure
  99.     }
  100.  
  101.     // Here is where things get wierd checking the reults i have som realy HUGE vec_lpIconImage[i]->icHeader.biWidth x vec_lpIconImage[i]->icHeader.biHeight
  102.     for ( unsigned i = 0; i < vec_lpIconImage.size(); i++ )
  103.     {
  104.         cout << "Width x Height: " << vec_lpIconImage[i]->icHeader.biWidth << "x" << vec_lpIconImage[i]->icHeader.biHeight << endl;
  105.         cout << "Size: " << vec_lpIconImage[i]->icHeader.biSizeImage << endl;
  106.     }
  107.  
  108.     system("PAUSE");
  109.     return 0;
  110. }
  111.  
  112. // Credit to my friend ***  thanks for your help and explination :)
  113. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  114. {
  115.     if (!IS_INTRESOURCE(lpType))
  116.     {
  117.         if( lpType == (RT_ICON + 11))
  118.         {
  119.             std::cout<<"Icon Group"<< lpType << "\n";
  120.         }
  121.     } else {
  122.         if( lpType == (RT_ICON + 11))
  123.         {
  124.             std::cout<<"Icon Group (short): "<< (short)lpType << "\n";    
  125.         }
  126.     }
  127.  
  128.     if( lpType == RT_ICON) // Icon Resource Type
  129.     {
  130.         EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  131.     }
  132.     return true;
  133. }
  134.  
  135. // Credit to my friend ***  thanks for your help and explination :)
  136. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  137. {
  138.     if (!IS_INTRESOURCE(lpName))
  139.     {
  140.         if( lpType == RT_ICON)
  141.             std::cout<<"Icon: "<< lpType << "\n";
  142.  
  143.     } else {
  144.         if( lpType == RT_ICON)
  145.             std::cout<<"Icon (short): "<< (short)lpType << "\n";
  146.     }
  147.  
  148.     return true;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment