Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / ATL

The Mini Shell Extension Framework – Part III

Rate me:
Please Sign up or sign in to vote.
4.96/5 (11 votes)
18 Sep 200516 min read 139.8K   1.4K   46  
Discussion of a small C++ framework to create Windows shell extensions (IShellFolderImpl).
//
// (C) Copyright by Victor Derks <vba64@xs4all.nl>
//
// See README.TXT for the details of the software licence.
//
#pragma once


#include <shlobj.h>
#include "shlobjhidden.h"


namespace MSF
{

struct ShellItemIdMember
{
	size_t      size;
	const void* pdata;
};


class CShellItemIdMember : public ShellItemIdMember
{
public:
	CShellItemIdMember(const bool* pbValue)
	{
		size = sizeof(*pbValue);
		pdata = pbValue;
	}


	CShellItemIdMember(const unsigned int* pnValue)
	{
		size = sizeof(*pnValue);
		pdata = pnValue;
	}


	CShellItemIdMember(const wchar_t* wszValue)
	{
		size = (wcslen(wszValue) + 1) * sizeof(wchar_t);
		pdata = wszValue;
	}
};


class CShellItemId
{
public:
	CShellItemId(const ShellItemIdMember* members, unsigned int count, bool bAddEmptyId = true) throw()
	{
		size_t size = sizeof(short) + GetMembersSize(members, count);

		_pItemId = static_cast<SHITEMID*>(SHAlloc(size + (bAddEmptyId ? sizeof(short) : 0)));
		if (_pItemId == NULL)
			return;

		SetSize(size);

		// Copy members into allocated SHITEMID.
		BYTE* p = _pItemId->abID;
		for (unsigned int i = 0; i < count; ++i)
		{
			memcpy(p, members[i].pdata, members[i].size);
			p += members[i].size;
		}

		// Add optional terminating SHITEMID to make it easy to convert to a ITEMIDLIST.
		if (bAddEmptyId)
		{
			SHITEMID* pItemIdTerminating = reinterpret_cast<SHITEMID*>(p);
			pItemIdTerminating->cb = 0;
		}
	}


	~CShellItemId() throw()
	{
		SHFree(_pItemId);
	}


	SHITEMID* Detach() throw()
	{
		SHITEMID* pItemId = _pItemId;
		_pItemId = NULL;
		return pItemId;
	}

private:

	void SetSize(size_t size) throw()
	{
		ATLASSERT(size <= USHRT_MAX && "size will be sliced!");
		_pItemId->cb = static_cast<USHORT>(size);
	}


	static size_t GetMembersSize(const ShellItemIdMember* members, unsigned int count) throw()
	{
		size_t size = 0;

		for (unsigned int i = 0; i < count; ++i)
		{
			size += members[i].size;
		}

		return size;
	}


	SHITEMID* _pItemId;
};


class CShellItemIterator
{
public:
	CShellItemIterator(const SHITEMID& itemid) throw() : _p(itemid.abID)
	{
#ifdef _DEBUG
		_pitemid = &itemid;
#endif
	}

	~CShellItemIterator() throw()
	{
#ifdef _DEBUG
		size_t s = static_cast<size_t>(_p - _pitemid->abID);
		ATLASSERT(s == (_pitemid->cb - sizeof(unsigned short)) && "not all items were retrieved!");
#endif
	}


	bool GetBool() throw()
	{
		bool b = *reinterpret_cast<const bool *>(_p);
		_p += sizeof(b);
		return b;
	}


	unsigned int GetUnsignedInt() throw()
	{
		unsigned int n = *reinterpret_cast<const unsigned int *>(_p);
		_p += sizeof(n);
		return n;
	}


	CString GetString()
	{
		// Note: strings are always stored in Unicode to prevent ANSI/Unicode mismatches.
		CStringW strW(reinterpret_cast<const wchar_t*>(_p));
		_p += (strW.GetLength() + 1) * sizeof(wchar_t);

		CString str = CW2T(strW);
		return str;
	}

private:
	const BYTE* _p;

#ifdef _DEBUG
	const SHITEMID* _pitemid;
#endif
};

} // namespace MSF

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Hitachi High-Tech Analytical Science
Netherlands Netherlands
Victor lives in Nijmegen, the oldest city in The Netherlands.
He studied Applied Physics in Delft and works Hitachi High-Tech Analytical Science.

Comments and Discussions