Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

The Mini Shell Extension Framework – Part III

, 18 Sep 2005
Discussion of a small C++ framework to create Windows shell extensions (IShellFolderImpl).
msf091.zip
include
columnprovider.rgs
contextmenu.rgs
contextmenu_sf.rgs
extension.rgs
extractimage_sf.rgs
infotip.rgs
infotip_sf.rgs
propertysheetext.rgs
propertysheetext_sf.rgs
shell32missing.def
shell32missing.lib
shellfolder.rgs
shellfolderwin98.rgs
vvvsample
Copy of sample1.vvv
menuicon.bmp
sample.def
sample.manifest
sample1.vvv
vvv.ico
vvvtest
sample.yyy
sample1.vvv
sample2.vvv
//
// (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

Share

About the Author

Victor Derks
Software Developer (Senior) Schneider Electric
Netherlands Netherlands
Victor lives in Nijmegen, the oldest city in The Netherlands.
He studied Applied Physics in Delft and now works for GE Healthcare.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 18 Sep 2005
Article Copyright 2005 by Victor Derks
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid