LoadMemory example for C++ 5.0 and later

This example uses WIN32 API calls to load a file into memory, then uses the LoadMemory method to load the memory-resident file into a bitmap.

void CTutorDlg::OnLoadMemory() 
{
   HFILE    hf;
   HGLOBAL  hMem;
   LPSTR    lpMem;
   long     iSize;
   ILEADRasterIO *pRasterIO=NULL;

#ifdef UNICODE

hf = _wopen (TEXT("d:\\lead14\\dist\\images\\image1.cmp"), OF_READ);

#else

hf = _lopen ("d:\\lead14\\dist\\images\\image1.cmp", OF_READ);

   #endif
   iSize = _llseek( hf, 0, SEEK_END );
   _llseek( hf, 0, SEEK_SET );
   if( (hMem = GlobalAlloc( GMEM_MOVEABLE, iSize)) == NULL )
   {
      _lclose( hf );
      MessageBox(TEXT("Not enough memory to hold the file in memory!") );
      return;
   }
   if( (lpMem = (LPSTR)GlobalLock(hMem)) == NULL )
   {
      _lclose( hf );
      GlobalFree( hMem );
      MessageBox(TEXT("Not enough memory to hold the file in memory!") );
      return;
   }
   if( _hread( hf, lpMem, iSize) != iSize )
   {
      _lclose( hf );
      GlobalUnlock( hMem );
      GlobalFree( hMem );
      MessageBox(TEXT("Error reading file") );
      return;
   }
   _lclose( hf );
   GlobalUnlock( hMem );
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
                   IID_ILEADRasterIO, (void**)&pRasterIO);
   if(pRasterIO->LoadMemory(m_LEADRasterView1.GetRaster(),
                            (long)hMem, 0, 0, -1, iSize ) )
      MessageBox(TEXT("Error calling LoadMemory") );
   else
      MessageBox(TEXT("LoadMemory succeeded") );
   GlobalFree( hMem );
   pRasterIO->Release();
}