In Part 2 we looked at how to use the ATL COM AppWizard to build the skeleton files needed for our COM component. Also, we examined how to add a Simple ATL Object to the project. In this part we will look at how to add methods to the Simple ATL Object's interface.
As we discussed in Part 1, our COM component will have one method
and no properties. This method, Comb, will take two integer inputs, n and
k, and return the value of C(n,k) (which is also an integer). Therefore we will
need to add one method to our interface IComb. To do this, right click on the interface
name, IComb, and select Add Method. You will be presented with the dialog box on the right.
Note that in the dialog box I entered Comb as the Method Name. The method expects two
in parameters, n and k, and will return an integer, NchooseK.
Note that to denote in parameters a [in] prefixes the variable name. This is similar to
a ByVal parameter in VisualBasic. Note that to have a method return a value you need to
specify it as a parameter in the function's parameter list, specifically denoted as [out, retval].
Furthermore, retval parameters must be pointers. Therefore, our Comb method
has the parameter list:
[in] int n, [in] int k, [out, retval] int * NchooseK
|
To add a property to a COM component, right click on the interface and select Add Property. Since this component doesn't require any properties, we won't be creating one in this article.
Once you have entered the Method Name and Parameters values in the Add Method to Interface dialog box,
click OK. This will create a new method in the interface IComb and will add a public member
function named Comb to the CComb class. Simply add the needed C++ code to this
function to give the method the functionality you desire. The code for the Comb function
can be seen below:
|
The code for the Comb function is pretty sweet, in my opinion. A full explanation of why
it works can be found in the extended readings. Once you enter this code
into the Comb function you are ready to compile your COM component. Go to the Build menu and
select Build Math.dll. Once the build process is completed, the component should be compiled, the DLL
created and registered! You can now use this COM component in your ASP pages! A quick example can be
seen below:
|
Happy Programming!
Related Articles:



