Using a VFP DLL in a dotNet system

If you want to migrate a Visual FoxPro system into dotNet then you will obviously have to rewrite everything in the new language, probably C# or Basic if you're sticking with Microsoft. If the FoxPro database is a large one then the rewrite will take a lot of time and effort and you will have to bring a completely new system into operation with a sudden-death changeover. Even with the best of testing this is still a risky business and one way of managing the risk is to convert some of your existing FoxPro system into a DLL that can be called from dotNet. This allows you to commission the new system in a number of stages. The first might be a database that is just a series of VB or C# forms which call VFP for data handling and business logic; subsequent stages would see the classes of the VFP DLL being replaced by native dotNet classes. The overall project might take a little longer but the user would be using the new forms for a lot of this time and the improved feedback should lead to a better final result.

Writing a VFP DLL

The functionality of the DLL must be wrapped into a class and this must be a custom class of type OLEPUBLIC. The example described below opens the Customer table and finds the name of the company for a given customer id.

The first step is to define the class and to give it a property that will hold the customer name. A real world class would of course have properties to match every field of the table:

Define Class Customer As Custom OlePublic
   *-- The company field is C(40) so declare a property to match.
   Company = Space(40)
Enddefine

The DLL can't have any user interface so we can't have messages telling us what Fox is doing. We can turn these off in the Init method of the class:

   Procedure Init
     *-- No user interface in a DLL so turn off messages.
     On Error
     Set Console Off
     Set Notify Off
     Set Safety Off
     Set Talk Off
     Set Notify Off
   Endproc

The DLL will have to open the customer table and set it into Cust_ID order so that we'll be able to find the customer later. The calling program will need to know whether the DLL has managed to open the table so we'll return the record count:

   Procedure OpenTable
     *-- Open the customer table.
     Select 0
     Use C:\Projects\Customer
     Set Order To Cust_ID

     *-- How many records are there in the table.
     Return Reccount()
   Endproc

The SetName method of the class will take the customer ID as a parameter and look for the matching record:

   Procedure SetName()
     *-- Set the name for this customer ID.
     Lparameter tcID
     Local llFound As Boolean
     If Seek(tcID)
       This.Company = Customer.Company
       llFound = .T.
     Else
       This.Company = Space(40)
       llFound = .F.
     Endif

     *-- Have we found anything.
     Return llFound
   Endproc

The next steps

Once you've got the basic class written the next stages are to test and build the DLL and then to call it from Visual Basic or C#.