Using UpdateResource in C#?

后端 未结 4 1814
迷失自我
迷失自我 2020-12-19 10:17

I\'m trying to change the icon of external executable programmatically. I\'ve googled and found much information about this problem using C++. Basically, I need to use Begin

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 10:45

    Just some pointers, this is quite hard to get right. Pass an RT_ICON by lying about the lpType argument. Change it from string to IntPtr and pass (IntPtr)3.

    The lpData argument is quite tricky. You need to pass the data the way it is compiled by the resource compiler (rc.exe). I have no idea if it mangles the raw data of the .ico file. The only reasonable thing to try is to read the data from the .ico file with FileStream into a byte[], you already seem to be doing this. I think the function was really designed to copy a resource from one binary image to another. Odds of your approach working are not zero.

    You are also ignoring another potential problem, the resource ID of the icon of the program isn't necessarily 1. It often is not, 100 tends to be a popular choice, but anything goes. EnumResourceNames would be required to make it reliable. The rule is that the lowest numbered ID sets the icon for the file. I'm not actually sure if that really means that the resource compiler puts the lowest number first, something that the API probably doesn't do.

    A very small failure mode is that UpdateResource can only updated numbered resource items, not named ones. Using names instead of numbers is not uncommon but the vast majority of images use numbers for icons.

    And of course, the odds that this will work without a UAC manifest are zero. You are hacking files that you don't normally have write access to.

提交回复
热议问题