16,471
社区成员
发帖
与我相关
我的任务
分享
bool ChangeExeBmp(LPCSTR BmpFile, LPCSTR ExeFile,UINT nBmpID)
{
HGLOBAL hResLoad; // handle to loaded resource
HMODULE hExe; // handle to existing .EXE file
HRSRC hRes; // handle/ptr. to res. info. in hExe
HANDLE hUpdateRes; // update resource handle
LPVOID lpResLock; // pointer to resource data
BOOL result;
// Load the .EXE file that contains the dialog box you want to copy.
hExe = LoadLibrary(ExeFile);
if (hExe == NULL)
{
TRACE(TEXT("Could not load exe."));
return 0;
}
// Locate the dialog box resource in the .EXE file.
hRes = FindResource(hExe, MAKEINTRESOURCE(nBmpID), RT_BITMAP);
if (hRes == NULL)
{
TRACE(TEXT("Could not locate Bitmap."));
return 0;
}
// Load the dialog box into global memory.
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
TRACE(TEXT("Could not load Bitmap."));
return 0;
}
// Lock the dialog box into global memory.
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
TRACE(TEXT("Could not lock bitmap."));
return 0;
}
// Open the file to which you want to add the dialog box resource.
hUpdateRes = BeginUpdateResource(ExeFile, 0);
if (hUpdateRes == NULL)
{
TRACE(TEXT("Could not open file for writing."));
return 0;
}
// Add the dialog box resource to the update list.
result = UpdateResource(hUpdateRes, // update resource handle
RT_BITMAP, // change dialog box resource
MAKEINTRESOURCE(nBmpID), // dialog box id
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), // neutral language
lpResLock, // ptr to resource info
SizeofResource(hExe, hRes)); // size of resource info
if (result == FALSE)
{
TRACE(TEXT("Could not add resource."));
return 0;
}
// Write changes to FOOT.EXE and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
TRACE(TEXT("Could not write changes to file."));
return 0;
}
// Clean up.
if (!FreeLibrary(hExe))
{
TRACE(TEXT("Could not free executable."));
return 0;
}
return 1;
}
bool UpdateResFromFile(char* filename, UINT resType, UINT resID, DWORD buffersize, char* destModule)
{
HANDLE hFile;
hFile = CreateFile(filename,
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"Could not open file.";
}
char* Data = new char[buffersize];
DWORD dwBytes;
BOOL result = ReadFile(hFile, (void*)Data, buffersize, &dwBytes, NULL);
CloseHandle(hFile);
HANDLE hUpdateRes;
hUpdateRes = BeginUpdateResource(destModule, FALSE);
if (hUpdateRes == NULL)
{
cout<<"Could not open file for writing.";
}
result = UpdateResource(hUpdateRes, // update resource handle
resType,
MAKEINTRESOURCE(resID),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), // neutral language
(void*)Data, // ptr to resource info
dwBytes); // size of resource info.
if (result == FALSE)
{
cout<<"Could not add resource.";
}
// Write changes to FOOT.EXE and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
cout<<"Could not write changes to file.";
}
delete Data;
return result;
}