Partager via


_AllocMemo( ) API Library Routine

Allocates size bytes of space in the memo file for a field and inserts the new block number in the associated record in the database.

long _AllocMemo(Locator FAR *fld, long size)
Locator FAR *fld;            /* Pointer to locator
 that defines the memo field. */
long size;                     /* Size of allocated space in bytes. */

Remarks

When _AllocMemo( ) is successful, it returns the location in the file to begin writing; otherwise, it returns – 1.

Caution   Writing more than size bytes causes memo file corruption.

If you plan to replace a memo field using direct methods, be sure your routine calls _AllocMemo( ) before writing to the memo file, even if the new value is the same length or smaller than the original. This prevents problems in multi-user situations. _AllocMemo( ) reuses the previously allocated space when it's safe to do so.

For more information on how to create an API library and integrate it with Visual FoxPro, see Accessing the Visual FoxPro API.

Example

The following example creates new memo field contents.

Visual FoxPro Code

SET LIBRARY TO ALLOMEMO
CREATE TABLE wMemo (memoField M)
APPEND BLANK
= NewMemo(@MemoField, "Hello, World.")
APPEND BLANK
= NewMemo(@MemoField, "Isn't this fun?")

C Code

#include <pro_ext.h>

FAR newMemo(ParamBlk FAR *parm)
{
   Locator FAR *memoFldLoc;
   FCHAN fchMemo;
   int memoLen;
   long loc;

   if ((fchMemo = _MemoChan(-1)) == -1)
   {
      _UserError("_MemoChan() failed");
   }
   memoFldLoc = &parm->p[0].loc;
   memoLen = parm->p[1].val.ev_length;

   if ((loc = _AllocMemo(memoFldLoc, memoLen)) == -1)
   {
      _UserError("_AllocMemo() failed");
   }
   _FSeek(fchMemo, loc, FS_FROMBOF);
   _HLock(parm->p[1].val.ev_handle);
   _FWrite(fchMemo, _HandToPtr(parm->p[1].val.ev_handle), memoLen);
   _HUnLock(parm->p[1].val.ev_handle);
}

FoxInfo myFoxInfo[] = {
   {"NEWMEMO", (FPFI) newMemo, 2, "R,C"},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};

See Also

_DBReplace( ) API Library Routine | _FindMemo( ) API Library Routine | _MemoChan( ) API Library Routine | _MemoSize( ) API Library Routine | Accessing the Visual FoxPro API