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

 Building a Simple COM Component with Visual C++ This article makes no attempt to explain the benefit of COM objects or how to build COM objects in other high-level languages such as VisualBasic and Java. There are a plethora of articles on 4Guys that describe where and why to use COM objects and how to create COM objects using VisualBasic and Java. Some of the more popular articles on these topics include: Writing a COM Object with VisualBasic 6.0 Building COM Objects with Java Using Business Objects (COM Components) In Your ASP Code Furthermore, this article makes no attempt to teach the C++ syntax and assumes the reader is already familiar with the C++ language. For this article I will be using Microsoft Visual C++ 6.0. Now that we've got all of the disclaimers out of the way, let's start building our COM component! For this article we will be creating a very simple (but useful) COM component. This COM component will calculate a binomial coefficient, sometimes referred to as a combinatorial. A combinatorial, `C(n,k)`, indicates how many ways `n` items can be arranged into groups of size `k`. For example, if we have four objects, `a`, `b`, `c`, and `d`, these can be arranged into four groups of size 3: `a b c`, `a b d`, `a c d`, and `b c d`. Note that order is not important; that is, `a b d` and `d b a` are considered equal. In the above example, we showed that `C(4,3) = 4`. This would be pronounced "4 choose 3 equals 4," meaning that out of a pool of four objects, there are four ways to create groups of size three. Our COM component will be used to calculate `n` choose `k`. It will consist of a single method, `Comb`, that expects two parameters: `n` and `k`. The method returns the value of `n` choose `k`. The `Comb` method will use the formula for binomial coefficients, which is: `C(n,k) = n! / (k! * (n-k)!)` The exclamation point is a short-hand way of denoting a factorial. Factorials are large numbers, computed by the formula: ``` n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1 ``` Therefore `5! = 5 * 4 * 3 * 2 * 1`, or 120. Now, you may be wondering why the hell anyone would be interested in these types of things. Believe it or not, but binomial coefficients have many practical uses in mathematics, especially in probability. Therefore, we can use combinatorials to determine gambling odds! For a look at how to calculate gambling odds with binomial coefficients check out the extended readings for this article. Building the Component in Visual C++ If you've built a COM component in VisualBasic before, you may think that COM objects are quite simple and can be built in a relatively short amount of time. This is a misconception - COM is a very intricate technology that is anything but "simple." VisualBasic, kindly, hides the messy details, making it appear very straightforward and simple. COM components can be built from the ground up just using C or C++, but that is incredibly involved and difficult. To aid with developing COM components in Visual C++ Microsoft has provided ATL (Active Template Library). ATL allows the program to contain macro-like language which is converted into the more complex-COM code at compile time. Furthermore, ATL Wizards exist to make the process of creating a COM component that much easier. In this article we'll use those Wizard to greatly simplify development. Since building a COM component is much more involved using Visual C++/ATL than using a language like VisualBasic or Java, one may wonder why anyone would wish to create a COM component using Visual C++. The decision to use Visual C++ is usually a performance-based one. Apps built using Visual C++ still boasts better performance than those built using VisualBasic; also, with Visual C++ you can perform lower-level functions, such as working with pointers, and make use of the standard template library (STL); finally, C++ is cool and is a very neat programming language. Of course, if you are wanting to prototype a COM component or are not overly concerned with performance, use VisualBasic or Java due to the time you'll be able to save during component development. That being said, let's start building our COM component! Begin by starting up Visual C++ and selecting to create a New Project. You should be presented with the following dialog box on the right. Select the create an ATL COM AppWizard Project and give it the Project name `Math`. Once you have selected this you will be taken to the ATL COM AppWizard. This Wizard, which we'll discuss in detail in Part 2, will write the vast majority of the code needed to build our COM component! Read Part 2

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