Click here to Skip to main content
15,886,137 members
Articles / Web Development / HTML

.NET CLR Injection: Modify IL Code during Run-time

Rate me:
Please Sign up or sign in to vote.
4.98/5 (240 votes)
7 Aug 2014LGPL310 min read 594.2K   18.4K   352  
Modify methods' IL codes on runtime even if they have been JIT-compiled, supports release mode / x64 & x86, and variants of .NET versions, from 2.0 to 4.5.
#include "StdAfx.h"
#include "DataStructure.h"
#include "Utility.h"

DotNetVersion g_tDotNetVersion;
HMODULE g_hJitModule = NULL;
HMODULE g_hClrModule = NULL;

MethodDesc::PFN_Reset MethodDesc::s_pfnReset = NULL;
MethodDesc::PFN_IsGenericMethodDefinition MethodDesc::s_pfnIsGenericMethodDefinition = NULL;
MethodDesc::PFN_GetNumGenericMethodArgs MethodDesc::s_pfnGetNumGenericMethodArgs = NULL;
MethodDesc::PFN_StripMethodInstantiation MethodDesc::s_pfnStripMethodInstantiation = NULL;
MethodDesc::PFN_HasClassOrMethodInstantiation MethodDesc::s_pfnHasClassOrMethodInstantiation = NULL;
MethodDesc::PFN_ContainsGenericVariables MethodDesc::s_pfnContainsGenericVariables = NULL;
MethodDesc::PFN_GetWrappedMethodDesc MethodDesc::s_pfnGetWrappedMethodDesc = NULL;
MethodDesc::PFN_GetDomain MethodDesc::s_pfnGetDomain = NULL;
MethodDesc::PFN_GetLoaderModule MethodDesc::s_pfnGetLoaderModule = NULL;


LoadedMethodDescIterator::PFN_LoadedMethodDescIteratorConstructor LoadedMethodDescIterator::s_pfnConstructor = NULL;
LoadedMethodDescIterator::PFN_LoadedMethodDescIteratorConstructor_v45 LoadedMethodDescIterator::s_pfnConstructor_v45 = NULL;
LoadedMethodDescIterator::PFN_Start LoadedMethodDescIterator::s_pfnStart = NULL;
LoadedMethodDescIterator::PFN_Next_v4 LoadedMethodDescIterator::s_pfnNext_v4 = NULL;
LoadedMethodDescIterator::PFN_Next_v2 LoadedMethodDescIterator::s_pfnNext_v2 = NULL;
LoadedMethodDescIterator::PFN_Current LoadedMethodDescIterator::s_pfnCurrent = NULL;

// detect the version of CLR
BOOL DetermineDotNetVersion(void)
{
	WCHAR wszPath[MAX_PATH] = {0};
	::GetModuleFileNameW( g_hClrModule, wszPath, MAX_PATH);
	CStringW strPath(wszPath);
	int nIndex = strPath.ReverseFind('\\');
	if( nIndex <= 0 )
		return FALSE;
	nIndex++;
	CStringW strFilename = strPath.Mid( nIndex, strPath.GetLength() - nIndex);
	if( strFilename.CompareNoCase(L"mscorwks.dll") == 0 )
	{
		g_tDotNetVersion = DotNetVersion_20;
		return TRUE;
	}

	if( strFilename.CompareNoCase(L"clr.dll") == 0 )
	{
		VS_FIXEDFILEINFO tVerInfo = {0};
		if ( CUtility::GetFileVersion( wszPath, &tVerInfo) &&
			 tVerInfo.dwSignature == 0xfeef04bd)
		{
			int nMajor = HIWORD(tVerInfo.dwFileVersionMS);
			int nMinor = LOWORD(tVerInfo.dwFileVersionMS);
			int nBuildMajor = HIWORD(tVerInfo.dwFileVersionLS);
			int nBuildMinor = LOWORD(tVerInfo.dwFileVersionLS);

			if( nMajor == 4 && nMinor == 0 && nBuildMajor == 30319 )
			{
				if( nBuildMinor < 10000 )
					g_tDotNetVersion = DotNetVersion_40;
				else
					g_tDotNetVersion = DotNetVersion_45;
				return TRUE;
			}
		}
		return FALSE;
	}

	return FALSE;
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions