Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI C# .NET CLR
Hi friends
 
I could discover that we can redirect to a specific version of a .net assembly by using configuration file as below:
 
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="myAssembly"
          publicKeyToken="32ab4ba45e0a69a1"
          culture="en-us" />
        <!-- Assembly versions can be redirected in application, 
          publisher policy, or machine configuration files. -->
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
 
But I do not want to use that config file. In fact I can not do that. I am working on a C++ application which also have some .net modules. We explicitly load the CLR by using stuff as shown below:
ICLRMetaHost *m_pMetaHost = NULL;
	HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&m_pMetaHost);
...
// Get the runtime host 
	hr = m_pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID *)&m_pRuntimeInfo);
	if (!m_pRuntimeInfo) {
		*pHR = hr;
		return false;	
	}
...
// Register our this object as host control
	hr = m_pClrHost->SetHostControl(static_cast<IHostControl *>(this));
	if (FAILED(hr)) {
		*pHR = hr;
		return false;
	}
 
	// Get pointer to ICLRControl interface
	ICLRControl *pCLRControl = NULL;
	hr = m_pClrHost->GetCLRControl(&pCLRControl);
 
	if (FAILED(hr) || pCLRControl == NULL) {
		*pHR = hr;
		return false;
	}
...
// String defines used to construct args to SetAppDomainType.  Note these are wide strings.
	#define ADM_ASSEMBLY_REF L"MyAssembly, Version=%s, Culture=neutral, PublicKeyToken=1c8d91d2de78b25e"
	#define ADM_CLASS_REF    L"My.Namespace.MyAppDomainManager"
 
	// Convert version string to wide char and into assembly ref string
	wchar_t versionInfo[256];
	MultiByteToWideChar(CP_ACP, 0, MI_VERSION_STRING, -1, versionInfo, sizeof(versionInfo) / sizeof(wchar_t));
	wchar_t assemblyRef[512];
	swprintf(assemblyRef, sizeof(assemblyRef), ADM_ASSEMBLY_REF, versionInfo);
 
	// Set the app domain manager type
	hr = pCLRControl->SetAppDomainManagerType(assemblyRef, ADM_CLASS_REF);
 
Can someone please tell how can I specify which version of a specific assembly is to be redirected to? Please note that I want to do this with respect to default application only. I am not creating any new application domain. I think while initializing/loading the CLR, we might have some way to specify the configuration information via code instead of using any physical configuration file.
 
Any help/direction is heartily appreciated.
 
Thanks in advance
Aseem Sharma
Posted 30-Jan-13 3:43am
Comments
Sergey Alexandrovich Kryukov at 30-Jan-13 12:44pm
   
Redirect? How can it be redirect? Are you familiar with side-by-side execution?
—SA
Aseem Sharma at 30-Jan-13 20:03pm
   
Thanks Sergey for your reply,
 
I am not much aware of Side by Side execution. But I could resolve my issue. We just need to override below function in our class MyAppDomainManager (derived from AppDomainManager)
 
public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
 
Since here we have access to AppDomainSetup , we can do the redirection stuff here only.
 
Regards
Aseem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100