1. Step-by-step example of creating a DLL COM server.
- Create a project (Not with the wizard)
- Add a library in the project. (Call it myclass)
- So you create a custom class (call it functions)
- Add a method to the custom class (call it returnvalue)
In it put something like:
LPARAMETERS val1,val2
val3 =val1+val2
RETURN val3 - Make sure your library is set to main.
- Compile the project, by selecting Multi Threaded COM server DLL.
- You will note that VFP self-registers the DLL on the development box, but it needs to be registered on any other machine.
- 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
Save that. Load the class library in the class browser, select the class (functions) right-mouse on it and select OLEPublic.
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
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