From 772c0dbcb9f34c834d6ccdbdfb0288b5bcb0cb15 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Tue, 23 Nov 2010 00:49:09 +0000 Subject: [PATCH] =?UTF-8?q?[NEW]=20(Author:=20Fran=C3=A7ois-R=20Boyer)=20A?= =?UTF-8?q?dd=20ChangeIcon=20command=20line=20program.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@714 f5eea248-9336-0410-98b8-ebc06183d4e3 --- .../src/tools/ChangeIcon/ChangeIcon.cpp | 260 ++++++++++++++++++ .../src/tools/ChangeIcon/ChangeIcon.vcproj | 192 +++++++++++++ 2 files changed, 452 insertions(+) create mode 100644 PowerEditor/src/tools/ChangeIcon/ChangeIcon.cpp create mode 100644 PowerEditor/src/tools/ChangeIcon/ChangeIcon.vcproj diff --git a/PowerEditor/src/tools/ChangeIcon/ChangeIcon.cpp b/PowerEditor/src/tools/ChangeIcon/ChangeIcon.cpp new file mode 100644 index 000000000..97b17a58d --- /dev/null +++ b/PowerEditor/src/tools/ChangeIcon/ChangeIcon.cpp @@ -0,0 +1,260 @@ +// Replace an icon group in an executable by one from an ICO file +// By Francois-R.Boyer@PolyMtl.ca for Notepad++ +// 2010-11-20 +// +// This code is based on: Maria Nadejde, "Replacing ICON resources in EXE and DLL files", The Code Project, 13 Nov 2008 +// ( http://www.codeproject.com/KB/DLL/ICON_Resources.aspx ) +// original article and code is licenced under The GNU General Public License (GPLv3) +// +// +// this file is part of ChangeIcon +// Copyright (C)2010 Francois-R Boyer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + +#ifndef UNICODE +#define UNICODE +#endif + +#ifndef _UNICODE +#define _UNICODE +#endif + +#include +#include +#include +#include // for offsetof + +#ifdef _DEBUG +#define IFDEBUG(x) x +#else +#define IFDEBUG(x) +#endif + +BOOL getMaxIconId_EnumNamesFunc(HANDLE hModule, LPCTSTR lpType, LPTSTR lpName, WORD* lpMaxID) +{ + if(IS_INTRESOURCE(lpName) && (USHORT)lpName>*lpMaxID) + *lpMaxID=(USHORT)lpName; + return true; +} + +WORD getMaxIconId(TCHAR* lpFileName) +{ + WORD nMaxID = 0; + HINSTANCE hLib = LoadLibraryEx(lpFileName,NULL,DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); + if(hLib == NULL) { _tprintf(_T("Unable to load library '%s'\n"), lpFileName); return 0xFFFF; } + // Enumerate icon "names" (IDs) to get next available ID + if(!EnumResourceNames(hLib, RT_ICON, (ENUMRESNAMEPROC)getMaxIconId_EnumNamesFunc,(LONG_PTR)&nMaxID)) { _tprintf(_T("Unable to enum icons\n")); return 0xFFFF; } + FreeLibrary(hLib); + IFDEBUG( _tprintf(_T("MaxIcon=%d\n"), nMaxID); ) + return nMaxID; +} + +class Icon { +public: + // Icon format from http://msdn.microsoft.com/en-us/library/ms997538.aspx + // for ICO and EXE files + struct ICONDIR { // File header: + WORD idReserved; // Reserved (must be 0) + WORD idType; // Resource Type (1 for icons) + WORD idCount; // How many images? + }; + struct ICONDIRENTRY { // One for each image: + BYTE bWidth; // Width, in piexels, of the image + BYTE bHeight; // Height, in pixels, of the image (times 2) + 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? + union { + DWORD dwImageOffset;// Where in the file is this image (in ICO file) + WORD nID; // the ID (in EXE file) + }; + }; + static const UINT sizeof_iconDirEntry_ICO = sizeof(ICONDIRENTRY); + static const UINT sizeof_iconDirEntry_EXE = offsetof(ICONDIRENTRY,nID)+sizeof(WORD); + + ICONDIR _head; + ICONDIRENTRY *_entries; + LPBYTE *_imagesData; + + Icon() : _entries(NULL), _imagesData(NULL) { _head.idCount = 0; } + void clear() { + if(_imagesData) { for(int i=0; i<_head.idCount; ++i) delete _imagesData[i]; delete[] _imagesData; _imagesData = 0; } + if(_entries) delete[] _entries; _entries = 0; + _head.idCount = 0; + } + ~Icon() { clear(); } + + bool readICO(TCHAR* filename); + bool readEXE(TCHAR* lpFileName, LPCTSTR lpResName, UINT resLangId); // Does not currently read image data + + bool writeToEXE(TCHAR* lpFileName, LPCTSTR lpResName, UINT resLangId); + + WORD count() { return _head.idCount; } +}; + +bool Icon::readICO(TCHAR* filename) +{ + clear(); + HANDLE hFile = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if(hFile == INVALID_HANDLE_VALUE) { _tprintf(_T("Error opening file '%s' for Reading\n"), filename); return false; } + DWORD dwBytesRead; + // Read header + if(!ReadFile( hFile, &_head, sizeof(_head), &dwBytesRead, NULL )) { _tprintf(_T("Error reading file '%s'\n"), filename); return false; } + IFDEBUG( _tprintf(_T("%d icon entries\n"), count()); ) + // Read entries + _entries = new ICONDIRENTRY[count()]; + if(!ReadFile( hFile, _entries, sizeof(*_entries)*count(), &dwBytesRead, NULL )) { _tprintf(_T("Error reading file '%s'\n"), filename); return false; } + // Read images + _imagesData=new LPBYTE[count()]; memset(_imagesData, sizeof(LPBYTE)*count(), 0); + for(int i=0; i + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +