Return of a Value to Visual FoxPro

The method you use to return a value from your program to Visual FoxPro depends on whether you're creating an ActiveX control or an FLL library.

Returning a Value from an ActiveX Control

To return a value from the ActiveX control to Visual FoxPro, use the RETURN statement in the control, passing a single value, as in the following example:

#define VERSION 101

// other code here

long CPyCtrl::GetVersion()
{
   // set the version number here in variable fVersion
   return VERSION;
}

Returning a Value from an FLL Library

To return values from an FLL library, use API functions, not native C or C++ commands. The following functions allow you to return values to Visual FoxPro.

Note   Don't use the following API function to return a value from an .ocx file; use the RETURN statement. The API return functions should only be used in FLL libraries.

Function Description
_RetChar(char *string) Sets the function return value to a null-terminated string.
_RetCurrency(CCY cval, int width) Sets the function return value to a currency value.
_RetDateStr(char *string) Sets the function return value to a date. The date is specified in mm/dd/yy[yy] format.
_RetDateTimeStr(char *string) Sets the function return value to a date and time specified in mm/dd/yy[yy] hh:mm:ss format.
_RetFloat(double flt, int width, int dec) Sets the function return value to a float value.
_RetInt(long ival, int width) Sets the function return value to a numeric value.
_RetLogical(int flag) Sets the function return value to a logical value. Zero is considered FALSE. Any non-zero value is considered TRUE.
_RetVal(Value *val) Passes a complete Visual FoxPro Value structure; any Visual FoxPro data type except for memo can be returned. You must call _RetVal( ) to return a string that contains embedded null characters or to return a .NULL. value.

Note   To return the value of an object data type, use the _RetVal() function, filling in the ev_object field in the Value structure.

The following example, Sum, accepts a reference to a numeric field in a table and uses _RetFloat to return the sum of the values in the field:

#include <Pro_ext.h>

Sum(ParamBlk *parm)
{
// declare variables
double tot = 0, rec_cnt;
int i = 0, workarea = -1; // -1 is current workarea
Value val;

// GO TOP
_DBRewind(workarea);

// Get RECCOUNT( )
rec_cnt = _DBRecCount(workarea);

// Loop through table
for(i = 0; i < rec_cnt; i++)
{ 
   //Place value of the field into the Value structure
   _Load(&parm->p[0].loc, &val);

   // add the value to the cumulative total
   tot += val.ev_real;

   // SKIP 1 in the workarea
   _DBSkip(workarea, 1);
} 

// Return the sum value to Visual FoxPro
_RetFloat(tot, 10, 4); 
}
// The Sum function receives one Reference parameter
FoxInfo myFoxInfo[] = {
   {"SUM", Sum, 1,"R"} 
};
FoxTable _FoxTable = {
   (FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};

Assuming there's a numeric field named amount in the currently open table, the following line of code in a Visual FoxPro program calls the function:

? SUM(@amount)

See Also

Passing and Reception of Parameters | Passing of Parameters to Visual FoxPro API Functions | Accessing the Visual FoxPro API | Extending Visual FoxPro with External Libraries | Access to Visual FoxPro Variables and Fields | API Library Construction