none
Invoke failed when called from c++ code in case of MMCSnapIn application RRS feed

  • Question

  • Hi,

    I have a Tool which uses two dlls which are :

    1) Managed Code ( C# ) - Managed.dll
    2) Native ( C++ ) - Unmanaged.dll

    I have an MMC SnapIn application running using MMC3.0. MMCSnapIn application is a mix of win32 and .NET controls. Some information about the MMCSnapIn app:

    1) It has two appdomains :
                    a) Default AppDomain
                    b) FormsView AppDomain


    My Tool loads the Unmanaged.dll and Managed.dll in the MMC process and after that in my unmanaged.dll I try to enumerate the appdomains in the MMC process and try to load Managed.dll in each Appdomain and then call some method defined in Managed.dll class. I do that using the following code

            while( objClrImpl.EnumerateDomains( &domain ) )
            {

                numDomains++;

                IAssemblyReflect managedAssembly;

               
                if( domain->Load( assemblyName, &managedAssembly) )
                {
                    ITypeReflectDelete pNetInfestType;
                    IObjectReflectDelete netinfest;
                  
                   if(managedAssembly->GetType(typeName,&pobjClassType))
                    {
                            bool bSuccess = pobjClassType->InvokeMember(    methodName,
                                                                NULL,
                                                                s_Args,
                                                                result );
                    }
                }
            }   


    The InvokeMember in above code works fine when I run it in any Managed or Mixed application even with Multi AppDomains, but fails when I run it inside MMC SnapIn application. It failed to InvokeMember for non-Default Appdomain i.e FormsView Appdomain.

    When I replaced the above code with below one , its started working.

            while( objClrImpl.EnumerateDomains( &domain ) )
            {

                numDomains++;

                IAssemblyReflect managedAssembly;

               
                if( domain->Load( assemblyName, &managedAssembly) )
                {
                    ITypeReflectDelete pNetInfestType;
                    IObjectReflectDelete netinfest;
                   
                    if(managedAssembly->CreateInstance(typeName,&objClass))
                    {
                        bool bobjClassType = objClass->GetType(&pobjClassType );
                            bool bSuccess = pobjClassType->InvokeMember(    methodName,
                                                                NULL,
                                                                s_Args,
                                                                result );
                    }
                }
            }   

    I do not want to create instance of my class everytime to call a static method.

    I found one difference between normal multiappdomain and MMCSnapIn application and that is in the way CLR Loads these assemblies. I think in case of MMCSnapIn it loads the assemblies as Domain Neutral Assemblies.

    I'm not sure if this is the reason, but thought of putting all the information I have.

    Thanks
    Tuesday, February 17, 2009 9:44 AM