_MemMove( ) API Library Routine Example

The following example uses _MemMove( ) to implement a substring function. From Visual FoxPro, MEMMOVE(s, n1, n2) returns the substring of s starting from position n1 and through position n2.

Visual FoxPro Code

SET LIBRARY TO MEMMOVE 
? MEMMOVE("Hello, world.", 8, 10)  && returns "wor"

C Code

#include <pro_ext.h>

FAR Example(ParamBlk FAR *parm)
{
   int SubstrLen;
   MHANDLE bufferHandle;
   char FAR *FirstDest;

   SubstrLen = parm->p[2].val.ev_long - parm->p[1].val.ev_long + 1;
   if ((bufferHandle = _AllocHand(SubstrLen + 1)) == 0)
   {
      _Error(182); // "Insufficient memory"
   }
   _HLock(bufferHandle);
   _HLock(parm->p[0].val.ev_handle);
   FirstDest = (char FAR *) _HandToPtr(parm->p[0].val.ev_handle) +
      parm->p[1].val.ev_long - 1;

   _MemMove(_HandToPtr(bufferHandle), FirstDest, SubstrLen);

   ((char FAR *) _HandToPtr(bufferHandle))[SubstrLen] = '\0';
   _RetChar(_HandToPtr(bufferHandle));
   _HUnLock(bufferHandle);
   _HUnLock(parm->p[0].val.ev_handle);
}
FoxInfo myFoxInfo[] = {
   {"MEMMOVE", (FPFI) Example, 3, "C,I,I"},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};