Unmanaged host App catching events from managed

I want to host a managed assembly (decrypted from resources) on a C++
application. The first step is done and works.

but I have a problem, if a missing assembly is not found when I execute the dynamic loaded assembly the application don't start and no "_com_error" is thrown.

I saw a bunch of methods exposed by the interface _AppDomain:

add_UnhandledException
remove_UnhandledException
...
add_AssemblyResolve
remove_AssemblyResolve
...

How to handle those events from the C++ using COM interop to access .NET

TIA,
ramon



Answer this question

Unmanaged host App catching events from managed

  • SirLiahona

    Hi Ramon,

    If you're using v2.0 of the runtime, you can provide an IHostControl::GetHostManager call to provide the CLR with an IHostAssemblyManager when it asks for one.  The IHostAssemblyManager interface has a method which the CLR calls to get an IHostAssemblyStore.  IHostAssemblyStore allows you to specify assemblies that your store will not load (such as mscorlib, system, system.xml, etc) and also has a ProvideAssembly method where you can supply the CLR with the assembly you've loaded from your resources.

    -Shawn



  • Kurdo

    Make sure that you're returning an assembly with teh correct architecture.  I believe the CLR will first ask you for a processor-specific assembly, then a neutral one.  Depending on what you're supplying, you'll need to respond to only one of those requests.

    -Shawn



  • John McLean

    I solved the problem :)

    Under custom AppDomainManager
    I need to override and implement:



    public override HostSecurityManager HostSecurityManager
    {
       get;
    }

     


    This is documented on the examples form the book, but I was blind :/
    But I confirm that the "pAssemblyId" in the method "ProvideAssembly" cannot be ZERO, so if anyone compile the samples it wll not work.

    bye


  • AlBu70

    Hello,

    I get rid of the missing assembly exception by setting the variable [out] "pAssemblyId" in the method "ProvideAssembly" to something diferent from ZERO, maybe when the host sets the assembly ID to zero, the CRL interpret that as a failure!

    ---- Assuming I'm on the rigth path, here is the next problem:

    This is the exception I caught under my custom AppDomainManager
    when trying to run the main assembly (resolved on the c++ host):



    Exception executing entry point: System.IO.FileLoadException: Could not load file or assembly 'SampleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418)
    File name: 'SampleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.Security.Policy.PolicyException: Execution permission cannot be acquired.
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission)
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)
       at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.Load(String assemblyString, Evidence assemblySecurity)
       at System.AppDomain.ExecuteAssemblyByName(String assemblyName, Evidence assemblySecurity, String[] args)
       at CocoonHostRuntime.CocoonDomainManager.Run(String assemblyName, String typeName) in D:\Projects\RunCocoon\CocoonHostRuntime\CocoonDomainManager.cs:line 33

     


    Any ideas I'm missing something for sure, but don't know what

    Thank you very much,
    RB


  • Toni_M

    Ramon,
      could you provide us with a bit more detail What version of .Net are you using How are you hosting the assembly What is your intention when you discover the unhandeled exception There are several potential methods for resolving this issue and I want to find the one that works best for your situation.

    Thanks,
      Piotr

  • JohnEK

    Hi,

    Thanks for helping.

    My first approach under 1.1 is over, so I will try using the new interface that Shawn is talking about.

    I found a good book: "Customizing The Microsoft Dot NET Framework Common Language Runtime"

    I'am following the cocoon examples, after some adjusts to make it compile on last version of version 2.0, I found new problems :/

    So far, I enum GAC and set all those assemblies inside:

    IHostAssemblyManager::GetNonHostStoreAssemblies

    Then IHostAssemblyStore::ProvideAssembly is called only for assemblies not in the GAC (btw, don't know if this is a good aproach! )

    The problem is: even if I return the right assembly, an exception is throw saying that the assembly is missing.

    Any ideas


  • Unmanaged host App catching events from managed