Afraid not. Here is the complete ModuleLoadFinished() method. The
ICorProfilerInfo is stored in the like-named member variable and
GetModuleName() and GetModuleBaseAddress() are just wrappers around the
ICorProfilerInfo->GetModuleInfo() method. Also note the filter at the top
based on the module name to ensure I don't start messing with mscorlib or
something (in case you try to run this).
*DISCLAIMER: This is a hack-test program, there is no synchronization,
proper error recovery or other important bits!*
STDMETHODIMP CProfiler::ModuleLoadFinished(ModuleID moduleId,
HRESULT hrStatus)
{
try
{
std::wstring moduleName = GetModuleName(moduleId);
std::wcerr << L"Module " << moduleName << L" has been loaded" <<
std::endl;
if (::wcsicmp(moduleName.c_str(),
L"C:\\work\\dotnet\\simple\\CrashTest\\bin\\Debug\\CrashTest.exe") == 0)
{
IMetaDataImport* pIMetaDataImport = NULL;
HRESULT hr = m_pICorProfilerInfo->GetModuleMetaData(moduleId,
ofRead,
IID_IMetaDataImport,
(IUnknown**)&pIMetaDataImport);
if (FAILED(hr))
{
std::wcerr << L"GetModuleMetaData returned 0x" << std::hex
<< hr << std::endl;
}
assert(pIMetaDataImport != NULL);
IMetaDataEmit* pIMetaDataEmit = NULL;
hr = m_pICorProfilerInfo->GetModuleMetaData(moduleId,
ofRead | ofWrite,
IID_IMetaDataEmit,
(IUnknown**)&pIMetaDataEmit);
if (FAILED(hr))
{
std::wcerr << L"GetModuleMetaData(2) returned 0x" <<
std::hex << hr << std::endl;
}
assert(pIMetaDataEmit != NULL);
HCORENUM hEnum = 0;
ULONG numTokens = 2048;
mdTypeDef* tokens = new mdTypeDef[numTokens];
hr = pIMetaDataImport->EnumTypeDefs(&hEnum,
tokens,
numTokens,
&numTokens);
if (FAILED(hr))
{
std::wcerr << L"EnumTypeDefs returned 0x" << std::hex << hr
<< std::endl;
}
pIMetaDataImport->CloseEnum( hEnum );
for(ULONG i=0; i<numTokens; i++)
{
//
// First let's generate a NOP IL Stream
//
BYTE data[2];
// Flags_CodeSize (Tiny w/ 1 byte of IL)
data[0] = CorILMethod_TinyFormat1 | (1 <<
(CorILMethod_FormatShift-1));
// The IL: NOP
data[1] = 0;
// Now allocate the memory for it
void* mem = NULL;
ULONG codeRVA = 0;
IMethodMalloc* mallocer = NULL;
hr =
m_pICorProfilerInfo->GetILFunctionBodyAllocator(moduleId,
&mallocer);
if (FAILED(hr))
{
std::wcerr << L"GetILFunctionBodyAllocator returned 0x"
<< std::hex << hr << std::endl;
}
else
{
mem = mallocer->Alloc(sizeof(data));
mallocer->Release();
const BYTE* baseAddress =
GetModuleBaseAddress(moduleId);
codeRVA = (ULONG)(((UINT_PTR)mem) -
((UINT_PTR)baseAddress));
::memcpy(mem, data, sizeof(data));
}
//
// Now the MethodDef
//
COR_SIGNATURE signature[8];
signature[0] = IMAGE_CEE_CS_CALLCONV_DEFAULT_HASTHIS |
IMAGE_CEE_CS_CALLCONV_DEFAULT;
signature[1] = 0; // No Parameters
signature[2] = ELEMENT_TYPE_VOID; // Retval
ULONG signatureSize = 3;
mdMethodDef methodToken = 0;
CorMethodAttr methodFlags = (CorMethodAttr)(mdFamily |
mdHideBySig | mdVirtual );
CorMethodImpl implFlags = (CorMethodImpl)(miIL | miManaged);
hr = pIMetaDataEmit->DefineMethod(tokens[i],
L"abcdef",
methodFlags,
signature,
signatureSize,
codeRVA,
implFlags,
&methodToken);
if (FAILED(hr))
{
std::wcerr << L"EnumTypeDefs returned 0x" << std::hex <<
hr << std::endl;
}
else
{
std::wcerr << L"Injected method into class @ 0x" <<
std::hex << codeRVA << std::endl;
}
//
// Now that we have the token, let's put the IL in place
//
if (mem != NULL)
{
hr = m_pICorProfilerInfo->SetILFunctionBody(moduleId,
methodToken, (BYTE*)mem);
if (FAILED(hr))
{
std::wcerr << L"SetILFunctionBody returned 0x" <<
std::hex << hr << std::endl;
}
}
}
//
// Cleanup
//
delete[] tokens;
if (pIMetaDataImport != NULL)
{
pIMetaDataImport->Release();
}
if (pIMetaDataEmit != NULL)
{
pIMetaDataEmit->Release();
}
}
}
catch(...)
{
std::wcerr << L"Caught ... exception during Module Creation" <<
std::endl;
}
return S_OK;
}
> Hi Brian,
> Can you try creating the method body before calling DefineMethod, then
[quoted text clipped - 23 lines]
> | NNTP-Posting-Host: ip67.tonic.com 216.54.231.67
> | Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
> phx.gbl!TK2MSFTNGP11.phx.gbl
> | Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.clr:9498
[quoted text clipped - 61 lines]
> |
> | Brian