CornedBee
Jun 4th, 2002, 07:20 AM
Why does this code produce an access violation at the CopyMemory line?
bool CDbBitmap::BmpToDibSec(BYTE *pData)
{
PBITMAPFILEHEADER pBMFH = (PBITMAPFILEHEADER)pData;
ASSERT(pBMFH->bfType == 0x4D42); // check again if really BMP
DWORD dwBitOffset = pBMFH->bfOffBits;
BITMAPINFO *pbi = (BITMAPINFO*)(pData + sizeof(BITMAPFILEHEADER));
// Create the DIB section
BYTE *pBits;
HBITMAP hB = ::CreateDIBSection(NULL, pbi, DIB_RGB_COLORS, (PVOID*)&pBits, NULL, 0);
::CopyMemory(pBits, pData + dwBitOffset, pBMFH->bfSize - dwBitOffset);
Attach((HGDIOBJ)hB);
return true;
}
pData is a pointer to memory which contains a complete BMP file. CreateDIBSection should allocate memory and set pBits to point to this memory.
I tried using HeapSize to get the block size of pBits but since it is allocated by the system it fails.
bool CDbBitmap::BmpToDibSec(BYTE *pData)
{
PBITMAPFILEHEADER pBMFH = (PBITMAPFILEHEADER)pData;
ASSERT(pBMFH->bfType == 0x4D42); // check again if really BMP
DWORD dwBitOffset = pBMFH->bfOffBits;
BITMAPINFO *pbi = (BITMAPINFO*)(pData + sizeof(BITMAPFILEHEADER));
// Create the DIB section
BYTE *pBits;
HBITMAP hB = ::CreateDIBSection(NULL, pbi, DIB_RGB_COLORS, (PVOID*)&pBits, NULL, 0);
::CopyMemory(pBits, pData + dwBitOffset, pBMFH->bfSize - dwBitOffset);
Attach((HGDIOBJ)hB);
return true;
}
pData is a pointer to memory which contains a complete BMP file. CreateDIBSection should allocate memory and set pBits to point to this memory.
I tried using HeapSize to get the block size of pBits but since it is allocated by the system it fails.