替换exe可执行文件的图标:
1、首先取得源资源的指针;
2、利用UpdateResource函数进行替换;
void
CDlgTest2Dlg::OnBTNUpdateResource()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
// TODO: Add your control notification handler code here
HMODULE hExe;
HANDLE hUpdateRes;
HRSRC hRes;
HANDLE hResLoad;
char *lpResLock;
BOOL result;
hExe=LoadLibrary("a.exe");
if (!hExe)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("载入可执行文件失败!");
}
hRes=FindResource(hExe,MAKEINTRESOURCE(IDI_ICON1),RT_GROUP_ICON);
if (!hRes)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("FindResource失败!");
}
hResLoad=LoadResource(hExe,hRes);
if (!hResLoad)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("LoadResource失败!");
}
lpResLock=(char*)LockResource(hResLoad);
if (!lpResLock)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("LockResource失败!");
}
hUpdateRes=BeginUpdateResource("switch.exe",FALSE);
if (!hUpdateRes)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("BeginUpdateResource失败!");
}
result=UpdateResource(hUpdateRes,RT_GROUP_ICON,MAKEINTRESOURCE(IDR_MAINFRAME),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),lpResLock,SizeofResource(hExe,hRes));
if (!result)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("UpdateResource失败!");
}
if (!EndUpdateResource(hUpdateRes, FALSE))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("Could not write changes to file.");
}
// Clean up.
if (!FreeLibrary(hExe))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MessageBox("Could not free executable.");
}
}