Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CLR探查器:使用DefineAseMblyRef时出现问题_C#_.net_Window_Hook_Clr Profiling Api - Fatal编程技术网

C# CLR探查器:使用DefineAseMblyRef时出现问题

C# CLR探查器:使用DefineAseMblyRef时出现问题,c#,.net,window,hook,clr-profiling-api,C#,.net,Window,Hook,Clr Profiling Api,我想编写一个CLR分析器,用GetILFunctionBody/SetILFunctionBody钩住我们的应用程序函数 我想使用defineasemblyref导入我们的c#dll(用于IL代码) 在此代码定义中,semblyref始终返回True?我的dll必须签名吗?它是否需要安装在全局程序集缓存(GAC)中 根据本MSDN博客: IMetaDataAssemblyEmit::DefineAsemblyRef()为您的程序集提供一个mdAssemblyRef。需要做一些工作才能正确执行此操

我想编写一个CLR分析器,用
GetILFunctionBody/SetILFunctionBody
钩住我们的应用程序函数

我想使用defineasemblyref导入我们的c#dll(用于IL代码) 在此代码定义中,semblyref始终返回
True
?我的dll必须签名吗?它是否需要安装在全局程序集缓存(GAC)中


根据本MSDN博客:

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


您还可以发现这个项目很有用—CLR动态钩子注入可以演示您正在尝试做的事情。

我已经多年没有涉足.NET探查器了,所以我可能在这里有点离谱,但为什么您希望DefineAseMblyRef失败呢?您只需声明一个引用—在使用引用之前,不会解析实际的程序集。您的程序集不必签名或在GAC中,但调用程序集确实需要能够找到它,因此,如果它不在GAC中,它需要位于搜索路径IIRC上的目录中。For posterity作为一个答案发布,似乎它可能会被删除。链接中的描述似乎与这个问题高度相关,尽管我没有确切的知识。
     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;  }

                ......