CLR分析器:使用DefineAssemblyRef时出现问题

may*_*sam 19 .net c# hook window clr-profiling-api

我想写一个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;  }

                ......
Run Code Online (Sandbox Code Playgroud)

use*_*883 1

根据此 MSDN 博客http://blogs.msdn.com/b/davbr/archive/2006/02/27/540280.aspx

\n

IMetaDataAssemblyEmit::DefineAssemblyRef() 为您提供程序集的 mdAssemblyRef。\xc2\xa0需要做一些工作才能正确完成此操作。引用程序集的可靠方法是对程序集进行签名,将其添加到 GAC,然后使用“gacutil /l”为您打印的公钥

\n

您还可以找到有用的这个项目 - CLR 动态钩子注入http://www.dupuis.me/node/18 ,它演示了您正在尝试执行的操作。

\n