2011-07-28 24 views
18

我想編寫一個CLR分析器來掛鉤我們的應用程序函數GetILFunctionBody/SetILFunctionBodyCLR分析器:使用DefineAssemblyRef時出現問題

我想使用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; } 

       ...... 
+1

我還沒有涉足.NET分析器多年,所以我可能在這裏基地,但你爲什麼會期望DefineAssemblyRef失敗?你只是聲明一個引用 - 實際的程序集在使用引用之前不會被解析。你的程序集不需要簽名或者在GAC中,但是調用程序集確實需要能夠找到它,所以如果它不在GAC中,它需要位於搜索路徑IIRC的目錄中。 –

+0

後代[此鏈接](http://www.dupuis.me/node/18)發佈爲可能被刪除的答案。鏈接中的描述似乎與問題高度相關,儘管我沒有確切的知識。 – Ben

回答

1

根據這個MSDN博客http://blogs.msdn.com/b/davbr/archive/2006/02/27/540280.aspx

IMetaDataAssemblyEmit :: DefineAssemblyRef()給你一個mdAssemblyRef你的裝配。 有一點工作是必要的,以得到這個權利。一個可靠的方法來引用你的程序集是簽署你的程序集,將其添加到GAC,並使用公鑰「gacutil/l」打印出來給你

你也可以找到有用的項目--CLR動態鉤子注入http://www.dupuis.me/node/18這種表現你正在嘗試做什麼。