CLR profiler: issue in using DefineAssemblyRef

試著忘記壹切 提交于 2019-12-08 14:38:03

问题


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

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;  }

                ......

回答1:


According to this MSDN blog http://blogs.msdn.com/b/davbr/archive/2006/02/27/540280.aspx :

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

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.



来源:https://stackoverflow.com/questions/6859881/clr-profiler-issue-in-using-defineassemblyref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!