views:

23

answers:

1

Related to my previous posts I'm moving to .NET 4. I've found that using the previous StrongName.h to get my assembly signing key in unmanaged code is now deprecated, and I need to use MetaHost.h and ICLRStrongName::StrongNameTokenFromAssembly.

The previous StrongNameTokenFromAssembly(..) was very straight forward, now this new one has no documentation on how to use. Does anyone have experience with this interface?

A: 

Wow... that required a lot of hacking around. Here we go!

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

}

Jippers