From: lolguy on
hello i got problems with UpdateResource api in whish it doesn't even
does anything it either corupts program with small programs and with
big programs and it doesn't do anything
[code]
#include <stdio.h>
#include <windows.h>
#define MIN 2
char *ReadFile(char *SzFile,int *BytesCount) {
int fSize;
FILE *pFile;
char *Buffer;
if(!(pFile=fopen(SzFile,"rb")))
return NULL;
fseek(pFile,0,SEEK_END);
fSize=ftell(pFile);
rewind(pFile);
if(!(Buffer=(char*)calloc(fSize,sizeof(char))))
return NULL;
fread(Buffer,fSize,1,pFile);
*BytesCount=fSize;
return Buffer;
}
bool AddIcon(char *SzFile,char *SzIcon) {
HANDLE ih;
char *Buffer;
int fSize=0;
if(!(Buffer=ReadFile(SzIcon,&fSize)) || !(ih=BeginUpdateResource
(SzFile,FALSE)) \
|| !UpdateResource(ih,MAKEINTRESOURCE(3),MAKEINTRESOURCE(50),
0,Buffer+22,fSize-22) \
|| !EndUpdateResource(ih,FALSE)
){
Buffer ? free(Buffer) , ih ? CloseHandle(ih) : 0 : 0;
return false;
}
free(Buffer),CloseHandle(ih);
return true;
}
int main(int argc,char *argv[])
{
if(argc<MIN) {
printf("wrong usage: must be bigger than %d\n",MIN);
return 1;
}
int sucess=0;
for(int i=1;i<argc;i++)
if(AddIcon(argv[i],argv[argc-1])) {
printf("\nadded icon %s to file %s\n",argv[argc-1],argv
[i]);
sucess++;
}
printf("we succesfully added %d icons",sucess);
return 0;
}
[/code]
From: Alain on

"lolguy" <tbgrafix(a)gmail.com> a �crit dans le message de news:
df86d20b-91c2-4ded-826f-aa9fe5c6f717(a)p9g2000vbl.googlegroups.com...
> hello i got problems with UpdateResource api in whish it doesn't even
> does anything it either corupts program with small programs and with
> big programs and it doesn't do anything

It's more complicated than that : you must play with Icon and Group Icons
directories.
There are some samples on the Web : a quick search gives that :

#pragma pack( push )
#pragma pack( 2 )

typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
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?
} ICONDIRENTRYCOMMON, *LPICONDIRENTRYCOMMON;

typedef struct
{
ICONDIRENTRYCOMMON common;
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;

typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource Type (1 for icons)
WORD idCount; // How many images?
ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
} ICONDIR, *LPICONDIR;

typedef struct
{
ICONDIRENTRYCOMMON common;
WORD nID; // the ID
} GRPICONDIRENTRY, *LPGRPICONDIRENTRY;

typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource type (1 for icons)
WORD idCount; // How many images?
GRPICONDIRENTRY idEntries[1]; // The entries for each image
} GRPICONDIR, *LPGRPICONDIR;

#pragma pack( pop )

BOOL AddIconToRes(HANDLE hRes, LPCTSTR pszFile, UINT nIconID)
{
void *pvFile;
DWORD nSize;
int f, z;
ICONDIR id, *pid;
GRPICONDIR *pgid;

ASSERT(pszFile);

if (!strlen(pszFile))
return FALSE;
f = _lopen(pszFile, OF_READ);
if (f == -1)
return FALSE;

_lread(f, &id, sizeof(id));
_llseek(f, 0, SEEK_SET);
pid = (ICONDIR *) malloc(sizeof(ICONDIR) + sizeof(ICONDIRENTRY) *
(id.idCount - 1));
pgid = (GRPICONDIR *) malloc(sizeof(GRPICONDIR) + sizeof(GRPICONDIRENTRY)
* (id.idCount - 1));
_lread(f, pid, sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * (id.idCount -
1));
memcpy(pgid, pid, sizeof(GRPICONDIR));

for (z = 0; z < id.idCount; z++)
{
pgid->idEntries[z].common = pid->idEntries[z].common;
pgid->idEntries[z].nID = z + 1;
nSize = pid->idEntries[z].common.dwBytesInRes;

pvFile = malloc(nSize);
if (!pvFile)
return _lclose(f), FALSE;

_llseek(f, pid->idEntries[z].dwImageOffset, SEEK_SET);
_lread(f, pvFile, nSize);
UpdateResource(hRes, RT_ICON, MAKEINTRESOURCE(z + nIconID),
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), pvFile, nSize);
free(pvFile);
}

_lclose(f);
UpdateResource(hRes, RT_GROUP_ICON, MAKEINTRESOURCE(nIconID),
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), pgid, sizeof(GRPICONDIR) +
sizeof(GRPICONDIRENTRY) * (id.idCount - 1));
return TRUE;
}