2010-11-01 90 views

回答

1

從ICLRRuntimeHost :: GetCLRControl生成ICLRControl後,使用IID_ICLRAppDomainResourceMonitor爲所需的接口執行ICLRControl :: GetCLRManager。

例如

ICLRMetaHost *meta; 
ICLRRuntimeInfo *info; 
ICLRRuntimeHost *host; 
ICLRControl *control; 
ICLRAppDomainResourceMonitor *monitor; 
CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (void **)&meta); 
meta->GetRuntime(L"v4.0.30319", IID_CLRRuntimeInfo, (void **)&runtime); 
info->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void **)&host); 
host->GetCLRControl(&control); 
control->GetCLRManager(IID_ICLRAppDomainResourceMonitor, (void **)&monitor); 

// ... rest of CLR startup ... 

unsigned long long bytes; 
monitor->GetCurrentAllocated(1, &bytes); 

編輯:請注意,您必須使用CLR v4.0的工作。使用4.0 metahost和2.0運行時是不夠的。

2

在上面的代碼中,我們可以成功創建ICLRAppDomainResourceMonitor的實例。

其實我試圖獲取運行在同一個系統上的每個.NET 4.0進程的每個AppDomain的屬性值。

我嘗試下面的代碼來獲取應用程序域的數據:

void getAttributeValues(struct processIDMap *NETProcessID){ //NETProcessID is collection of .NET 4.0 process running on system 

    ICorPublishAppDomain* appDomains[1]; 
    ULONG aFetched = 1; 

    ICLRMetaHost *meta = NULL; 
    ICLRRuntimeInfo *info = NULL; 
    ICLRRuntimeHost *host = NULL; 
    ICLRControl *control = NULL; 
    ICLRAppDomainResourceMonitor *monitor = NULL; 


    hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (void **)&meta); 
    if (! SUCCEEDED(hr)) 
     printf("hr failed...."); 


    struct processIDMap *tempStruct = NETProcessID; 

    while(tempStruct != NULL){ 

     HANDLE pHandle = NULL; 
     IEnumUnknown * pRtEnum = NULL; 

     DWORD Aid = 0; 
     ULONGLONG bytes = 0; 
     ULONG fetched = 0; 

     pHandle = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,tempStruct->PID); 
     hr = meta->EnumerateLoadedRuntimes(pHandle, &pRtEnum); 
     if (! SUCCEEDED(hr)) 
      printf("hr failed...."); 

     while ((hr = pRtEnum->Next(1,(IUnknown **)&info,&fetched)) == S_OK && fetched > 0){ 


      hr = info->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void **)&host); 
      if (! SUCCEEDED(hr)) 
       printf("hr failed...."); 

      hr = host->GetCLRControl(&control); 
      if (! SUCCEEDED(hr)) 
       printf("hr failed...."); 

      hr = control->GetCLRManager(IID_ICLRAppDomainResourceMonitor, (void **)&monitor); 
      if (! SUCCEEDED(hr)) 
       printf("hr failed...."); 

      hr = monitor->GetCurrentAllocated(Aid, &bytes); 
      if (! SUCCEEDED(hr)) 
       printf("hr failed...."); 

     } 

     //info->Release(); 
     //control->Release(); 
     //monitor->Release(); 
     //host->Release(); 

     tempStruct = tempStruct->next; 
     pRtEnum->Release(); 
     CloseHandle(pHandle); 


    } 

    meta->Release(); 

}

但API 顯示器 - > GetCurrentAllocated(AID,&字節)hr的返回值作爲-2146234348即COR_E_APPDOMAINUNLOADED

請提供您的意見。

謝謝,