Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Resources.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <Windows.h>
- using namespace std;
- BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam);
- BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam);
- #pragma pack( push )
- #pragma pack( 2 )
- typedef struct
- {
- BYTE bWidth; // Width, in pixels, of the image
- BYTE bHeight; // Height, in pixels, of the image
- BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
- BYTE bReserved; // Reserved
- WORD wPlanes; // Color Planes
- WORD wBitCount; // Bits per pixel
- DWORD dwBytesInRes; // how many bytes in this resource?
- WORD nID; // the ID
- } GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
- #pragma pack( pop )
- // #pragmas are used here to insure that the structure's
- // packing in memory matches the packing of the EXE or DLL.
- #pragma pack( push )
- #pragma pack( 2 )
- typedef struct
- {
- WORD idReserved; // Reserved (must be 0)
- WORD idType; // Resource type (1 for icons)
- WORD idCount; // How many images?
- GRPICONDIRENTRY idEntries[1]; // The entries for each image
- } GRPICONDIR, *LPGRPICONDIR;
- #pragma pack( pop )
- typedef struct
- {
- BYTE bWidth; // Width, in pixels, of the image
- BYTE bHeight; // Height, in pixels, of the image
- BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
- BYTE bReserved; // Reserved ( must be 0)
- WORD wPlanes; // Color Planes
- WORD wBitCount; // Bits per pixel
- DWORD dwBytesInRes; // How many bytes in this resource?
- DWORD dwImageOffset; // Where in the file is this image?
- } ICONDIRENTRY, *LPICONDIRENTRY;
- typedef struct {
- WORD idReserved; // Reserved (must be 0)
- WORD idType; // Resource Type (1 for icons)
- WORD idCount; // How many images?
- ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
- } ICONDIR, *LPICONDIR;
- typedef struct
- {
- BITMAPINFOHEADER icHeader; // DIB header
- RGBQUAD icColors[1]; // Color table
- BYTE icXOR[1]; // DIB bits for XOR mask
- BYTE icAND[1]; // DIB bits for AND mask
- } ICONIMAGE, *LPICONIMAGE;
- LPTSTR IconGrpName = NULL;
- int main(int argc, char* argv[])
- {
- HMODULE hExe = LoadLibrary("TestRes.exe");
- EnumResourceTypes(hExe, (ENUMRESTYPEPROC)ResTypes, 0);
- // Find the group resource which lists its images
- /* Need to set "IconGrpName" this from ResTypee */
- DWORD dwIconGrpSize = 0;
- HRSRC hRsrc = FindResource( hExe, MAKEINTRESOURCE(1), RT_GROUP_ICON );
- // Load and Lock to get a pointer to a GRPICONDIR
- HGLOBAL hGlobal = LoadResource( hExe, hRsrc );
- dwIconGrpSize = SizeofResource( hExe, hRsrc );
- GRPICONDIR *lpIconGroup = new GRPICONDIR[ dwIconGrpSize ];
- RtlZeroMemory( lpIconGroup, dwIconGrpSize );
- lpIconGroup = (GRPICONDIR*)LockResource( hGlobal );
- // We now have the header of the icon file now we need all the icons.
- cout << "Icon Count: " << lpIconGroup->idCount << endl;
- cout << "Type: " << lpIconGroup->idType << endl;
- cout << "idReserved: " << lpIconGroup->idReserved << endl;
- for ( unsigned i = 0; i < lpIconGroup->idCount; i++ )
- {
- cout << "\tEntry ID: " << lpIconGroup->idEntries[i].nID << endl;
- cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bHeight << endl;
- cout << "\tEntry Height: " << (int)lpIconGroup->idEntries[i].bWidth << endl;
- cout << "\tEntry Size: " << lpIconGroup->idEntries[i].dwBytesInRes << endl << endl;
- }
- vector<ICONIMAGE*> vec_ICONIMAGE;
- for ( int i = 0; i < lpIconGroup->idCount; i++ )
- {
- HRSRC hrSrc = FindResource( hExe, MAKEINTRESOURCE( lpIconGroup->idEntries[i].nID ), RT_ICON );
- HGLOBAL hGlobEntry = LoadResource( hExe, hrSrc );
- ICONIMAGE *lpIconImage = new ICONIMAGE[ SizeofResource( hExe, hrSrc ) ];
- RtlZeroMemory( lpIconImage, SizeofResource( hExe, hrSrc ) );
- lpIconImage = (ICONIMAGE*)LockResource( hGlobEntry );
- vec_ICONIMAGE.push_back( lpIconImage );
- }
- ICONDIR *lpIconDir = new ICONDIR[ sizeof( ICONDIR ) ];
- RtlZeroMemory( lpIconDir, sizeof( ICONDIR ) );
- lpIconDir->idCount = lpIconGroup->idCount;
- lpIconDir->idType = lpIconGroup->idType;
- lpIconDir->idReserved = 0;
- DWORD dwTotalSize = 0;
- DWORD dwTotalOffset = 0;
- for ( int i = 0; i < lpIconDir->idCount; i++ )
- {
- lpIconDir->idEntries[i].bColorCount = lpIconGroup->idEntries[i].bColorCount;
- lpIconDir->idEntries[i].bHeight = lpIconGroup->idEntries[i].bHeight;
- lpIconDir->idEntries[i].bReserved = lpIconGroup->idEntries[i].bReserved;
- lpIconDir->idEntries[i].bWidth = lpIconGroup->idEntries[i].bWidth;
- lpIconDir->idEntries[i].dwBytesInRes = lpIconGroup->idEntries[i].dwBytesInRes;
- lpIconDir->idEntries[i].dwImageOffset = lpIconGroup->idEntries[i].dwBytesInRes;
- lpIconDir->idEntries[i].wBitCount = lpIconGroup->idEntries[i].wBitCount;
- lpIconDir->idEntries[i].wPlanes = lpIconGroup->idEntries[i].wPlanes;
- dwTotalSize += lpIconGroup->idEntries[i].dwBytesInRes;
- dwTotalOffset += lpIconDir->idEntries[i].dwBytesInRes;
- }
- DWORD dwPos = 0;
- cout << "Total Size of .ICO from Resource.EXE: " << dwTotalSize << endl;
- cout << "Total Offset: " << dwTotalOffset << endl;
- char *lpIconFileBuffer = new char[ dwTotalSize ];
- CopyMemory( lpIconFileBuffer, lpIconDir, sizeof( ICONDIR ) );
- for ( int i = 0; i < lpIconDir->idCount; i++ )
- {
- cout << "Writen: " << dwPos << endl;
- CopyMemory( &lpIconFileBuffer[ dwPos ], vec_ICONIMAGE[i], lpIconDir->idEntries[i].dwBytesInRes );
- dwPos += lpIconDir->idEntries[i].dwImageOffset;
- }
- cout << "Bytes Writen: " << dwPos << endl;
- HANDLE hFile = CreateFile("test.ico", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- cout << "Error Code: " << GetLastError() << " > Failed to create output file aborting." << endl;
- CloseHandle(hFile);
- } else {
- cout << "Created " << "test.ico please wait..." << endl;
- }
- DWORD bytesWriten = 0;
- if (!WriteFile(hFile, lpIconFileBuffer, dwPos, &bytesWriten, NULL))
- {
- cout << "Error Code: " << GetLastError() << " > An error acured when writing file to disk abort." << endl;
- CloseHandle(hFile);
- } else {
- cout << ".";
- }
- CloseHandle(hFile);
- system("PAUSE");
- return 0;
- }
- // Credit to my friend *** thanks for your help and explination :)
- BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
- {
- if (!IS_INTRESOURCE(lpType))
- {
- if( lpType == (RT_ICON + 11))
- {
- std::cout<<"Icon Group"<< lpType << "\n";
- IconGrpName = lpType;
- }
- } else {
- if( lpType == (RT_ICON + 11))
- {
- std::cout<<"Icon Group (short): "<< (short)lpType << "\n";
- IconGrpName = lpType;
- }
- }
- if( lpType == RT_ICON) // Icon Resource Type
- {
- EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
- }
- return true;
- }
- // Credit to my friend *** thanks for your help and explination :)
- BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
- {
- if (!IS_INTRESOURCE(lpName))
- {
- if( lpType == RT_ICON)
- std::cout<<"Icon: "<< lpType << "\n";
- } else {
- if( lpType == RT_ICON)
- std::cout<<"Icon (short): "<< (short)lpType << "\n";
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement