2010-10-20 41 views
0

與我以前的帖子相關我正在轉移到.NET 4.我發現使用以前的StrongName.h在非託管代碼中獲取我的程序集簽名密鑰現在已被棄用,並且我需要使用MetaHost.h和ICLRStrongName :: StrongNameTokenFromAssembly。如何在.NET 4中使用ICLRStrongName?

以前的StrongNameTokenFromAssembly(..)是非常直接的,現在這個新的沒有關於如何使用的文檔。有沒有人有這個界面的經驗?

回答

0

哇...需要大量的黑客入侵。開始了!

ICLRMetaHost *pMetaHost = NULL; 
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, 
       IID_ICLRMetaHost, (LPVOID*)&pMetaHost); 
if(hr == S_OK) 
{ 
    WCHAR version[100]; 
    DWORD size; 

    hr == pMetaHost->GetVersionFromFile(MyGetApplicationExecutablePath().c_str(), (LPWSTR) &version, &size); 
    if(hr == S_OK) 
    { 
    LPWSTR assemblyVer = version; 
    ICLRRuntimeInfo *pRuntimInfo = NULL; 

    hr = pMetaHost->GetRuntime(assemblyVer, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimInfo); 
    if (hr == S_OK) 
    { 
     ICLRStrongName *pStrongName = NULL; 
     hr = pRuntimInfo->GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName, (LPVOID*)&pStrongName); 

     if(hr == S_OK) 
     { 
      pStrongName->StrongNameTokenFromAssembly(MyGetApplicationExecutablePath().c_str(), &token, &len); 
      DWORD verified = 0; 
      BOOLEAN sigVerified = pStrongName->StrongNameSignatureVerification(MyGetApplicationExecutablePath().c_str(), SN_INFLAG_FORCE_VER , &verified); 
      if (!verified) 
      { 
       //Do something nasty here if the Signature verification failed 
      } 
      pStrongName->StrongNameFreeBuffer(token); 
     } 
    } 
    } 

}