Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / ATL

Shell Extensions for .NET Assemblies

Rate me:
Please Sign up or sign in to vote.
4.98/5 (29 votes)
17 Nov 2005Ms-PL3 min read 184.1K   2.6K   81  
Shell extensions to distinguish between .NET assemblies and Win32 applications and libraries.
// AssemblyColumns.cpp : Implementation of CAssemblyColumns

#include "stdafx.h"
#include "AssemblyColumns.h"
#include <strsafe.h>

// CAssemblyColumns

STDMETHODIMP CAssemblyColumns::GetColumnInfo(DWORD dwIndex, SHCOLUMNINFO* psci)
{
	if (dwIndex > 1)
		return S_FALSE;

	if (psci == NULL)
		return E_POINTER;

	psci->scid.fmtid = CLSID_AssemblyColumns;
	psci->scid.pid = dwIndex;
	psci->vt = VT_BSTR;
	psci->fmt = LVCFMT_LEFT;
	psci->csFlags = SHCOLSTATE_TYPE_STR | SHCOLSTATE_SLOW;

	switch (dwIndex)
	{
	case 0:
		psci->cChars = 20;
		StringCchCopyW(psci->wszTitle, MAX_COLUMN_NAME_LEN, CT2WEX<MAX_COLUMN_NAME_LEN>(m_strFormatTitle));
		StringCchCopyW(psci->wszDescription, MAX_COLUMN_DESC_LEN, CT2WEX<MAX_COLUMN_DESC_LEN>(m_strFormatDesc));
		break;

	case 1:
		psci->cChars = 20;
		StringCchCopyW(psci->wszTitle, MAX_COLUMN_NAME_LEN, CT2WEX<MAX_COLUMN_NAME_LEN>(m_strKeyTokenTitle));
		StringCchCopyW(psci->wszDescription, MAX_COLUMN_DESC_LEN, CT2WEX<MAX_COLUMN_DESC_LEN>(m_strKeyTokenDesc));
		break;

	default:
		return S_FALSE;
	}

	return S_OK;
}

STDMETHODIMP CAssemblyColumns::GetItemData(LPCSHCOLUMNID pscid, LPCSHCOLUMNDATA pscd, VARIANT* pvarData)
{
	BSTR bstrVal = NULL;
	FileType ft;
	HRESULT hr;

	if (!pscid || !pscd || !pvarData)
		return E_POINTER;

	if (CLSID_AssemblyColumns != pscid->fmtid)
		return S_FALSE;

	switch (pscid->pid)
	{
	case 0:
		// Get the file type.
		hr = m_spAsmInfo->GetFileType((BSTR)pscd->wszFile, &ft);
		if (FAILED(hr))
			return S_FALSE;

		// Return the appropriate format.
		switch (ft)
		{
		case Win32Executable:
			m_bstrWin32Executable.CopyTo(&bstrVal);
			break;

		case Win32Library:
			m_bstrWin32Library.CopyTo(&bstrVal);
			break;

		case DotNETExecutable:
			m_bstrNETExecutable.CopyTo(&bstrVal);
			break;

		case DotNETLibrary:
			m_bstrNETLibrary.CopyTo(&bstrVal);
			break;

		case DotNETModule:
			m_bstrNETModule.CopyTo(&bstrVal);
			break;

		case Win64ExecutableIA64:
			m_bstrWin32ExecutableIA64.CopyTo(&bstrVal);
			break;

		case Win64LibraryIA64:
			m_bstrWin32LibraryIA64.CopyTo(&bstrVal);
			break;

		case DotNETExecutableIA64:
			m_bstrNETExecutableIA64.CopyTo(&bstrVal);
			break;

		case DotNETLibraryIA64:
			m_bstrNETLibraryIA64.CopyTo(&bstrVal);
			break;

		case DotNETModuleIA64:
			m_bstrNETModuleIA64.CopyTo(&bstrVal);
			break;

		case Win64ExecutableX64:
			m_bstrWin32ExecutableX64.CopyTo(&bstrVal);
			break;

		case Win64LibraryX64:
			m_bstrWin32LibraryX64.CopyTo(&bstrVal);
			break;

		case DotNETExecutableX64:
			m_bstrNETExecutableX64.CopyTo(&bstrVal);
			break;

		case DotNETLibraryX64:
			m_bstrNETLibraryX64.CopyTo(&bstrVal);
			break;

		case DotNETModuleX64:
			m_bstrNETModuleX64.CopyTo(&bstrVal);
			break;

		default:
			return S_FALSE;
		}

		break;

	case 1:
		// Get the public key token. Return for anything other than S_OK
		// since S_FALSE indicates that the file was not an assembly.
		hr = m_spAsmInfo->GetPublicKeyToken((BSTR)pscd->wszFile, &bstrVal);
		if (S_OK != hr || NULL == bstrVal) return S_FALSE;

		break;

	default:
		return S_FALSE;
	}
	
	pvarData->vt = VT_BSTR;
	pvarData->bstrVal = bstrVal;
	return S_OK;
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer Microsoft
United States United States
Principal Software Engineer currently working on Azure SDKs at Microsoft. My opinions are my own. I work on a number of OSS projects for work and personally in numerous languages including C++, C#, JavaScript, Go, Rust, et. al. See a problem, fix a problem (or at least create an issue)!

Avid outdoor adventurer 🏔️❄️👞🚴‍♂️, husband, father.

Comments and Discussions