【发布时间】:2011-10-15 03:36:41
【问题描述】:
我想编写一个 CLR 分析器来将我们的应用程序函数与 GetILFunctionBody/SetILFunctionBody 挂钩。
我想使用 DefineAssemblyRef 来导入我们的 c# dll(用于 IL 代码)
在这段代码中DefineAssemblyRef 总是返回True?我的dll必须签名吗?是否需要安装在全局程序集缓存 (GAC) 中?
HRESULT CProfilerCallback::JITCompilationStarted
(
UINT functionId,
BOOL fIsSafeToBlock
)
{
ClassID classID;
ModuleID moduleID;
mdToken token;
wchar_t wszClass[512];
wchar_t wszMethod[512];
HRESULT result = S_OK;
ClassID classId = 0;
ModuleID moduleId = 0;
mdToken tkMethod = 0;
// Get the moduleID and tkMethod
m_pICorProfilerInfo->GetFunctionInfo(functionId, &classId, &moduleId, &tkMethod);
if(!GetMethodNameFromFunctionId(functionId,wszClass,wszMethod))
{return S_FALSE;}
if(wcscmp(wszMethod,L"FunctionName") == 0)
{
// Get the metadata import
IMetaDataImport* pMetaDataImport = NULL;
DebugBreak();
result = m_pICorProfilerInfo->GetModuleMetaData
(
moduleId,
ofRead,
IID_IMetaDataImport,
(IUnknown** )&pMetaDataImport
);
if (FAILED(result))
{ return S_FALSE;}
//
// Metadata modification
//
IMetaDataEmit* pMetaDataEmit = NULL;
IMetaDataAssemblyEmit* pMetaDataAssemblyEmit = NULL;
mdAssemblyRef tkLoggerLib;
HRESULT res;
res = m_pICorProfilerInfo->GetModuleMetaData
(
moduleId, /// The ID of the module to which the interface instance will be mapped
ofRead | ofWrite,
IID_IMetaDataEmit,
(IUnknown** )&pMetaDataEmit
);
if (FAILED(res)) {DebugBreak(); return S_FALSE;} /// DebugBreak for debug
res = pMetaDataEmit->QueryInterface
(
IID_IMetaDataAssemblyEmit,
(void**)&pMetaDataAssemblyEmit
);
if (FAILED(res)) { return S_FALSE;}
// Get the token for the Logger class and its Log method
mdTypeDef tkLogger = 0;
mdMethodDef tkLog = 0;
// Create a token for the Log.dll assembly
ASSEMBLYMETADATA amd;
ZeroMemory(&amd, sizeof(amd));
amd.usMajorVersion = 0;
amd.usMinorVersion = 0;
amd.usBuildNumber = 0;
amd.usRevisionNumber = 0;
res= pMetaDataAssemblyEmit->DefineAssemblyRef
(
NULL, 0, // No public key token
L"Dllname", ///dll name
&amd, NULL, 0, 0,
&tkLoggerLib
);
if (FAILED(res)) {return S_FALSE; }
......
【问题讨论】:
-
我已经很多年没有涉足 .NET 分析器了,所以我可能会离开这里,但是你为什么会期望 DefineAssemblyRef 失败呢?您只是在声明一个引用 - 在使用引用之前,实际的程序集不会被解析。您的程序集不必签名或在 GAC 中,但调用程序集确实需要能够找到它,因此如果它不在 GAC 中,则它需要位于搜索路径 IIRC 上的目录中。跨度>
-
为了后代this link 被发布为似乎可能被删除的答案。链接中的描述似乎与问题高度相关,尽管我没有确切的知识。
标签: c# .net window hook clr-profiling-api