Partager via


_AllocHand( ) API Library Routine

Returns a new MHANDLE of size hsize.

MHANDLE _AllocHand(unsigned int hsize)
unsigned int hsize;            /* Size of new memory handle in bytes. */

Remarks

_AllocHand( ) returns 0 when there is insufficient memory to fulfill the request. Memory allocated with _AllocHand( ) isn't initialized, and must be freed after use.

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

Example

In The following example, a character is replicated in memory supplied by _AllocHand( ). The API function REPLTOMH( ) below returns the memory handle to Visual FoxPro. From Visual FoxPro the memory handle is passed to API functions, which expect a memory handle argument (passed as an integer, "I").

Visual FoxPro Code

SET LIBRARY TO ALLOCHAN
mh = REPLTOMH("x", 120)
? MHTOFOX(mh)
? LEN(MHTOFOX(mh))
? MHTOFOX(mh)
= FREEMH(mh)

C Code

#include <pro_ext.h>

//   Replicate char argument to memory allocated with _AllocHand().
//   Return the memory handle to Visual FoxPro.
void FAR replToMH(ParamBlk FAR *parm)
{
   char FAR *rep;
   char c = *(char *) _HandToPtr(parm->p[0].val.ev_handle);
   MHANDLE mh;

   if ((mh = _AllocHand((int) parm->p[1].val.ev_long + 1)) == 0)
   {
      _Error(182);  // "Insufficient memory"
   }
   _HLock(mh);
   rep = _HandToPtr(mh);
   _MemFill(rep, c, (int) parm->p[1].val.ev_long);
   rep[parm->p[1].val.ev_long] = '\0';  // null terminate
   _HUnLock(mh);

   _RetInt(mh, 10);
}

//   Returns characters in memory handle.
//   Argument in call from Visual FoxPro
//   must be a valid Visual FoxPro memory handle.
void FAR MHToFoxString(ParamBlk FAR *parm)
{
   char FAR *string;
   MHANDLE mh = parm->p[0].val.ev_long;

   _HLock(mh);
   string = _HandToPtr(mh);
   _RetChar(string);
   _HUnLock(mh);
}

//   Frees memory handle.  Argument in call from
//   Visual FoxPro must be a valid
//   Visual FoxPro memory handle.
void FAR freeMH(ParamBlk FAR *parm)
{
   _FreeHand((MHANDLE) parm->p[0].val.ev_long);
}

FoxInfo myFoxInfo[] = {
   {"REPLTOMH", (FPFI) replToMH, 2, "C,I"},
   {"MHTOFOX", (FPFI) MHToFoxString, 1, "I"},
   {"FREEMH", (FPFI) freeMH, 1, "I"},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};

See Also

Concepts

API Library Routines A-Z

Reference

_FreeHand( ) API Library Routine

_GetHandSize( ) API Library Routine

_HandToPtr( ) API Library Routine

_HLock( ) API Library Routine

_HUnLock( ) API Library Routine

_MemAvail( ) API Library Routine

_SetHandSize( ) API Library Routine

Other Resources

API Library Routines by Category