Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

When i am trying to add resources to another file at run time, some of the earlier resources are getting deleted. Please find the source code below:

C++
void CResourceIncludeSampleDlg::OnBnClickedButton1()
{
	CString strInputFile = _T("C:\\SampleData\\FileToInsert.zip"); // This File is 100 MB

	HANDLE hFile   = CreateFile(strInputFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	DWORD FileSize = GetFileSize(hFile, NULL);

	BYTE *pBuffer = new BYTE[FileSize];
	DWORD dwBytesRead;
	ReadFile(hFile, pBuffer, FileSize, &dwBytesRead, NULL);

	for (int iIndex = 1; iIndex <= 4; iIndex++)
	{
		InsertResource(FileSize, iIndex, pBuffer);
	}

	CloseHandle(hFile);
}

void CResourceIncludeSampleDlg::InsertResource(DWORD FileSize, int iIndex, BYTE *pBuffer)
{
	CString strOutputFile = _T("C:\\SampleData\\ResourceIncludeSample_Source.exe");
	int iResourceID = 300 + iIndex;

	HANDLE hResource = BeginUpdateResource(strOutputFile, FALSE);
	if (INVALID_HANDLE_VALUE != hResource)
	{
		if (UpdateResource(hResource, _T("VIDEOBIN"), MAKEINTRESOURCE(iResourceID), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
						   (LPVOID)pBuffer, FileSize) == TRUE)
		{
			EndUpdateResource(hResource, FALSE);
		}
	}
}


After completion of the execution, i am expecting output as 301, 302, 303 and 304 added under "VIDEOBIN" category. But only 2 (sometimes 3) resources are present. One resource is always deleted.

Could you please let me know what could be wrong or any fix for the same ?
Any help or sample source code is greatly appreciated.

Thanks and Regards,
YKK Reddy
Posted

1 solution

Your code only calls EndUpdateResource if UpdateResource returns TRUE. So it is possible that some calls are failing, but you will never know if that happens. Add some code to check the return value and take some appropriate action if it fails. But in either case I think you should always call EndUpdateResource to ensure the system releases locks and resources etc.
 
Share this answer
 
Comments
[no name] 25-Mar-15 9:18am    
When i debug the code, i found that the execution was proper. EndUpdateResource() was being called always. But at the end, one inserted resource was missing.
Richard MacCutchan 25-Mar-15 9:33am    
There must be something else happening, but only your debugger will show what it may be. You may also like to note the comments in the Remarks section of https://msdn.microsoft.com/en-us/library/windows/desktop/ms648049%28v=vs.85%29.aspx.
[no name] 10-Apr-15 5:53am    
Hi All,

Are there any updates to this question.
Is it a Win32 API limitation or some sort of bottleneck ?

please suggest any workaround (if present).

Thanks and Regards,
Kishor Reddy
Richard MacCutchan 10-Apr-15 7:26am    
Obviously not. This would appear to be something specific to your application. You may need to revisit my previous comments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900