Click here to Skip to main content
15,891,136 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 141.9K   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 "cfhandler.h"
#include "pidl.h"
#include "util.h"


namespace MSF
{

// Note: handler expect that pidls are alive as long as handler is used.

class CCfShellIdListHandler : public CCfHandler
{
public:
	typedef std::vector<CPidl> CPidls;

	CCfShellIdListHandler(const ITEMIDLIST* pidlFolder, CPidls& pidls) :
		CCfHandler(CFSTR_SHELLIDLIST, true, false),
		m_pidlFolder(pidlFolder),
		m_pidls(pidls)
	{
	}


	virtual HRESULT Validate(const FORMATETC& formatetc) const
	{
		if (formatetc.dwAspect != DVASPECT_CONTENT)
			return DV_E_DVASPECT;

		if (!IsBitSet(formatetc.tymed, TYMED_HGLOBAL))
			return DV_E_TYMED;

		if (formatetc.lindex != -1)
			return DV_E_LINDEX;

		return S_OK;
	}


	virtual void GetData(const FORMATETC&, STGMEDIUM& stgmedium) const
	{
		UINT sizeheader = static_cast<UINT>(sizeof(CIDA) + (sizeof(UINT) * m_pidls.size()));

		UINT size = sizeheader + m_pidlFolder.GetSize();

		for (CPidls::const_iterator it = m_pidls.begin(); it != m_pidls.end(); ++it)
		{
			size += it->GetSize();
		}

		CStgMedium medium(GlobalAllocThrow(size));

		CIDA* pcida = static_cast<CIDA*>(medium.GetHGlobal());

		pcida->cidl = static_cast<UINT>(m_pidls.size());

		UINT offset = sizeheader;
		offset += AddPidlToCida(pcida, m_pidlFolder, 0, offset);

		for (UINT i = 0; i < m_pidls.size(); ++i)
		{
			offset += AddPidlToCida(pcida, m_pidls[i], i + 1, offset);
		}

		medium.Detach(stgmedium);
	}

private:

	CCfShellIdListHandler& operator=(const CCfShellIdListHandler&); // not implemented by design.

	unsigned int AddPidlToCida(CIDA* pcida, const CPidl& pidl, UINT index, UINT offset) const throw()
	{
		pcida->aoffset[index] = offset;

		unsigned int size = pidl.GetSize();
		memcpy(reinterpret_cast<BYTE*>(pcida) + offset, pidl.get(), size);
		return size;
	}

	CPidl   m_pidlFolder;
	CPidls& m_pidls;
};

} // end of MSF namespace

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