Click here to Skip to main content
15,072,134 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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
C++
#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?
Posted
Comments
The_Inventor 9-Aug-13 21:33pm
   
Maybe you failed to save the new groupicon to a file called C:\test.ico, so that it could be loaded by the test.exe? Also you may want to EndUpdateResource(hWhere, FALSE), before you
delete buffer; // free memory ...
pasztorpisti 10-Aug-13 15:07pm
   
I have used Begin/EndUpdateResource in the past and worked on code that manipulates PE file structure. My observation was that the Begin/EndUpdateResource function calls aren't always reliable. They simple didn't handle all cases and sometimes I had to solve the problem with own code. A PE file can store the icon in many places inside the executable (usually appended at the end of file) and its a good question how good are these functions in rebuilding the resource tree of the exe when needed. Maybe the funcs have been improved with newer windows versions but who knows... If the problem is a bug in your code then you need a guy who has fresh up to date experience with these function calls and spots the bug immediately or someone who is willing to debug it instead of you... Why don't you debug it? Where does it fail? What does GetLastError() return and what is the meaning of this getlasterror value (use FormatMessage to convert it to error message)...
wawada 11-Aug-13 10:44am
   
going to look at your answer in 24 hour have not time now but thanks

1 solution

The stages to do so are:
1. Load executable
2. Find resource
3. Load resource
4. Lock resource
5. Begin update resource
6. Update resource
7. End update resource
8. Clean up

Let's go step by step:

1. Loading executable:
C++
hExe = LoadLibrary(Where);
if(hExe == NULL)
{
    printf("Error loading executable\n");
    return -1 ;
}


2. Locating the resource we would like to replace (the Icon)

C++
hRes = FindResource(hExe, MAKEINTRESOURCE(HL),RT_ICON);
if (hRes == NULL)
{
    printf("Could not locate icon.\n");
    return -1 ;
}


3. Loading the resource

C++
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
    printf("Could not load resource");
    return -1 ;
}

4. Locking resource

C++
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
    printf("Could not lock resource");
    return -1 ;
}


5. Begin update

C++
hUpdateRes = BeginUpdateResource(What, FALSE);
if (hUpdateRes == NULL)
{
    printf("Could not open icon file for writing.\n");
    return -1 ;
}


6. Actual update

C++
result = 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 update

C++
if (!EndUpdateResource(hUpdateRes, FALSE))
{
    printf("Could not end update resoruce");
    return -1 ;
}


8. Clean up.

C++
if (!FreeLibrary(hExe))
{
    printf("Could not free resources");
    return -1 ;
}
   
v2
Comments
wawada 12-Aug-13 19:27pm
   
i tested but i dont know what i should declare
hExe
hRes
hUpdateRes
result
as :/ or do i not need to declare.
and for some reason cannot i not have return -1 ;
Michael Haephrati 13-Aug-13 3:32am
   
HGLOBAL hRes; // handle to loaded resource
HMODULE hExe; // handle to existing .EXE file
HANDLE hUpdateRes; // update resource handle
BOOL result;
wawada 13-Aug-13 10:56am
   
some errors i have got when i try
i hope someone can help me on this to

errors

Error: identifier "hResLoad" is undefined

#define MAKEINTRESOURCE ;
Error expected an expression

Error: identifier "HL" is undefined

#define RT_ICON MAKEINTRESOURCE(3)

Error: identifier "lpResLock" is undefined

Error: return value type does not match the function type

#define MAKEINTRESOURCE MAKEINTRESOURCEA

Error: identifier "PENG" is undefined

HGLOBAL hRes

Error: argument of type "HGLOBAL" is incompatible with parameter of type "HRSRC"
Michael Haephrati 13-Aug-13 11:45am
   
Well

Error: identifier "hResLoad" is undefined

-> Define it

#define MAKEINTRESOURCE ;

Change to
#define MAKEINTRESOURCE
Error expected an expression

Error: identifier "HL" is undefined
-> Define it

#define RT_ICON MAKEINTRESOURCE(3)

Error: identifier "lpResLock" is undefined
-> Define it

Error: return value type does not match the function type

and so on...
wawada 16-Aug-13 9:52am
   
i can't get it to work :/ im going to try when i have sleep thanks for your answer i hope you will help me more
wawada 21-Aug-13 7:35am
   
anyway i have look at it i think i got it work. thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900