Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a DLL in VFP

Classes and Objects

How to create a DLL in VFP

by  Mike Gagnon  Posted    (Edited  )
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.

[ol][li]Create a project (Not with the wizard)[/li]
[li]Add a library in the project. (Call it myclass)[/li]
[li]So you create a custom class (call it functions)[/li]
[li]Add a method to the custom class (call it returnvalue)
In it put something like:
LPARAMETERS val1,val2
val3 =val1+val2
RETURN val3[/li]

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

[li]Make sure your library is set to main.[/li]
[li]Compile the project, by selecting Multi Threaded COM server DLL.[/li]
[li]You will note that VFP self-registers the DLL on the development box, but it needs to be registered on any other machine.[/li]
[li]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[/li]

The value of myValue should give you 3.[/ol]

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

Note: Create a program, copy the code below and call it "myclass.prg".
Code:
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

Mike Gagnon
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top