本文介绍了c / c ++更改可执行文件图标请帮助不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我找到了用c / c ++更改图标的代码,而我在c或c ++上的表现不好 并且在编译完成后它无法运行 main。 cpp #include < io.h > #include < stdio.h > #include < stdlib.h > #include < fcntl.h > #include < windows.h > #pragma pack(push,2) typedef struct { WORD Reserved1; // reserved,必须为0 WORD ResourceType; // 图标类型为1 WORD ImageCount; // 结构中的图标数量(1) BYTE宽度; // 图标宽度(32) BYTE高度; // 图标高度(32) BYTE颜色; // 颜色(0表示每像素超过8位) BYTE Reserved2; // reserved,必须为0 WORD Planes; // 颜色平面 WORD BitsPerPixel; // 位深度 DWORD ImageSize; // 结构大小 WORD ResourceID; // 资源ID } GROUPICON; #pragma pack(pop) void InjectMainIcon( char *其中, char *什么) { HANDLE hWhere = BeginUpdateResource(Where ,FALSE); char * buffer; // 用于存储原始图标数据的缓冲区 long buffersize; // 缓冲区长度 int hFile; // 文件句柄 hFile = open(什么,O_RDONLY | O_BINARY) ; if (hFile == - 1 ) 返回; // 如果文件不存在,则无法打开等。 // 计算缓冲区长度并将文件加载到缓冲区 buffersize = filelength(hFile) ; buffer =( char *)malloc(buffersize); 读取(hFile,buffer,buffersize); close(hFile); UpdateResource( hWhere, // 处理可执行文件 RT_ICON, // 资源类型 - 图标 MAKEINTRESOURCE( 1 ), // 使id为 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // 默认语言 (缓冲区+ 22), // 跳过前22个字节,因为这是 // 图标标题和目录条目(如果文件 // 包含目录条目的多个图像 // 将大于22个字节 buffersize- 22 // 缓冲区长度 ); // 同样,我们将此结构用于教育目的。 // 可以从 中读取图标标题和目录条目 // 文件。 GROUPICON grData; // 这是标题 grData.Reserved1 = 0 ; // reserved,必须为0 grData.ResourceType = 1 ; // 图标类型为1 grData.ImageCount = 1 ; // 结构中的图标数量(1) // 这是目录条目 grData.Width = 32 ; // 图标宽度(32) grData.Height = 32 ; // icon height(32) grData.Colors = 0 ; // colors(256) grData.Reserved2 = 0 ; // reserved,必须为0 grData.Planes = 2 ; // 颜色平面 grData.BitsPerPixel = 32 ; // 位深度 grData.ImageSize = buffersize - 22 ; // 图片大小 grData.ResourceID = 1 ; // 资源ID为1 UpdateResource( hWhere, RT_GROUP_ICON, // RT_GROUP_ICON资源包含信息 // 关于存储的图标 MAINICON, // MAINICON包含信息关于 // 应用程序显示的图标 MAKELANGID(LANG_ENGLISH ,SUBLANG_DEFAULT),& grData, // 指向此结构的指针 sizeof (GROUPICON)); delete buffer; // 可用内存 // 执行更新,不要丢弃更改 EndUpdateResource(hWhere,FALSE); } // void main(int argc,char * argv [] ) int main( int argc, char * argv []) { if (argc!= 3 ) { printf( 用法:chicon [可执行文件名] [icon filename]); return ( 0 ); } InjectMainIcon(argv [ 1 ],argv [ 2 ]); } InjectMainIconC:\ test.exeC:\ test.ico 当一切都是清除test.exe加载和图标是相同的,我有测试以管理员身份运行,但它不工作有人可以帮助我吗?解决方案 这样做的阶段是: 1.加载可执行文件 2.查找资源 3.加载资源 4.锁定资源 5.开始更新资源 6.更新资源 7.结束更新资源 8.清理 让我们一步一步走: 1.加载可执行文件: hExe = LoadLibrary(Where); if (hExe == NULL) { printf( 加载executable\\\时出错; 返回 - 1 ; } 2.找到我们想要替换的资源(图标) hRes = FindResource(hExe,MAKEINTRESOURCE(HL),RT_ICON); if (hRes == NULL) { printf( 找不到icon.\); 返回 - 1 ; } 3.加载资源 hResLoad = LoadResource(hExe,hRes); if (hResLoad == NULL) { printf( 无法加载资源); 返回 - 1 ; } 4.锁定资源 lpResLock = LockResource(hResLoad); if (lpResLock == NULL) { printf( 无法锁定资源); 返回 - 1 ; } 5.开始更新 hUpdateRes = BeginUpdateResource(What,FALSE); if (hUpdateRes == NULL) { printf( 无法打开用于书写的图标文件。\ n); 返回 - 1 ; } 6.实际更新 result = UpdateResource(hUpdateRes,RT_ICON,MAKEINTRESOURCE(PENG),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),lpResLock,SizeofResource(hExe,hRes)); if (result == FALSE) { printf( 无法开始更新资源。); 返回 - 1 ; } 7.结束更新 if (!EndUpdateResource(hUpdateRes,FALSE)) { printf( 无法结束更新resoruce); 返回 - 1 ; } 8.清理。 if (!FreeLibrary(hExe)) { printf( 无法释放资源); 返回 - 1 ; } i have found code to change icon with c/c++ and my not good on c or c++and it not working after i have compiled and run main.cpp#include <io.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <windows.h>#pragma pack(push, 2)typedef struct { WORD Reserved1; // reserved, must be 0 WORD ResourceType; // type is 1 for icons WORD ImageCount; // number of icons in structure (1) BYTE Width; // icon width (32) BYTE Height; // icon height (32) BYTE Colors; // colors (0 means more than 8 bits per pixel) BYTE Reserved2; // reserved, must be 0 WORD Planes; // color planes WORD BitsPerPixel; // bit depth DWORD ImageSize; // size of structure WORD ResourceID; // resource ID} GROUPICON;#pragma pack(pop)void InjectMainIcon(char *Where, char *What){ HANDLE hWhere = BeginUpdateResource(Where, FALSE); char *buffer; // buffer to store raw icon data long buffersize; // length of buffer int hFile; // file handle hFile = open(What, O_RDONLY | O_BINARY); if (hFile == -1) return; // if file doesn't exist, can't be opened etc. // calculate buffer length and load file into buffer buffersize = filelength(hFile); buffer = (char *)malloc(buffersize); read(hFile, buffer, buffersize); close(hFile); UpdateResource( hWhere, // Handle to executable RT_ICON, // Resource type - icon MAKEINTRESOURCE(1), // Make the id 1 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // Default language (buffer+22), // skip the first 22 bytes because this is the // icon header&directory entry (if the file // contains multiple images the directory entries // will be larger than 22 bytes buffersize-22 // length of buffer ); // Again, we use this structure for educational purposes. // The icon header and directory entries can be read from // the file. GROUPICON grData; // This is the header grData.Reserved1 = 0; // reserved, must be 0 grData.ResourceType = 1; // type is 1 for icons grData.ImageCount = 1; // number of icons in structure (1) // This is the directory entry grData.Width = 32; // icon width (32) grData.Height = 32; // icon height (32) grData.Colors = 0; // colors (256) grData.Reserved2 = 0; // reserved, must be 0 grData.Planes = 2; // color planes grData.BitsPerPixel = 32; // bit depth grData.ImageSize = buffersize - 22; // size of image grData.ResourceID = 1; // resource ID is 1 UpdateResource( hWhere, RT_GROUP_ICON, // RT_GROUP_ICON resources contain information // about stored icons "MAINICON", // MAINICON contains information about the // application's displayed icon MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), &grData, // Pointer to this structure sizeof(GROUPICON) ); delete buffer; // free memory // Perform the update, don't discard changes EndUpdateResource(hWhere, FALSE);}//void main(int argc, char * argv[])int main(int argc, char* argv[]){ if (argc != 3) { printf("Usage: chicon [executable filename] [icon filename]"); return(0); } InjectMainIcon(argv[1], argv[2]);}InjectMainIcon "C:\test.exe" "C:\test.ico"when everything is clear test.exe loads and the icon is the same and i have test to run as administrator but it not working can someone help me? 解决方案 The stages to do so are:1. Load executable2. Find resource3. Load resource4. Lock resource5. Begin update resource6. Update resource7. End update resource8. Clean upLet's go step by step:1. Loading executable:hExe = LoadLibrary(Where);if(hExe == NULL){ printf("Error loading executable\n"); return -1 ;}2. Locating the resource we would like to replace (the Icon)hRes = FindResource(hExe, MAKEINTRESOURCE(HL),RT_ICON);if (hRes == NULL){ printf("Could not locate icon.\n"); return -1 ;}3. Loading the resourcehResLoad = LoadResource(hExe, hRes);if (hResLoad == NULL){ printf("Could not load resource"); return -1 ;}4. Locking resourcelpResLock = LockResource(hResLoad);if (lpResLock == NULL){ printf("Could not lock resource"); return -1 ;}5. Begin update hUpdateRes = BeginUpdateResource(What, FALSE);if (hUpdateRes == NULL){ printf("Could not open icon file for writing.\n"); return -1 ;}6. Actual updateresult = UpdateResource(hUpdateRes,RT_ICON,MAKEINTRESOURCE(PENG),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),lpResLock,SizeofResource(hExe, hRes)); if (result == FALSE){ printf("Could not begin update resource."); return -1 ;}7. End updateif (!EndUpdateResource(hUpdateRes, FALSE)){ printf("Could not end update resoruce"); return -1 ;}8. Clean up.if (!FreeLibrary(hExe)){ printf("Could not free resources"); return -1 ;} 这篇关于c / c ++更改可执行文件图标请帮助不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 07:37