int wmain(int argc, wchar_t** argv) { if (argc != 3) { printf("Usage: redit <exe file> <icon file>\n"); return 1; } mozilla::ScopedClose file; if (0 != _wsopen_s(&file.rwget(), argv[2], _O_BINARY | _O_RDONLY, _SH_DENYWR, _S_IREAD) || (-1 == file)) { fprintf(stderr, "Unable to open icon file.\n"); return 1; } // Load all the data from the icon file long filesize = _filelength(file); nsAutoArrayPtr<BYTE> data(new BYTE[filesize]); if(!data) { fprintf(stderr, "Failed to allocate memory for icon file.\n"); return 1; } _read(file, data, filesize); IconHeader* header = reinterpret_cast<IconHeader*>(data.get()); // Open the target library for updating ScopedResourceUpdate updateRes(BeginUpdateResourceW(argv[1], FALSE)); if (NULL == updateRes) { fprintf(stderr, "Unable to open library for modification.\n"); return 1; } // Allocate the group resource entry long groupSize = sizeof(IconHeader) + header->ImageCount * sizeof(IconResEntry); nsAutoArrayPtr<BYTE> group(new BYTE[groupSize]); if(!group) { fprintf(stderr, "Failed to allocate memory for new images.\n"); return 1; } memcpy(group, data, sizeof(IconHeader)); IconDirEntry* sourceIcon = reinterpret_cast<IconDirEntry*>(data + sizeof(IconHeader)); IconResEntry* targetIcon = reinterpret_cast<IconResEntry*>(group + sizeof(IconHeader)); for (int id = 1; id <= header->ImageCount; id++) { // Add the individual icon if (!UpdateResourceW(updateRes, RT_ICON, MAKEINTRESOURCE(id), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), data + sourceIcon->ImageOffset, sourceIcon->ImageSize)) { fprintf(stderr, "Unable to update resource (RT_ICON).\n"); return 1; } // Copy the data for this icon // (note that the structs have different sizes) memcpy(targetIcon, sourceIcon, sizeof(IconResEntry)); targetIcon->ResourceID = id; sourceIcon++; targetIcon++; } if (!UpdateResourceW(updateRes, RT_GROUP_ICON, L"MAINICON", MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), group, groupSize)) { fprintf(stderr, "Unable to update resource (RT_GROUP_ICON).\n"); return 1; } // Save the modifications if(!EndUpdateResourceW(updateRes.forget(), FALSE)) { fprintf(stderr, "Unable to write changes to library.\n"); return 1; } return 0; }
void CTCExeVersionManager::changedExeIcon(LPCTSTR lpExeName, LPCTSTR lpIconFile) { 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); //打开图标文件 hIconFile = CreateFile(lpIconFile, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIconFile == INVALID_HANDLE_VALUE) { return; } //读取文件头部信息 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; } //从Icon文件中读取每一个图标的目录信息 ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL); if (!ret) { delete[] pIconDirEntry; CloseHandle(hIconFile); return; } //建立EXE文件中RT_GROUP_ICON所需的数据结构存放区域 nGSize=sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY); pGrpIconDir=(LPGRPICONDIR)new BYTE[nGSize]; //填充信息,这里相当于一个转换的过程 pGrpIconDir->idReserved=header.idReserved; pGrpIconDir->idType=header.idType; pGrpIconDir->idCount=header.idCount; //复制信息并设置每一个图标对应的ID。ID为位置索引号 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; } //开始更新EXE中的图标资源,ID定为最小0,如果原来存在0ID的图标信息则被替换为新的。 hUpdate = BeginUpdateResourceW(lpExeName, false); if (hUpdate!=NULL) { //首先更新RT_GROUP_ICON信息 ret = UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(0), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pGrpIconDir, nGSize); if (!ret) { delete[] pIconDirEntry; delete[] pGrpIconDir; CloseHandle(hIconFile); return; } //接着的是每一个Icon的信息存放 for(i=0;i<header.idCount;i++) { //Icon的字节数 nSize = pIconDirEntry[i].dwBytesInRes; //偏移文件的指针到当前图标的开始处 dwRet=SetFilePointer(hIconFile, pIconDirEntry[i].dwImageOffset, NULL, FILE_BEGIN); if (dwRet==INVALID_SET_FILE_POINTER) { break; } //准备pIconBytes来存放文件里的Byte信息用于更新到EXE中。 delete[] pIconBytes; pIconBytes = new BYTE[nSize]; ret = ReadFile(hIconFile, (LPVOID)pIconBytes, nSize, &dwReserved, NULL); if(!ret) { break; } //更新每一个ID对应的RT_ICON信息 ret = UpdateResourceW(hUpdate, RT_ICON, MAKEINTRESOURCE(pGrpIconDir->idEntries[i].nID), MAKELANGID(LANG_CHINESE, SUBLANG_SYS_DEFAULT), (LPVOID)pIconBytes, nSize); if(!ret) { break; } } //结束EXE资源的更新操作 if (pIconBytes!=NULL) { delete[] pIconBytes; } EndUpdateResourceW(hUpdate, false); } //清理资源并关闭Icon文件,到此更新操作结束! delete[] pGrpIconDir; delete[] pIconDirEntry; CloseHandle(hIconFile); m_isChangeExeIcon=true; }