Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* =====================================================================================
- Icon Changer Project 2013
- by
- dkwhite & WetCode
- Copyright (c) 2013 dkwhite & WetCode
- Permission to use, copy, modify and distribute this software for personal and educational use is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of dkwhite & WetCode authors are not used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. dkwhite & WetCode the authors make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.
- dkwhite & WetCode THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL dkwhite & WetCode THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- ======================================================================================================== */
- // IconChanger.h
- #ifndef ICONCHANGER_H
- #define ICONCHANGER_H
- #include <windows.h>
- #include <tchar.h>
- #include <string>
- #include <iostream>
- #include <vector>
- typedef struct tagHEADER
- {
- WORD idReserved;
- WORD idType;
- WORD idCount;
- }HEADER, *LPHEADER;
- typedef struct tagICONDIRENTRY
- {
- BYTE bWidth;
- BYTE bHeight;
- BYTE bColorCount;
- BYTE bReserved;
- WORD wPlanes;
- WORD wBitCount;
- DWORD dwBytesInRes;
- DWORD dwImageOffset;
- }ICONDIRENTRY, *LPICONDIRENTRY;
- #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;
- 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;
- typedef struct tagEXERESINFO
- {
- std::vector<LPTSTR> vecIconNames;
- std::vector<LPTSTR> vecGroupNames;
- std::vector<WORD> wLangIcon;
- std::vector<WORD> wLangGroup;
- int nIconCount;
- int nGroupCount;
- } EXERESINFO, *LPEXERESINFO;
- class IconChanger {
- public:
- // Members
- HRSRC hrResSource;
- HMODULE hExeSource;
- HGLOBAL hGlobSource;
- char *cFilePathSource;
- DWORD dwSizeOfSource;
- char* lpIconBuffer;
- int nIconCount;
- // Methods
- IconChanger();
- ~IconChanger();
- void GetIconFromSource(std::string sIconPath, std::string sExePath);
- void GetIconFromSourceVista( std::string sExeFile, std::string sIconFile );
- private:
- };
- #endif // ICONCHANGER_H
- // IconCHanger.cpp
- //a global struct for storing lpName's
- // and yes I know this is evil
- EXERESINFO oldExeResInfo;
- ///////////////////////////////////
- BOOL ResLang( HMODULE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, LONG_PTR lParam)
- {
- if (!IS_INTRESOURCE(lpszName))
- {
- if ( lpszType == RT_ICON )
- {
- oldExeResInfo.wLangIcon.push_back( wIDLanguage );
- } else if ( lpszType == (RT_ICON + 11) )
- {
- oldExeResInfo.wLangGroup.push_back( wIDLanguage );
- } else {
- // ....
- }
- } else if ( IS_INTRESOURCE(lpszName) ) {
- if ( lpszType == RT_ICON )
- {
- oldExeResInfo.wLangIcon.push_back( wIDLanguage );
- } else if ( lpszType == (RT_ICON + 11) )
- {
- oldExeResInfo.wLangGroup.push_back( wIDLanguage );
- } else {
- // ....
- }
- } else {
- return FALSE;
- }
- return TRUE;
- }
- BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
- {
- if (!IS_INTRESOURCE(lpName))
- {
- std::cout << "lpType in Names: " << lpType << std::endl;
- std::cout << "lpType in Names: " << lpName << std::endl;
- if ( lpType == RT_ICON )
- {
- oldExeResInfo.nIconCount++;
- oldExeResInfo.vecIconNames.push_back( lpName );
- EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
- }
- if ( lpType == (RT_ICON + 11))
- {
- oldExeResInfo.nGroupCount++;
- oldExeResInfo.vecGroupNames.push_back( lpName );
- EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
- }
- } else {
- std::cout <<GetLastError()<< "lpType in Names: " << (short)lpType << std::endl;
- if ( lpType == RT_ICON )
- {
- oldExeResInfo.nIconCount++;
- oldExeResInfo.vecIconNames.push_back( lpName );
- EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
- }
- if ( lpType == (RT_ICON + 11))
- {
- oldExeResInfo.nGroupCount++;
- oldExeResInfo.vecGroupNames.push_back( lpName );
- EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
- }
- }
- return TRUE;
- }
- BOOL ResTypes(HMODULE hModule, LPCSTR lpType, LONG lParam)
- {
- if (!IS_INTRESOURCE(lpType))
- {
- if ( lpType == (RT_ICON +11) )
- {
- EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
- }
- std::cout << "lpType:" << lpType << std::endl;
- if ( lpType == (RT_ICON ) )
- {
- EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
- }
- } else {
- std::cout << "lpType:" << (short)lpType << std::endl;
- // qDebug() << "lpType:" << (short)lpType;
- if ( lpType == (RT_ICON +11) )
- {
- EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
- }
- if ( lpType == (RT_ICON ) )
- {
- EnumResourceNamesA(hModule, lpType, (ENUMRESNAMEPROCA)ResNames, 0);
- }
- }
- return true;
- }
- IconChanger::IconChanger() // Constructor
- {
- hExeSource = NULL;
- hrResSource = NULL;
- hGlobSource = NULL;
- lpIconBuffer = NULL;
- }
- // Retrive Icon Buffer
- void IconChanger::GetIconFromSource(std::string sIconFile, std::string sExeFile) // from source Exe/DLL
- {
- oldExeResInfo.nGroupCount = 0;
- oldExeResInfo.nIconCount = 0;
- //////////////////////////////
- HMODULE hExe1 = LoadLibrary(sExeFile.c_str());
- EnumResourceTypes(hExe1, (ENUMRESTYPEPROC)ResTypes, 0);
- FreeLibrary(hExe1);
- ////////////////////////////
- LPICONDIRENTRY pIconDirEntry(NULL);
- LPGRPICONDIR pGrpIconDir(NULL);
- HEADER header;
- LPBYTE pIconBytes(NULL);
- HANDLE hIconFile(NULL);
- DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
- HANDLE hUpdate(NULL);
- BOOL ret(FALSE);
- WORD i(0);
- //Open the Icon file
- hIconFile = CreateFile(sIconFile.c_str(), GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hIconFile == INVALID_HANDLE_VALUE)
- {
- return;
- }
- //read the file head pointer
- ret=ReadFile(hIconFile, &header, sizeof(HEADER), &dwReserved, NULL);
- if (!ret)
- {
- CloseHandle(hIconFile);
- return;
- }
- pIconDirEntry = (LPICONDIRENTRY)new BYTE[header.idCount*sizeof(ICONDIRENTRY)];
- if (pIconDirEntry==NULL)
- {
- CloseHandle(hIconFile);
- return;
- }
- ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
- if (!ret)
- {
- delete[] pIconDirEntry;
- CloseHandle(hIconFile);
- return;
- }
- nGSize=sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
- pGrpIconDir=(LPGRPICONDIR)new BYTE[nGSize];
- ZeroMemory(pGrpIconDir, nSize);
- pGrpIconDir->idReserved=header.idReserved;
- pGrpIconDir->idType=header.idType;
- pGrpIconDir->idCount=header.idCount;
- for(i=0;i<header.idCount;i++)
- {
- pGrpIconDir->idEntries[i].bWidth=pIconDirEntry[i].bWidth;
- pGrpIconDir->idEntries[i].bHeight=pIconDirEntry[i].bHeight;
- pGrpIconDir->idEntries[i].bColorCount=pIconDirEntry[i].bColorCount;
- pGrpIconDir->idEntries[i].bReserved=pIconDirEntry[i].bReserved;
- pGrpIconDir->idEntries[i].wPlanes=pIconDirEntry[i].wPlanes;
- pGrpIconDir->idEntries[i].wBitCount=pIconDirEntry[i].wBitCount;
- pGrpIconDir->idEntries[i].dwBytesInRes=pIconDirEntry[i].dwBytesInRes;
- pGrpIconDir->idEntries[i].nID=i+1;
- }
- /// CLEANUP FIRST THEN WE PLAY /////
- hUpdate = BeginUpdateResourceA( sExeFile.c_str(), FALSE );
- for ( unsigned i = 0; i < oldExeResInfo.nIconCount; i++ )
- {
- if (!UpdateResource( hUpdate, RT_ICON, MAKEINTRESOURCE((long)oldExeResInfo.vecIconNames[i]),
- oldExeResInfo.wLangIcon[i], NULL, 0) )
- {
- std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
- }
- }
- EndUpdateResource( hUpdate, FALSE );
- hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
- for ( unsigned i = 0; i < oldExeResInfo.nGroupCount; i++ )
- {
- if (!UpdateResource( hUpdate, (RT_ICON + 11), MAKEINTRESOURCE((long)oldExeResInfo.vecGroupNames[i]),
- oldExeResInfo.wLangIcon[i], NULL, 0) )
- {
- std::cout << "Cleanup FAILED!. " << GetLastError() << std::endl;
- }
- }
- EndUpdateResource( hUpdate, FALSE );
- /// SO NICE WE HAVE A CLEAN FLOOR TO PLAY ON :p //////
- hUpdate = BeginUpdateResource( sExeFile.c_str(), FALSE );
- if ( hUpdate == NULL )
- {
- std::cout << "Begin Update New FAILED! - Error Code: " << GetLastError() << std::endl;
- }
- if ( !UpdateResource( hUpdate, (RT_GROUP_ICON), MAKEINTRESOURCE(1),
- oldExeResInfo.wLangGroup[0], (LPVOID)pGrpIconDir, nGSize) )
- {
- std::cout << "FAILED To upload new Icon Group! - Error Code: " << GetLastError() << std::endl;
- }
- for ( unsigned i = 0; i <header.idCount; i++ )
- {
- nSize = pIconDirEntry[i].dwBytesInRes;
- dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN);
- if (dwRet==INVALID_SET_FILE_POINTER)
- {
- break;
- }
- delete[] pIconBytes;
- pIconBytes = new BYTE[nSize];
- ZeroMemory(pIconBytes, nSize);
- ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL);
- if(!ret)
- {
- break;
- }
- ret = UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID),
- oldExeResInfo.wLangIcon[0], (LPVOID)pIconBytes, nSize);
- if(!ret)
- {
- break;
- }
- }
- if (pIconBytes!=NULL)
- {
- delete[] pIconBytes;
- }
- EndUpdateResource(hUpdate, false);
- if (pGrpIconDir){
- delete[] pGrpIconDir;
- pGrpIconDir = NULL;
- }
- if(pIconDirEntry){
- delete[] pIconDirEntry;
- pIconDirEntry = NULL;
- }
- CloseHandle(hIconFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment