The COM Course - Part 3
In this section, we're going to knock together our own ActiveX EXE.
Our component will essentially be a file monitorerer. Every sixty seconds or so, it will fire off and check for the existence of a particular file. If found, our component will attempt to raise an event to the calling application. If not, our component will attempt to raise the Spanish Armada from the shores of Cornwall.
Now, if we were to stick all our code into a regular ActiveX DLL project, our normal program code would have to pause and hang about until this file checking code had finished. Because an ActiveX EXE project has its own process space, this code runs of its own accord asynchronously without delaying our normal program code.
So let's get started:
- Create a new 'ActiveX EXE' project
- Name the Project: File
- Name your Class: FileCheck
First off, we're going to need to create something that checks for our file every minute or so. Here, we're just going to slot a Form with a Timer inside our ActiveX EXE (yes, you can have Forms inside your ActiveX EXE/DLL projects!). We won't be showing the Form, but every minute or so will use the Timer control on it to check for a specified file. If found, the Timer will 'tell' our class by raising an event. Let's get coding:
- Click 'Project', 'Add Form'
- Add a Timer to Form1
- Declare the following variable behind your Form:
Public Filename As String
This will hold the name of the file we wish to monitor.
- Declare the following event behind your Form:
Public Event FileFound()
This even will be raised by our Timer code if the above file is found.
- Insert the following code behind Timer1:
Private Sub Timer1_Timer() If Dir(Filename) <> "" Then RaiseEvent FileFound Timer1.Interval = 0 End IfEnd Sub
Here, our code simply checks for the file. If found, it raises the FileFound event, then sets the Timer1 Interval property to zero, stopping all future timer checks.
- Open your FileCheck class
- Declare the following object in General Declarations:
Dim WithEvents objFileCheck As Form1
This is your Form1. You're telling Visual Basic this is a space to hold your Form. The WithEvents keyword means your class can receive the events it transmits, such as our coded FileFound.
- Select 'Class' from the 'Object' drop-down list
- Choose 'Initialize' from the 'Procedure' drop-down list
- Slap the following code behind the Class_Initialize event:
Private Sub Class_Initialize() Set objFileCheck = New Form1End Sub
This code simply sets our objFileCheck to equal a new instance of Form1. Following this, we can use all the functionality we programmed into Form1. Next up, let's write a Sub that the program using our application will use to monitor a file.
- Throw the following code into your FileCheck class:
Public Sub MonitorFile(Filename As String) objFileCheck.Filename = Filename objFileCheck.Timer1.Interval = 60000End Sub
When our user calls this, passing a filename, the Filename variable of our Form is set to the passed filename. Then, the Timer is 'started' by setting the Interval property to 60,000 milliseconds (or a minute to the rest of us).
So, we've created all the jazz to monitor our file. But when it is actually 'found', we need to tell the program using our ActiveX EXE by raising the FileFound event.
- Add the following event declaration to the General Declarations section:
Public Event FileFound(Filename As String)
This code simply 'defines' our FileFound event. Next, we'll add code to fire off this event when appropriate.
- Select 'objFileCheck' from the 'Object' drop-down list
- Choose 'FileFound' from the 'Procedure' drop-down list
You're currently looking in the event our Form will raise when it 'finds' the file. This is where we need to raise our own FileFound event to the program using our EXE.
- Add the following code to the objFileCheck_FileFound event:
And that's our class complete!
When a programmer uses our class, he (or rather more unusually, she) can run the MonitorFile method, passing a filename. That starts off the Form Timer. When the Timer code finds the file, checking every sixty seconds, it raises an event in our FileCheck class. This raises another event to the program using our program, telling the programmer their file has been found.
Clear as mud? Let's test it!
Page 4 of 9