Click here to Skip to main content
15,896,111 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 185.6K   2.6K   81  
Shell extensions to distinguish between .NET assemblies and Win32 applications and libraries.
// Modules.h: Module class wrappers for dynamic runtime linking.

#pragma once

#include <windows.h>

struct ENTRYPOINT
{
	BOOL fLoaded;
	BOOL fFound;
	LPVOID pfn;
} EMPTY_ENTRYPOINT = { FALSE, FALSE, NULL };
typedef ENTRYPOINT *LPENTRYPOINT;

class CModule
{
protected:

	CModule(LPCTSTR pszModuleFileName) throw()
	{
		m_hModule = LoadLibrary(pszModuleFileName);
	}

	~CModule() throw()
	{
		if (m_hModule)
			FreeLibrary(m_hModule);
	}

	template<typename T>
	T GetProc(LPCSTR pszProcName, LPENTRYPOINT proc)
	{
		// If not already loaded, get the proc address.
		if (m_hModule && !proc->fLoaded)
		{
			proc->pfn = GetProcAddress(m_hModule, pszProcName);
			if (proc->pfn)
				proc->fFound = TRUE;

			proc->fLoaded = TRUE;
		}
		else if (!m_hModule)
		{
			// mscoree.dll was not found.
			throw ERROR_FILE_NOT_FOUND;
		}
		
		if (!proc->fFound)
		{
			// The function isn't defined/exported.
			throw ERROR_INVALID_FUNCTION;
		}

		return static_cast<T>(proc->pfn);
	}

private:

	HMODULE m_hModule;
};

class CMscoreeModule :
	public CModule
{
public:

	CMscoreeModule() throw() :
		m_fnStrongNameFreeBuffer(EMPTY_ENTRYPOINT),
		m_fnStrongNameTokenFromAssembly(EMPTY_ENTRYPOINT),
		m_fnStrongNameTokenFromAssemblyEx(EMPTY_ENTRYPOINT),
		CModule(TEXT("mscoree.dll"))
	{
	}

	VOID StrongNameFreeBuffer(LPBYTE pbMemory)
	{
		STRONGNAMEFREEBUFFER pfn = GetProc<STRONGNAMEFREEBUFFER>("StrongNameFreeBuffer", &m_fnStrongNameFreeBuffer);
		return (pfn)(pbMemory);
	}

	BOOLEAN StrongNameTokenFromAssembly(LPCWSTR wszFilePath, LPBYTE* ppbStrongNameToken, ULONG* pcbStrongNameToken)
	{
		STRONGNAMETOKENFROMASSEMBLY pfn = GetProc<STRONGNAMETOKENFROMASSEMBLY>("StrongNameTokenFromAssembly", &m_fnStrongNameTokenFromAssembly);
		return (pfn)(wszFilePath, ppbStrongNameToken, pcbStrongNameToken);
	}

	BOOLEAN StrongNameTokenFromAssemblyEx(LPCWSTR wszFilePath, LPBYTE* ppbStrongNameToken, ULONG* pcbStrongNameToken,
		LPBYTE* ppbPublicKeyBlob, ULONG* pcbPublicKeyBlob)
	{
		STRONGNAMETOKENFROMASSEMBLYEX pfn = GetProc<STRONGNAMETOKENFROMASSEMBLYEX>("StrongNameTokenFromAssemblyEx", &m_fnStrongNameTokenFromAssemblyEx);
		return (pfn)(wszFilePath, ppbStrongNameToken, pcbStrongNameToken, ppbPublicKeyBlob, pcbPublicKeyBlob);
	}

private:

	typedef VOID (WINAPI *STRONGNAMEFREEBUFFER)(LPBYTE);
	typedef BOOLEAN (WINAPI *STRONGNAMETOKENFROMASSEMBLY)(LPCWSTR, LPBYTE*, ULONG*);
	typedef BOOLEAN (WINAPI *STRONGNAMETOKENFROMASSEMBLYEX)(LPCWSTR, LPBYTE*, ULONG*, LPBYTE*, ULONG*);

	ENTRYPOINT m_fnStrongNameFreeBuffer;
	ENTRYPOINT m_fnStrongNameTokenFromAssembly;
	ENTRYPOINT m_fnStrongNameTokenFromAssemblyEx;

};
CMscoreeModule g_mscoree;

//class CKernel32Module :
//	public CModule
//{
//public:
//
//	CKernel32Module() :
//	  m_fnGetUserDefaultUILanguage(EMPTY_ENTRYPOINT),
//	  CModule(TEXT("kernel32.dll"))
//	{
//	}
//
//	LANGID GetUserDefaultUILanguage(void)
//	{
//		GETUSERDEFAULTUILANGUAGE pfn = GetProc<GETUSERDEFAULTUILANGUAGE>("GetUserDefaultUILanguage", &m_fnGetUserDefaultUILanguage);
//		return (pfn)();
//	}
//
//private:
//
//	typedef LANGID (WINAPI *GETUSERDEFAULTUILANGUAGE)(void);
//
//	ENTRYPOINT m_fnGetUserDefaultUILanguage;
//};
//CKernel32Module g_kernel32;

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