使用DefineAssemblyRef时出现问题

使用DefineAssemblyRef时出现问题

本文介绍了CLR分析器:使用DefineAssemblyRef时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个CLR分析器,以将我们的应用程序功能与GetILFunctionBody/SetILFunctionBody挂钩.

I want to write a CLR profiler to hook our application function with GetILFunctionBody/SetILFunctionBody.

我想使用DefineAssemblyRef导入我们的c#dll(用于IL代码)在这段代码中,DefineAssemblyRef总是返回True?我的dll是否必须签名?是否需要将其安装在全局程序集缓存(GAC)中?

I want to use DefineAssemblyRef to import our c# dll (for use in IL code)in this code DefineAssemblyRef always return True? Does my dll have to be signed? Does it need to be installed in the Global Assembly Cache (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;  }

                ......

推荐答案

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

IMetaDataAssemblyEmit :: DefineAssemblyRef()为您的程序集提供mdAssemblyRef. 要做一些正确的事情是必要的.引用程序集的一种可靠方法是对程序集进行签名,将其添加到GAC中,并使用为您打印出来的"gacutil/l"公钥

IMetaDataAssemblyEmit::DefineAssemblyRef() gives you an mdAssemblyRef to your assembly. A little work is necessary to get this right. A reliable way to reference your assembly is to sign your assembly, add it to the GAC, and use the public key that "gacutil /l" prints out for you

您也可以发现此项目有用-CLR动态挂钩注入 http://www.dupuis.me /node/18 可以演示您正在尝试做的事情.

You could also find usefull this project - CLR dynamic hook injection http://www.dupuis.me/node/18 that kind of demonstrates what you are trying to do.

这篇关于CLR分析器:使用DefineAssemblyRef时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:28