To read the article online, visit http://www.4GuysFromRolla.com/webtech/052600-1.3.shtml

Building a Simple COM Component with Visual C++, Part 3


  • Read Part 2
  • Read Part 1

  • 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.

    Add the Comb method to the IComb 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:

    STDMETHODIMP CComb::Comb(int n, int k, int *NchooseK)
    {
      float fAnswer = 1.0,
            fN = (float) n,
            fK = (float) k,
            fTemp = 1.0;
    
      if (k < 0)
        *NchooseK = 0;
      else {
        for(; fTemp <= fN - fK ;)
          fAnswer *= (fTemp + fK) / fTemp++;
    
        *NchooseK = (int) fAnswer;
      }
    
      return S_OK;
    }
    

    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:

    <% @LANGUAGE="VBScript" %>
    <% Option Explicit %>
    <%
      Dim objComb
      Set objComb = Server.CreateObject("Math.Comb")
    %>
      6 choose 4 = <%=objComb.Comb(6,4)%><BR>
      7 choose 3 = <%=objComb.Comb(7,3)%><BR>
      52 choose 5 = <%=objComb.Comb(52,5)%><BR>
      10 choose -4 = <%=objComb.Comb(10,-4)%>
    <%
      Set objComb = Nothing   'Clean up...
    %>
    

    Happy Programming!


    Related Articles:

  • Read the Extended Readings on Binomial Coefficients


  • Article Information
    Article Title: Building a Simple COM Component with Visual C++, Part 3
    Article Author: Scott Mitchell
    Published Date: Friday, May 26, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/052600-1.3.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers