T
T
Ternick2020-06-11 19:00:39
C++ / C#
Ternick, 2020-06-11 19:00:39

How to change the icon of an executable file?

How to change the icon of an executable file?
Found only this option, which does not work.

spoiler
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#pragma pack(push, 2)
typedef struct {
  WORD _id_reserved;
  WORD _id_type;
  WORD _id_count;
  BYTE _width;
  BYTE _height;
  BYTE _color_count;
  BYTE _reserved;
  WORD _planes;
  WORD _bit_count;
  DWORD _bytes_in_resource;
  DWORD _image_offset;
} ICON_ENTRY;
#pragma pack(pop)


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  wstring ico = L"ic.ico";
  wstring exe = L"app.exe";
  HANDLE resource_handle = BeginUpdateResource(exe.data(), FALSE);
  wchar_t* buffer;
  size_t buffer_size;
  wifstream icon(ico.data(), ios::in | ios::binary);
  
  if (icon.good()) {
    icon.seekg(0, icon.end);
    buffer_size = (size_t)icon.tellg();
    buffer = new wchar_t[buffer_size];
    icon.read(buffer, buffer_size);
    icon.close();

    UpdateResource(resource_handle, RT_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), buffer + 22, buffer_size - 22);

    ICON_ENTRY icon_entry;

    icon_entry._id_reserved = 0;
    icon_entry._id_type = 1;
    icon_entry._id_count = 1;
    icon_entry._width = 32;
    icon_entry._height = 32;
    icon_entry._color_count = 0;
    icon_entry._reserved = 0;
    icon_entry._planes = 2;
    icon_entry._bit_count = 32;
    icon_entry._bytes_in_resource = buffer_size - 22;
    icon_entry._image_offset = 2;

    UpdateResource(resource_handle, RT_GROUP_ICON, L"MAINICON", MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), &icon_entry, sizeof(ICON_ENTRY));

    EndUpdateResource(resource_handle, FALSE);
    delete buffer;
  }
  return 0;
}


It writes some data to the .exe file, but the icon that is written in the .exe file is not readable and so it just messes with the existing icon.

What could be wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question