Tuesday, May 21, 2019

How to create a DLL in VFP



Please note that Foxpro DLLs DO NOT support interfaces of any kind (Including wait windows). If you require interface support, create a COM Server instead.

1. Step-by-step example of creating a DLL COM server.

  1. Create a project (Not with the wizard)

  2. Add a library in the project. (Call it myclass)

  3. So you create a custom class (call it functions)

  4. Add a method to the custom class (call it returnvalue)
    In it put something like:
    LPARAMETERS val1,val2
    val3 =val1+val2
    RETURN val3


  5. Save that. Load the class library in the class browser, select the class (functions) right-mouse on it and select OLEPublic.

  6. Make sure your library is set to main.

  7. Compile the project, by selecting Multi Threaded COM server DLL.

  8. You will note that VFP self-registers the DLL on the development box, but it needs to be registered on any other machine.

  9. To call your DLL from VFP for example use somethign like:
    oFunc = CREATEOBJECT("proj1.functions")  &&Proj1 was the name of my project (and the DLL)
    myValue=oFunc.returnvalue(1,2)
    ? myValue


  10. The value of myValue should give you 3.

2. How to create a DLL COM server from a program

Note: Create a program, copy the code below and call it "myclass.prg".

IF PROGRAM() != "MYCLASS"
   ?"this file MUST BE NAMED 'myclass.prg'"
   return
ENDIF
IF FILE("myclass.dll")
   DECLARE integer DllUnregisterServer IN myclass.dll
   DllUnregisterServer()
   CLEAR DLLS
ENDIF
BUILD PROJECT myserver FROM myclass
BUILD DLL myserver from myserver recomp
*now test this COM server:
ox = CreateObject("myserver.myclass")    && create the server object
ox.mydocmd("USE home(1)+'samples\data\customer'")    && use a table
?ox.myeval("RECCOUNT()")    && get the record count

DEFINE CLASS myclass AS session OLEPUBLIC
   PROCEDURE MyDoCmd(cCmd as String) as Variant ;
         helpstring "Execute a VFP cmd"
      &cCmd    && just execute parm as if it were a fox command
   FUNCTION MyEval(cExpr as String) ;
         helpstring "Evaluate a VFP expression"
      RETURN &cExpr    && evaluate parm as if it were a fox expr
   FUNCTION Error(nError, cMethod, nLine)
      COMreturnerror(cMethod+'  err#='+str(nError,5)+;
         '  line='+str(nline,6)+' '+message(),_VFP.ServerName)
      && this line is never executed
ENDDEFINE


by Mike Gagnon 

No comments:

Post a Comment