I am trying to debug the legacy code. The code compresses the file (with different sections) during some operation. And later tries to decompress the same.
Now when new sections are added as part of another feature, the legacy code, throws exception for this code, as the SECTION_ERROR. It s due to some MAGIC number written for this section is not matching .

that is just background.

Now, I was trying to debug in release mode and found that, when t is writting the magic number:
size_t MemMappedCFileWriteLock::Write(const void * pBuffer, size_t nSize, size_t nCount)
	size_t stWritten = 0;
	size_t stTotal = nCount * nSize; // nSize should be 1 unless someone changes AIPerist
	size_t stRemain = stTotal;
	while ( stWritten < stTotal)
		DWORD dwWriteCount = 0;
		DWORD dwWritten;
		if (stRemain > ULONG_MAX)
			dwWriteCount = ULONG_MAX;
			dwWriteCount = (DWORD)stRemain; // <= ULONG_MAX
		bool bRet = (WriteFile(m_hFile, (char*)pBuffer + stWritten, dwWriteCount, &dwWritten, NULL) != 0);
		if (!bRet)
		stWritten += (size_t)dwWriteCount;
		stRemain -= (size_t)dwWriteCount;
	return stWritten;

    _In_ HANDLE hFile,
    _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer,
    _In_ DWORD nNumberOfBytesToWrite,
    _Out_opt_ LPDWORD lpNumberOfBytesWritten,
    _Inout_opt_ LPOVERLAPPED lpOverlapped
The debugger showed the value of nsize and nCount as 1, 4.
But the dwWriteCount = (DWORD)stRemain; , sends the no of bytes as 1 to the WriteFile function.

Does this casting look suspicious ? SShould it not send, 4 bytes to WriteFile function !

Thanks a lot