SaveMemory example for C++ Builder
void __fastcall TForm1::SaveMemory1Click(TObject *Sender)
{
L_HANDLE hMem = 0;
int hf, iSize;
void *lpMem;
AnsiString cMsg, cTemp;
if(Lead1->SaveMemory (hMem, FILE_CMP, 24, 2, iSize) != SUCCESS)
{
MessageDlg ("Error calling SaveMemory", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
if( Lead1->GetMemoryInfo (hMem, 0, iSize) != SUCCESS)
{
GlobalFree ((HGLOBAL)hMem);
return;
}
// Ask for a confirmation to save this file
cMsg = "Write this file on disk?\n\nFile info:\n";
cMsg += IntToStr(Lead1->InfoWidth) + " x ";
cMsg += IntToStr(Lead1->InfoHeight) + " x ";
cMsg += IntToStr(Lead1->InfoBits)+ " BPP\n";
cMsg += "Size in memory: " + IntToStr(Lead1->InfoSizeMem);
cMsg += "\nSize on disk : " + IntToStr(Lead1->InfoSizeDisk);
if(MessageDlg(cMsg, mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) != mrYes)
{
GlobalFree ((HGLOBAL)hMem);
return;
}
lpMem = GlobalLock((HGLOBAL)hMem);
if(lpMem==0)
{
GlobalFree ((HGLOBAL)hMem);
MessageDlg ("Error calling GlobalLock", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
hf = FileCreate("c:\\savemem.cmp");
if(hf==-1)
{
GlobalUnlock ((HGLOBAL)hMem);
GlobalFree ((HGLOBAL)hMem);
MessageDlg ("Error calling GlobalLock", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
if( FileWrite(hf, lpMem, iSize) != iSize)
MessageDlg ("Error calling FileWrite", mtError, TMsgDlgButtons() << mbOK, 0);
else
MessageDlg ("The file was saved successfully on disk.", mtInformation, TMsgDlgButtons() << mbOK, 0);
FileClose (hf);
GlobalUnlock ((HGLOBAL)hMem);
GlobalFree ((HGLOBAL)hMem);
}