Wiki Home

COMC omponent Example


Namespace: WIN_COM_API
This is an example of creating, invoking and instancing a COM component. The example outlines a simple data access class that allows navigation to the top, bottom, next and previous record. It should take you about 10 minutes to do this. All you have to do is copy-and-paste the program and the commands.


Step 1: Create a class definition with OLEPUBLIC

create project ComExamp

Copy-and-paste this class definition into 'Program1.PRG':

DEFINE CLASS cPerson AS Custom OLEPUBLIC

  FirstName = SPACE(10)
  LastName  = SPACE(10)
  cVersion  = "1.2"

  PROCEDURE Init
  USE Person
  This.RefreshData()
  ENDPROC

  PROCEDURE Destroy
  USE IN Person
  ENDPROC

  PROCEDURE RefreshData
  This.FirstName = Person.FirstName
  This.LastName = Person.LastName
  ENDPROC

  PROCEDURE GoNext
  SELECT Person
  SKIP 1
  This.RefreshData()
  ENDPROC

  PROCEDURE GoPrev
  SKIP -1
  This.RefreshData()
  ENDPROC

  FUNCTION FindInName( tcSubName )
  LOCATE FOR UPPER ( tcSubName ) $ UPPER( FirstName + LastName )
  This.RefreshData()
  RETURN FOUND()
  ENDPROC

  FUNCTION FullName
  RETURN ALLTRIM( This.FirstName - ( " " + This.LastName ) )
  ENDPROC

ENDDEFINE


Step 2: Create a table and add some data

Copy-and-paste this code into new .PRG, and run it (Ctrl+E).

CREATE TABLE Person ( FirstName c(20), LastName c(20) )
INSERT INTO Person VALUES ( "George", "Harrison" )
INSERT INTO Person VALUES ( "John", "Lennon" )
INSERT INTO Person VALUES ( "Paul", "McCartney" )
INSERT INTO Person VALUES ( "Ringo", "Starr" )


Step 3: Build the project into a DLL

Compile the project into either a single-threaded or multi-threaded COM server DLL, filename: comdata.dll.

(paste this into the command window - make sure it runs - you have to hit enter, and the command has to still be in the command window.)
build dll comdata from ComExamp

Step 4: Register the DLL

In DOS (or from Windows, click on Start and then Run and then), do the following (you may have to provide the path to Regsvr32 and the DLL):
C:\WINDOWS\SYSTEM\REGSVR32 comdata.dll

(you don't need to do this unless you are moving the dll to another machine. VFP registers it for you when you build it.)

Step 5: Invoke and use the COM component

In VFP:

* create the object
oPerson = CREATEOBJECT("comdata.cPerson")

* display the current person
?oPerson.firstname
?oPerson.lastname

* move to the next person and display
oPerson.goNext
?oPerson.firstname
?oPerson.lastname

* release the object from memory
RELEASE oPerson


In Excel:

Tools, Macros, VB Editor
View, Code, Insert the code below,
Alt+Tab back to Excel, Tools, Macros, Macros, Run.

Public oPerson As Object
Sub Beatles()
    Set oPerson = CreateObject("comdata.cperson")
    ActiveSheet.Cells(1, 1).Value = oPerson.FirstName
    ActiveSheet.Cells(1, 2).Value = oPerson.LastName
    oPerson.goNext
    ActiveSheet.Cells(2, 1).Value = oPerson.FirstName
    ActiveSheet.Cells(2, 2).Value = oPerson.LastName
    oPerson.FindInName ("mccar")
    ActiveSheet.Cells(3, 1).Value = oPerson.FullName()
    SET oPerson = Nothing
End Sub


To make this go smoother, read and implement the error handler at Com Error Handling. It was left from here to keep it short.

Once you get this working, see DcomExample (coming soon as soon as I figure out what is needed, got it to work once, then couldn't get it to work again on another machine).
I refactored the code a little bit. There was nothing wrong with it for the purposes of this example, but when working with COM you should take extra care to avoid any possible error. I created a new function called OpenData which insures that the person table is open and then added code to call it in all of the necessary methods. I also added code to the Init method to place VFP in unattended mode to prevent any modal dialogs from coming up if VFP was started as an automation server. -- Mike Feltman

I am unfactoring it back to something simple. If you want to see all of Mike's work, look here: _ Com Component Example 11 -- CFK grrrr ;)

I think someone said it is better to use the Session object for COM stuff. :)
DEFINE CLASS cPerson AS Session OLEPUBLIC

Gordon King

See COM, Com Discussion Exam 70-155 Study Group Exam 70-155 Study Guide
Contributors: Carl Karsten Mike Feltman
Category Exam 70-155 Hot Topic, Category C _ O _ M Category Code Samples
( Topic last updated: 2006.10.30 11:26:32 AM )