Why does this function cause an assertion failure on my dll?

Debug Assertion failed!
...
File: Wingdi.cpp
Line: 1011


the code:

void
SaveBitmapEx(CString sFile, CBitmap *bitmap, CPalette *pal)
{
// create a DIB bitmap
int bmData;
BITMAPINFOHEADER bi;
memset(&bi, 0, sizeof(bi));
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biPlanes = 1;
bi.biCompression = BI_RGB;
// get and store dimensions of bitmap
BITMAP bm;
bitmap->GetObject(sizeof(bm),(LPSTR)&bm);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
// get number of bits required per pixel
int bits = bm.bmPlanes * bm.bmBitsPixel;
if (bits <= 1)
bi.biBitCount = 1;
else if (bits <= 4)
bi.biBitCount = 4;
else if (bits <= 8)
bi.biBitCount = 8;
else
bi.biBitCount = 24;

// calculate color table size
int biColorSize=0;
if (bi.biBitCount!=24) biColorSize=(1<<bi.biBitCount);
biColorSize*=sizeof(RGBQUAD);
// calculate picture data size
bi.biSizeImage=(DWORD)bm.bmWidth * bi.biBitCount; //bits per row
bi.biSizeImage=(((bi.biSizeImage) + 31) / 32) * 4;//DWORD aligned
bi.biSizeImage*=bm.bmHeight; //bytes required for whole bitmap
// return size to caler in case they want to save to file
bmData=bi.biSize + biColorSize;
// allocate a hunk of memory to hold header, color table and picture data
HANDLE hDIB = ::GlobalAlloc(GHND, bi.biSize + biColorSize + bi.biSizeImage);
// get a memory pointer to this hunk by locking it
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)::GlobalLock(hDIB);
// copy our header structure into hunk
*lpbi=bi;
// get a device context and select our bitmap's palette into it
CDC dc;
dc.Attach(::GetDC(NULL));
CPalette *pPal = dc.SelectPalette(pal,FALSE);
dc.RealizePalette();
// load our memory hunk with the color table and picture data
::GetDIBits(dc.m_hDC, (HBITMAP)bitmap->m_hObject, 0, (UINT)bi.biHeight, (LPSTR)lpbi +
(WORD)lpbi->biSize + biColorSize, (LPBITMAPINFO)lpbi,
DIB_RGB_COLORS);
// clean up
::GlobalUnlock(hDIB);
dc.SelectPalette(pPal,FALSE);
dc.RealizePalette();
// get a memory pointer to it
LPBYTE lpBitmap=(LPBYTE)::GlobalLock(hDIB);
int bmSize=::GlobalSize(hDIB);
// create file
CFile file;
file.Open((LPCTSTR) sFile, CFile::modeCreate|CFile::modeWrite);
// write the bitmap header
BITMAPFILEHEADER bmfh;
bmfh.bfType='MB'; //(actually 'BM' for bitmap)
bmfh.bfSize=sizeof(BITMAPFILEHEADER)+bmSize;
bmfh.bfReserved1=0;
bmfh.bfReserved2=0;
bmfh.bfOffBits=bmData;
file.Write(&bmfh,sizeof(BITMAPFILEHEADER));

// write the bitmap body
file.Write(lpBitmap,bmSize);
// cleanup
file.Close();
::GlobalUnlock(hDIB);
::GlobalFree(hDIB);
}


int
CaptureWindow ( HWND hWndSrc )
{

CWnd* pWnd = CWnd::FromHandle(hWndSrc);
CBitmap bitmap;
CWindowDC dc(pWnd);
CDC memDC;
CRect rect;
bool bFin;
CString sMaskFichero, sFichero, sConvert;
int i;
CFile f;
CFileException ex;


memDC.CreateCompatibleDC(&dc);
pWnd->GetWindowRect(rect);
bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);
// Create logical palette if device support a palette
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);//256
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;

pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );//255

// Create the palette
pal.CreatePalette( pLP );

delete[] pLP;
}

sMaskFichero = "C:\\temp";
i=0;
bFin = false;
for(i=0;i<=100 && !bFin;i++)
{
sFichero = "";
sConvert = "";
sConvert.Format("%d",i);
sFichero = sMaskFichero + sConvert + ".bmp";
if (!f.Open((LPCTSTR) sFichero, CFile::modeRead | CFile::shareDenyWrite, &ex))
{
bFin = true;
}
}
if (bFin)
{
SaveBitmapEx(sFichero,&bitmap,&pal);
pal.DeleteObject();
memDC.SelectObject(pOldBitmap);
bitmap.DeleteObject();

}
return (i);
}

extern "C" __declspec(dllexport)
int
CapturarObjeto( HWND hWnd )
{
int nTemp;

//AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
nTemp = CaptureWindow(hWnd);
return(nTemp);
}

Thanks all!