Click here to Skip to main content
11,567,067 members (47,861 online)
Click here to Skip to main content
Add your own
alternative version

Shell Extensions for .NET Assemblies

, 17 Nov 2005 Ms-PL 138.2K 2.4K 80
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)

Share

About the Author

Heath Stewart
Program Manager Microsoft
United States United States
Heath Stewart is a happily married software engineer originally from the Midwest and a graduate of Iowa State University. Heath start programming early in life and enjoys continuous research and development in new languages, frameworks, and platforms. Fluent in many different programming languages, he has developed many large-scale software solutions for companies in different areas, such as Internet filtering, intrusion detection systems, production management systems, and web applications for various purposes. He also enjoys photography.

Currently, Heath is a Program Manager in the Visual Studio Professional Deployment Experience (VSPro DEX) team at Microsoft. Previous to his employment, he was a Microsoft MVP for Visual C#.

He is also a CodeProject protector and is happy to help the development community.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 17 Nov 2005
Article Copyright 2002 by Heath Stewart
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid