4 Replies - 1572 Views - Last Post: 09 December 2012 - 04:59 AM Rate Topic: -----

#1 chipicau  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 15-May 12

UpdateResource icon

Posted 08 December 2012 - 03:26 PM

Hello.

I've looked in the Internet, as I always do before asking a question in DIC, but there's a lack of information about it. Asking a question here is always my last resort.

I'm trying to change another executable's icon with no success.
I embedded another icon in my program (there's the main one, and the secondary which will be used as the new icon for another executable) through a .rc file.
Here's the code I'm using:

HANDLE hResource = BeginUpdateResource("user.exe", NULL);

HRSRC hRes = FindResource(hInstance, MAKEINTRESOURCE(2), RT_GROUP_ICON); //this finds the embedded icon in my program which has the ID of 2.

LoadResource(hInstance, hRes); //this loads the resource found

UpdateResource(hResource, RT_GROUP_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), LockResource(hRes), SizeofResource(hInstance, hRes)); //this updates the icon to the resource

EndUpdateResource(hResource, NULL); //this writes the changes back to the executable




BTW, this is my .rc file:
1 ICON "icon.ico" //this is my program's main icon, the one that gets displayed as the executable's image
2 ICON "newicon.ico" //this is the icon to be set as the icon of another executable




There's no error with any of the API calls, but the new executable's icon is null (default icon) after this.

Any hints?
Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: UpdateResource icon

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4191
  • View blog
  • Posts: 11,864
  • Joined: 18-April 07

Re: UpdateResource icon

Posted 08 December 2012 - 04:58 PM

First of all many of these commands return true/false booleans to let you know if they succeeded or not. I suggest you save these results to variables that you can then test against to let you know first of all which command is giving you the trouble.

For instance...

bool result = UpdateResource(hResource, RT_GROUP_ICON, MAKEINTRESOURCE(1), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), LockResource(hRes), SizeofResource(hInstance, hRes));

if (!result) {
   // Something here to let you know it failed... print a statement, log an error or whatever
}



The preceding code would then tell you if the UpdateResource is the one failing you or not and help you narrow down your problem.

Secondly, EndUpdateResource()'s second parameter should be true/false, not null. True will discard the changes (not what you want here) and False will make the changes go into effect (which is what you want).

Hope this advice helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 chipicau  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 15-May 12

Re: UpdateResource icon

Posted 08 December 2012 - 05:13 PM

Hello, Martyr2.

Actually, I had already tested for the return value and it returned true, which means it succeeded. I already knew that. But there's something I'm missing about changing the executable's icon.

I changed the second parameter to FALSE, but the results were the same, the executable icon was set to the default one, and correct me if I'm wrong but there's no difference between NULL or FALSE, because NULL = 0 and FALSE = 0, so NULL = FALSE.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: UpdateResource icon

Posted 09 December 2012 - 01:46 AM

Hi,

Your resource file is wrong...

1 ICON "icon.ico" //this is my program's main icon, the one that gets displayed as the executable's image
2 ICON "newicon.ico" //this is the icon to be set as the icon of another executable





First avoid using 0-10 for resources as Windows itself can sometimes use these numbers

I tend to start at 100

my version of your rc file


include "resource.h"
ICON1 ICON DISCARDABLE "icon.ico" //this is my program's main icon, the one that gets displayed as the executable's image
ICON2 ICON DISCARDABLE "newicon.ico" //this is the icon to be set as the icon of another executable






resource.h

#ifndef RESOURCE_H
#define RESOURCE_H

#define ICON1 100
#define ICON2 101

#endif





Then to use the icon resource MAKEINTRESOURCE(ICON1) etc...

Regards

Snoopy.

This post has been edited by snoopy11: 09 December 2012 - 01:49 AM

Was This Post Helpful? 1
  • +
  • -

#5 chipicau  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 15-May 12

Re: UpdateResource icon

Posted 09 December 2012 - 04:59 AM

Hello, snoopy11.

I took your advice and stopped using 0-10 and now I'm using 100 and 101 for the icons.
But my problem is setting another executable's icon. Do you have any idea about how I should do it?

Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1