把lpExeFile的图标替换成和lpIconFile的图标
用法 ChangeExeIcon(L"G:\\yourImageName.ico",L"C:\\qq.exe");
BOOL ChangeExeIcon(LPWSTR lpIconFile, LPWSTR lpExeFile)
{
ICONDIR stID = { 0 };
ICONDIRENTRY stIDE = { 0 };
GRPICONDIR stGID = { 0 };
HANDLE hFile = NULL;
DWORD nSize = 0;
DWORD nGSize = 0;
DWORD dwReserved = 0;
HANDLE hUpdate = NULL;
PBYTE pIcon = NULL;
PBYTE pGrpIcon = NULL;
BOOL ret = FALSE;
hFile = ::CreateFileW(lpIconFile, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return false;
}
ZeroMemory(&stID, sizeof(ICONDIR));
if (!::ReadFile(hFile, &stID, sizeof(ICONDIR), &dwReserved, NULL)) {
goto _EXIT_PROC;
}
ZeroMemory(&stIDE, sizeof(ICONDIRENTRY));
if (!::ReadFile(hFile, &stIDE, sizeof(ICONDIRENTRY), &dwReserved, NULL)) {
goto _EXIT_PROC;
}
nSize = stIDE.dwBytesInRes;
pIcon = (PBYTE)malloc(nSize);
::SetFilePointer(hFile, stIDE.dwImageOffset, NULL, FILE_BEGIN);
if (::ReadFile(hFile, (LPVOID)pIcon, nSize, &dwReserved, NULL)) {
goto _EXIT_PROC;
}
ZeroMemory(&stGID, sizeof(GRPICONDIR));
stGID.idCount = stID.idCount;
stGID.idReserved = 0;
stGID.idType = 1;
CopyMemory(&stGID.idEntries, &stIDE, 12);
stGID.idEntries.nID = 0;
nGSize = sizeof(GRPICONDIR);
pGrpIcon = (PBYTE)malloc(nGSize);
CopyMemory(pGrpIcon, &stGID, nGSize);
hUpdate = ::BeginUpdateResourceW(lpExeFile, false);
if (hUpdate == NULL) {
goto _EXIT_PROC;
}
if (!::UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(1), 0, (LPVOID)pGrpIcon, nGSize)) {
goto _EXIT_PROC;
}
if (!::UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(1), 0, (LPVOID)pIcon, nSize)) {
goto _EXIT_PROC;
}
if (!::EndUpdateResource(hUpdate, false)) {
goto _EXIT_PROC;
}
ret = TRUE;
_EXIT_PROC:
if (hFile != INVALID_HANDLE_VALUE) {
::CloseHandle(hFile);
}
if (pIcon != NULL) {
free(pIcon);
}
if (pGrpIcon != NULL) {
free(pGrpIcon);
}
return ret;
}