LoadMemory example for C++ Builder
This example uses WIN32 API calls to load a file into memory, and then uses the LoadMemory method to load the memory-resident file into a bitmap.
long hf; // For WIN32 declare this as Long
HGLOBAL hMem; // For WIN32 declare this as Long
void * lpMem = NULL;
long iSize;
LEADRasterIO* pRasterIO= NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
// Load the file into memory
hf = FileOpen("v:\\images\\image1.cmp", fmOpenRead);
if (hf <= 0 )
{
MessageDlg("Error opening file!", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
iSize = FileSeek(hf,0,2);
FileSeek(hf, 0, 0);
hMem = GlobalAlloc(GMEM_MOVEABLE,iSize);
if (hMem == 0)
{
FileClose (hf);
MessageDlg("Not enough memory to hold the file in memory!", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
lpMem= GlobalLock(hMem);
if (!lpMem)
{
FileClose (hf);
GlobalFree(hMem);
MessageDlg("Not enough memory to hold the file in memory!", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
if (FileRead(hf, lpMem, iSize) < iSize)
{
FileClose (hf);
GlobalUnlock(hMem);
GlobalFree(hMem);
MessageDlg("Error reading file!", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
GlobalUnlock (hMem);
FileClose (hf);
//Load the bitmap from the memory-resident file.
if (pRasterIO->LoadMemory (LEADRasterView1->Raster, (long)hMem, 0, 0, -1, iSize) != 0)
MessageDlg ("Error calling LoadMemory", mtError, TMsgDlgButtons() << mbOK, 0);
else
MessageDlg ("LoadMemory succeeded", mtInformation, TMsgDlgButtons() << mbOK, 0);
GlobalFree (hMem);
pRasterIO-> Release( );