Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C++

COM in plain C, Part 5

Rate me:
Please Sign up or sign in to vote.
4.94/5 (35 votes)
21 May 2006CPOL18 min read 120.5K   2.3K   101  
Add a connectable object (source/sink).
// This file contains functions for managing a list of "Port names".

#include <windows.h>
#include <objbase.h>
#include "IExample5.h"
#include "IEnumVariant.h"

// The IENUMLIST for our "Port names"
static IENUMLIST	PortsList;

// This is just a helper function to allocate/initialize an IDispatch
// (really, a MyRealICollection) object for our PortsList.
IDispatch * allocPortsCollection(void)
{
	// Allocate/return a collection object (actually, a MyRealICollection) wrapping PortsList
	return(allocICollection(&PortsList));
}

// This is just a helper function to free up our PortsList. Called when our DLL unloads.
void freePortsCollection(void)
{
	// Free all IENUMITEMs in PortsList
	freeEnumList(&PortsList);
}

// This is just a helper function to initialize our Ports list.
// Called when our DLL first loads.
HRESULT initPortsCollection(void)
{
	VARIANT		value;
	IENUMITEM	*item;

	PortsList.head = 0;

	// Initially set the count to 1 so this list is kept until
	// we finally call freePortsCollection
	PortsList.count = 1;

	// Create a list with 3 IENUMITEMs with BSTR values of
	// "Port 1", "Port 2", and "Port 3"
	value.vt = VT_BSTR;
	if ((value.bstrVal = SysAllocString(L"Port 1")))
	{
		if ((PortsList.head = item = allocIENUMITEM(&value)))
		{
			if ((value.bstrVal = SysAllocString(L"Port 2")))
			{
				if ((item->next = allocIENUMITEM(&value)))
				{
					item = item->next;
					if ((value.bstrVal = SysAllocString(L"Port 3")))
					{
						if ((item->next = allocIENUMITEM(&value)))
						{
							item->next->next = 0;
							return(S_OK);
						}
					}
				}
			}
		}
	}

	freePortsCollection();
	return(E_FAIL);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions