Creating a Super Simple COM Object

Every now and then I teach a beginning API test automation class. As part of that class I like to walk students through the process of creating a super simple COM object to test. The idea is to show students some of the magic behind the scenes which I believe will help them better understand the test automation. Here are the steps for creating a super simple COM object named MathCOMLib housed in file MathCOMLib.dll which contains an Interface named IMathMethods which is exposed as a class MathClass which contains a single method Sum(int x, int y) that returns the sum of integers x and y. It’s surprisingly tricky in the sense that one bad mouse click means disaster.

Launch an instance of Visual Studio being sure to be using “Run as administrator”. I’m using VS 2010 but most recent versions of VS work the same to create a COM component. Click File | New | Project. Select the C++ | ATL project template, name it MathCOMLib (the name of the project will become the name of the resulting DLL), specify any convenient Location, clear the “Create directory for solution” and “Add to source control” check boxes, and click OK.

In the Solution Explorer window, right click on the bolded MathCOMLib project (be careful to get the project, not the solution entry), and select Add | Class from the context menu. Select the ATL Simple Object item and click Add.

In the ATL Simple Object Wizard, enter MathClass into the Short Name field. In VS 2008 and earlier, all the remaining fields (including ProgID) will be filled in automatically. But, immensely annoyingly, with VS 2010 you need to manually type in the ProgID field: MathCOMLib.MathClass (the name of the Project + “.” + the name of the Class). The ProgID field is used if you want to allow your COM object to be callable from a scripting language like JavaScript. Click Next. Leave all the File Type Handler Options blank, and click Next. Leave the Options default selections alone, and click Finish.

Now in the Solution Explorer window, select the Class View tab. You should see an interface entry named IMathClass. Right click on that interface (not the class as you might expect) and select Add | Add Method. In the Add Method Wizard, enter Sum for the Method Name. Select the “in” Parameter Attribute, select LONG for the Parameter Type, enter x for the Parameter Name, and then click the Add button. Repeat for y (“in”, “LONG”, “y”, Add). Now, select the *LONG Parameter Type which enables the retval Parameter Attribute, and check retval, and enter pRes (which stands for pointer to result) as the Parameter Name, and click Add. Click Next.

Leave the IDL Attribute alone, and click Finish. Now go back to the main Solution Explorer window. You should see a MathClass.cpp entry and then double click on it. You will be transferred to auto-generated code that looks like:



// TODO: Add your implementation code here

return S_OK;


Replace the comment line with:

*pRes = x + y;

Notice that return values are passed using a pointer because the regular return is reserved for an HRESULT. Now, on a 32-bit machine you can just Build the library COM object DLL by clicking Build | Build Solution. But on a 64-bit machine you need to set the Build Configuration to 64-bit by Build | Configuration Manager -> Active Solution Platform -> x64. The Build process also registers the COM object on your current machine so you don’t have to do it by using the regsvr32.exe or regsvr64.exe utility. At this point you can use Windows Explorer and verify that you just created a file named MathCOMLib.dll at wherever you told VS to save it, in the .\MathCOMLib\Debug subdirectory (along with a bazillion other files).

Now you can call your Sum method to test it in several ways. The easiest is from a C# console application. After creating the application you would first Add Reference to the MathCOMLib.dll file. Then calling code would look like this:

MathLib.MathClass mc = new MathLib.MathClass();

int x = 2;

int y = 3;

int sum = mc.Sum(x, y);


From JavaScript you could call the method like this:

var mc = new ActiveXObject(“MathCOMLib.MathClass”);

var x = 2;

var y = 3;

var sum = mc.Sum(x, y);


And of course you could call the Sum method by using C++ if you’re a real glutton for punishment.

This entry was posted in Software Test Automation. Bookmark the permalink.