SaveMemory2 example for C++ 5.0 and later

void CSaveMemory2Dlg::OnSave() 
{
   LPSTR    lpMem;
   HFILE    hf;
   TCHAR     szText[256];
   OFSTRUCT OF;
   
   ILEADRasterIO *pRasterIO=NULL;
   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL,
                    IID_ILEADRasterIO, (void**)&pRasterIO);

   if(pRasterIO->SaveMemory2(m_LEADRasterView1.GetRaster(),
                            FILE_BMP, 4,
                            (QFactorConstants)0,
                            SAVE_REPLACE))
   {
      MessageBox(TEXT("Error calling SaveMemory"));
      return;
   }
   if(pRasterIO->GetMemoryInfo(m_LEADRasterView1.GetRaster(),
                               pRasterIO->GetSaveMemory2Handle(), 0, pRasterIO->GetSaveMemory2Size()))
   {
    pRasterIO->PutSaveMemory2Handle(0);
    MessageBox(TEXT("Error calling GetMemoryInfo"));
    return;
   }
   /*  Ask for a confirmation to save this file */
   wsprintf(szText,
            TEXT("File info: \r %u x %u x %u BPP \rSize in memory: %lu \rSize on disk %lu"),
            (unsigned int)pRasterIO->GetInfoWidth(),
            (unsigned int)pRasterIO->GetInfoHeight(),
            (unsigned int)pRasterIO->GetInfoBits(),
            (unsigned long)pRasterIO->GetInfoSizeMem(),
            (unsigned long)pRasterIO->GetInfoSizeDisk());
   if( MessageBox(szText, TEXT("Write this file on disk ?"), MB_YESNO) != IDYES )
   {
    pRasterIO->PutSaveMemory2Handle(0);
    return;
   }
   
   lpMem = (char*)GlobalLock((HGLOBAL)pRasterIO->GetSaveMemory2Handle());
   if( lpMem == NULL )
   {
    pRasterIO->PutSaveMemory2Handle(0);
    MessageBox(TEXT("Error calling GlobalLock"));
    return;
   }
   
   #ifdef UNICODE
      hf = OpenFile(TEXT("c:\\savemem.bmp"),&OF, OF_CREATE);
   #else
      hf = OpenFile ("c:\\savemem.bmp", &OF,OF_CREATE);
   #endif

   if( hf == -1 )
   {
    GlobalUnlock((HGLOBAL)pRasterIO->GetSaveMemory2Handle());
    pRasterIO->PutSaveMemory2Handle(0);
    MessageBox(TEXT("Error calling GlobalLock"));
    return;
   }
   if( _lwrite( hf, lpMem, pRasterIO->GetSaveMemory2Size()) != (UINT)pRasterIO->GetSaveMemory2Size())
    MessageBox(TEXT("Error calling GlobalLock"));
   else
    MessageBox(TEXT("savemem.cmp was saved successfully on disk."));
   _lclose( hf );
   GlobalUnlock((HGLOBAL)pRasterIO->GetSaveMemory2Handle());
   pRasterIO->PutSaveMemory2Handle(0);
   pRasterIO->Release();
}