Creating a Basic FLL Library

Because an FLL library is essentially a DLL with calls to the Visual FoxPro API, you create an FLL library by following the steps in your development environment for creating a DLL.

To create a project for the FLL library

  1. Start Microsoft Visual C/C++.
  2. From the File menu, choose New.
  3. In the New dialog box, choose Project Workspace.
  4. In the New ProjectWorkspace dialog box, specify a project name.
  5. In the Type list, choose Dynamic-Link Library.

After creating the basic DLL structure, you add the functions you want to be able to call from Visual FoxPro. The following sections provide skeletons for creating functions in both C and C++.

Setting Up a Library Template

Each function library that you create has the same basic structure. By using a template for the structure, all you have to do is fill in the blanks that apply to your specific library routine.

There are five elements in a Visual FoxPro library template:

  1. #include statement
  2. Function definition
  3. Function code
  4. FoxInfo structure
  5. FoxTable structure

A Sample C Template

You can use the following template to create libraries written in C:

#include <Pro_ext.h>

void Internal_Name(ParamBlk *parm)
{
// function code goes here.
}

FoxInfo myFoxInfo[] = {
   {"FUNC_NAME", (FPFI) Internal_Name, 0, ""},
};

FoxTable _FoxTable = {
   (FoxTable *)0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};

A Sample C++ Template

For C++ routines, you can use the following template. This template differs from the C template because it declares the FoxTable structure as external:

#include <Pro_ext.h>

void Internal_Name(ParamBlk  *parm)
{
// function code goes here.
}

   FoxInfo myFoxInfo[] = {
      {"FUNC_NAME", (FPFI) Internal_Name, 0, ""},
   };

extern "C" {
   FoxTable _FoxTable = {
      (FoxTable *)0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
   };
}

Using the Template

To use the header file and create a compiled library, you need:

  • The header file Pro_ext.h. You can print this file to see the function declarations, typedefs, and structs used in the Visual FoxPro API.
  • The file Winapims.lib.

Both of these files are installed in the API subdirectory when you install Visual FoxPro.

The function definition returns void and expects the following parameter: ParamBlk *parm. The ParamBlk structure is discussed under Passing and Reception of Parameters.

Other than the files listed above, the only other required elements of a Visual FoxPro library are the FoxInfo and FoxTable structures.

Using FoxInfo and FoxTable Structures

Your library functions communicate with Visual FoxPro through the FoxInfo structure. From this structure, Visual FoxPro determines the function name and the number and type of parameters. The FoxTable structure is a linked list that keeps track of the FoxInfo structures. See Pro_ext.h in the Visual FoxPro API directory for the FoxInfo and FoxTable struct definitions.

FoxInfo Structure

The FoxInfo structure is the vehicle used to communicate function names and parameter descriptions between Visual FoxPro and your library. A generic FoxInfo structure looks like this:

FoxInfo arrayname[ ] = {
   {funcName1, FPFI function1, parmCount1, parmTypes1}
   {funcName2, FPFI function2, parmCount2, parmTypes2}
      . . .
   {funcNameN, FPFI functionN, parmCountN, parmTypesN}
};

The placeholders are defined as follows:

  • arrayname
    A variable of type FoxInfo. Note that you can include several FoxInfo structure lines in this array.
  • funcName
    Contains the name (in uppercase and no more than 10 characters) that the Visual FoxPro user calls to invoke your function.
  • function
    The address of your C language routine. This is the exact (case-sensitive) name you use to define your function.
  • parmCount
    Specifies the number of parameters described in the parmTypes string or one of the following flag values.
    Value Description
    INTERNAL Specifies that the function cannot be called directly from Visual FoxPro.
    CALLONLOAD Specifies that the routine is to be called when the library is loaded. CALLONLOAD can't call any routine that returns results to Visual FoxPro.
    CALLONUNLOAD Specifies that the routine is to be called when the library is unloaded or when the Visual FoxPro QUIT command is issued. CALLONUNLOAD cannot call any routine that returns results to Visual FoxPro.
  • parmTypes
    Describes the data type of each parameter. The following table lists the valid values for parmTypes.
    Value Description
    ""
    No parameter
    "?"
    Any type can be passed. In the body of the function, you'll need to check the type of the passed parameter.
    "C"
    Character type parameter
    "D"
    Date type parameter
    "I"
    Integer type parameter
    "L"
    Logical type parameter
    "N"
    Numeric type parameter
    "R"
    Reference
    "T"
    DateTime type parameter
    "Y"
    Currency type parameter
    "O"
    Object type parameter

Include a type value for each parameter passed to the library. For example, if you create a function that accepts a character and a numeric parameter, substitute "CN" for parmType.

Note   To indicate that a parameter is optional, precede it with a period. Only trailing parameters can be omitted.

The following FoxInfo structure defines a library with one function — internally called dates and externally accessed as DATES — that accepts one Character type parameter:

FoxInfo myFoxInfo[] = {
   { "DATES", (FPFI) dates, 1, "C" }
};

When you've compiled the library with this FoxInfo structure and loaded it in Visual FoxPro with the SET LIBRARY TO command, you can call this function in Visual FoxPro with the following line of code:

=DATES("01/01/95")

FoxTable Structure

The FoxTable structure is a linked list that keeps track of all the FoxInfo structures you have for a given library:

FoxTable _FoxTable = {nextLibrary, infoCount,infoPtr};

where the placeholders are defined as follows:

  • nextLibrary
    A pointer used internally by Visual FoxPro; should be initialized to 0.
  • infoCount
    The number of Visual FoxPro external routines defined in this library.
  • infoPtr
    The address of the first element of an array of FoxInfo structures. This name must match the array name listed in the FoxInfo statement.

The following is an example of a FoxTable statement. If your FoxInfo array name is myFoxInfo, you'll never need to change this statement:

FoxTable _FoxTable = {
   (FoxTable  *) 0,
   sizeof( myFoxInfo) / sizeof( FoxInfo ),
   myFoxInfo
};

Visual FoxPro captures General Protection Faults (GPFs) in ActiveX controls placed on a form, or COM objects instantiated from within Visual FoxPro. A GPF in an ActiveX control or COM object is now treated as a trappable Visual FoxPro error (Error 1440 - OLE object might be corrupt).

See Also

Creating a Basic ActiveX Object | API Library Construction | Adding Visual FoxPro API Calls | Accessing the Visual FoxPro API | Extending Visual FoxPro with External Libraries | Passing and Reception of Parameters | SET LIBRARY TO