Save/Open Bitmap

Heya,

I'm currently working on an application that explores the different effetcs that can be achieved with GLSL. My problem is that I want the ability to save a bitmap and then use it as the current the texture. Below is the code I'm using to save the bitmap. I then send a message to the the View to update the texture(passing through the full path) and try to re open the file.

CString szFullPath = "textures/shade.bmp";
if(dlg.DoModal() == IDOK)
{

CString szPathName = dlg.GetPathName();

FILE *pFile = fopen(szPathName, "wb");
if(pFile == NULL)
{
AfxMessageBox(IDS_FILE_CREATE_ERROR_MESSAGE);
return;
}
BITMAPFILEHEADER bmfh;
int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
LONG lImageSize = BMIH.biSizeImage;
LONG lFileSize = nBitsOffset + lImageSize;
bmfh.bfType = 'B'+('M'<<8);
bmfh.bfOffBits = nBitsOffset;
bmfh.bfSize = lFileSize;
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), pFile);

UINT nWrittenInfoHeaderSize = fwrite(&BMIH, 1, sizeof(BITMAPINFOHEADER), pFile);
UINT nWrittenDIBDataSize = fwrite(m_pDrawingSurfaceBits, 1, lImageSize, pFile);

szFullPath = dlg.GetPathName();

if( ferror( pFile ) )
{
perror( "Read error" );
}

fclose(pFile);
pFile = NULL;

}

It crashes the application with no error messages. After stepping through itit throws an unhandled exception after trying to open the file again. Any ideas?
[1658 byte] By [lk_] at [2007-11-19 7:39:52]