在 C# 中使用 UpdateResource?
我正在尝试以编程方式更改外部可执行文件的图标。我用 google 搜索并找到了很多关于使用 C++ 的问题的信息。基本上,我需要使用 BeginUpdateResource、UpdateResource 和 EndUpdateResource。问题是 - 我不知道在 C# 中将什么传递给 UpdateResource。
这是我到目前为止的代码:
class IconChanger
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr BeginUpdateResource(string pFileName,
[MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage,
IntPtr lpData, uint cbData);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
public enum ICResult
{
Success,
FailBegin,
FailUpdate,
FailEnd
}
public ICResult ChangeIcon(string exeFilePath, byte[] iconData)
{
// Load executable
IntPtr handleExe = BeginUpdateResource(exeFilePath, false);
if (handleExe == null)
return ICResult.FailBegin;
// Get language identifier
CultureInfo currentCulture = CultureInfo.CurrentCulture;
int pid = ((ushort)currentCulture.LCID) & 0x3ff;
int sid = ((ushort)currentCulture.LCID) >> 10;
ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));
// Get pointer to data
GCHandle iconHandle = GCHandle.Alloc(iconData, GCHandleType.Pinned);
// Replace the icon
if (UpdateResource(handleExe, "#3", "#1", languageID, iconHandle.AddrOfPinnedObject(), (uint)iconData.Length))
{
if (EndUpdateResource(handleExe, false))
return ICResult.Success;
else
return ICResult.FailEnd;
}
else
return ICResult.FailUpdate;
}
}
关于 lpType - 在 C++ 中,您传递 RT_ICON (或 RT_GROUP_ICON)。我应该在 C# 中传递什么值? 同样的问题也适用于 lpName 参数。 我不确定语言标识符(我在互联网上找到了这个),因为我无法测试它。 我也不确定我是否提供了适当的图标数据。目前, iconData 包含 .ico 文件中的字节。
有人能指出我正确的方向吗?
非常感谢。
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 BeginUpdateResource, UpdateResource and EndUpdateResource. The problem is - I don't know what to pass to UpdateResource in C#.
Here's the code I have so far:
class IconChanger
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr BeginUpdateResource(string pFileName,
[MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage,
IntPtr lpData, uint cbData);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
public enum ICResult
{
Success,
FailBegin,
FailUpdate,
FailEnd
}
public ICResult ChangeIcon(string exeFilePath, byte[] iconData)
{
// Load executable
IntPtr handleExe = BeginUpdateResource(exeFilePath, false);
if (handleExe == null)
return ICResult.FailBegin;
// Get language identifier
CultureInfo currentCulture = CultureInfo.CurrentCulture;
int pid = ((ushort)currentCulture.LCID) & 0x3ff;
int sid = ((ushort)currentCulture.LCID) >> 10;
ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));
// Get pointer to data
GCHandle iconHandle = GCHandle.Alloc(iconData, GCHandleType.Pinned);
// Replace the icon
if (UpdateResource(handleExe, "#3", "#1", languageID, iconHandle.AddrOfPinnedObject(), (uint)iconData.Length))
{
if (EndUpdateResource(handleExe, false))
return ICResult.Success;
else
return ICResult.FailEnd;
}
else
return ICResult.FailUpdate;
}
}
Regarding lpType - in C++, you pass RT_ICON (or RT_GROUP_ICON). What value should I pass in C#?
The same question goes for lpName parameter.
I'm not sure about language identifier (I found this on Internet) since I cannot test it.
I'm also not sure whether I'm providing appropriate icon data. Currently, iconData contains the bytes from .ico file.
Can anybody point me to right direction?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是一些提示,这很难做到正确。通过谎报 lpType 参数来传递 RT_ICON。将其从字符串更改为 IntPtr 并传递 (IntPtr)3。
lpData 参数非常棘手。您需要按照资源编译器 (rc.exe) 编译数据的方式传递数据。我不知道它是否会破坏 .ico 文件的原始数据。唯一合理的尝试是使用 FileStream 将 .ico 文件中的数据读取到 byte[] 中,您似乎已经在这样做了。我认为该函数实际上是为了将资源从一个二进制图像复制到另一个二进制图像而设计的。你的方法奏效的几率不为零。
你还忽略了另一个潜在的问题,程序图标的资源ID不一定是1。通常不是,100往往是一个流行的选择,但任何事情都会发生。需要 EnumResourceNames 才能使其可靠。规则是最小编号的 ID 设置文件的图标。我实际上不确定这是否真的意味着资源编译器将最低的数字放在第一位,而 API 可能不会这样做。
一个非常小的故障模式是UpdateResource只能更新编号的资源项,而不能更新命名的资源项。使用名称代替数字并不罕见,但绝大多数图像都使用数字作为图标。
当然,如果没有 UAC 清单,这种方法起作用的可能性为零。您正在侵入通常没有写入权限的文件。
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.
我设法使用 ResourceHacker 并以这篇文章为例,在纯 C# 中实现了此功能。只需使用常规 .ico 作为输入即可。在 ResourceHacker (http://www.angusj.com/resourcehacker/) 中,您将看到图标标识符(在我的情况下为 1)和语言标识符(在我的情况下为 1043):
我使用了以下代码:
I managed to get this working in pure C# using ResourceHacker and this posting as an example. Just use a regular .ico as input. In ResourceHacker (http://www.angusj.com/resourcehacker/) you will see the icon identifier (in my case 1) and the language identifier (in my case 1043):
I used this code:
这是对我有用的解决方案。我无法在 .NET 中编写它,但设法编写了我在 C# 应用程序中引用的 C++ DLL。
C++ DLL
C++ 解决方案的内容 我正在构建 DLL:
C# 代码
这是我用来从以前编写的 DLL 导入
ChangeIcon
函数的 C# 代码:希望至少有人会发现这很有用。
This is the solution that worked for me. I was not able to write it in .NET, but have managed to write C++ DLL which I am referencing in my C# application.
C++ DLL
The contents of C++ solution I am building the DLL from:
C# code
This is C# code which I'm using to import
ChangeIcon
function from previously written DLL:Hope at least somebody will find this useful.
UpdateResource 的 C# 声明:
对于字符串资源类型或名称,只需传递字符串即可。
对于系统预定义的类型(例如
RT_ICON
)和 int ID(例如IDI_APPLICATION
),您可以将该整数值重新解释转换为指针,例如(char*)3< /code> 代表
RT_ICON
。C# declaration for UpdateResource:
For a string resource type or name, you just pass the string.
For system-predefined types like
RT_ICON
and int IDs likeIDI_APPLICATION
, you pass that integer value reinterpret-casting it to a pointer, like(char*)3
forRT_ICON
.