2012-04-24 48 views
2

我在C++和C#代码之间有一个可用的CLI接口。该代码有一个C++抽象接口,如:通过在非默认AppDomain中调用C#函数调用C++指针接口

-------------C++ Interface--------------- 
namespace cppns 
{ 
    class cppInterface 
    { 
     public: 
     virtual bool Start(const char *pcDir) = 0; 
    }; 
} 

------Implementation of abstract C++ interface in same dll--------- 
namespace cppns 
{ 
    class cppimp : public cppInterface 
    private: 
     gcroot<MyInternalCSharpClass^> mInternalClassAccess; 
    public: 
     cppimp::cppimp() 
     { 
      mInternalClassAccess = gcnew MyInternalCSharpClass(); 
     } 

     virtual bool cppimp::Start(const char *pcDir) 
     { 
      System::AppDomain ^appDom = AppDomain::CurrentDomain::get(); 
      System::String ^strDomainName = appDom->FriendlyName; 

      mInternalClassAccess->Initalize(pcDir); 
     } 
} 

---------Method to create an instance of the class in a factory-------------- 
cppns::cppInterface *GetImplObject() 
{ 
    return new cppns::cppimp(); 
} 

----------Factory class .h to allow C++ to get an instance of the cppimp class------ 
------The C++ code knows about the abstract interface by including the header file-- 
------FactoryExport is __declspec(dllexport) when compiled in dll and--------------- 
----- __declspec(dllimport) when used as a header file in exe that uses header------ 
class FactoryExport ClassFactory 
{ 
    public: 
     static cppns::cppInterface *CreateImpl(); 
}; 

----------Factory class .cpp to allow C++ to get an instance of the cppimp class------ 
cppns::cppInterface *ClassFactory::CreateImpl() 
{ 
    return GetImplObject(); 
} 

此代码正确地让我打电话给CreateImplInit在获取包含Start方法的接口的实现。我的问题是,我试图强制整个CLR/.NET加载并执行到不是默认AppDomain的AppDomain中。我可以使用下面的代码创建一个辅助的AppDomain:

CComPtr<ICorRuntimeHost> pRuntimeHost; 
    //Retrieve a pointer to the ICorRuntimeHost interface 
    HRESULT hr = CorBindToRuntimeEx(
       L"v2.0.50727", //Retrieve last version before 4.0. 
       // NULL, //Retrieve latest version by default 
       L"wks", 
       STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC, 
       CLSID_CorRuntimeHost, 
       IID_ICorRuntimeHost, 
       (void**)&pRuntimeHost.p 
       ); 

hr = pRuntimeHost->Start(); 

DWORD dwAppDomainId = 22; 
WCHAR domainName[80 + 1]; 
    swprintf(domainName, 80, L"%s-%ld",L"NoDefaultDomain", dwAppDomainId); 

CComPtr<IUnknown> pUnknownAppDomain; 
hr = pRuntimeHost->CreateDomainEx(domainName, NULL, NULL, &pUnknownAppDomain); 

CComPtr<_AppDomain> pAppDomain; 
hr = pUnknownAppDomain->QueryInterface(__uuidof(_AppDomain), (VOID**)&pAppDomain.p); 

BSTR bstrFriendlyName; 
hr = pAppDomain->get_FriendlyName(&bstrFriendlyName); 
if (SUCCEEDED(hr)) 
{ 
    _bstr_t bstrFriendlyNameWrap(bstrFriendlyName, false); 
} 

_bstr_t bstrAssemblyName("InteropCode"); 
CComPtr<_Assembly> pAssembly; 
hr = pAppDomain->Load_2(bstrAssemblyName, &pAssembly); 

BSTR bstrFullName; 
hr = pAssembly->get_FullName(&bstrFullName); 
if (SUCCEEDED(hr)) 
{ 
    _bstr_t bstrFullNameWrap(bstrFullName, false); 
    std::cout << "Assembly name is: " << bstrFullNameWrap << "\n"; 
} 

让工厂回到我cppns接口::本次应用程序域内cppInterface的每个尝试已经失败。我甚至尝试创建一个C#类的二级工厂,该工厂返回指向已实现接口的指针,以便对程序集进行Invoke调用,希望可以使其余的代码在已加载Assembly的AppDomain中执行,但Invoke返回一个IDispatch指针,我似乎无法将其映射回我的界面上的任何类型的C++指针。

namespace cppns 
{ 
    public ref class NetFactory 
    { 
    public: 
     NetFactory() 
     { 
     } 

     cppInterface *CreateInterop() 
     { 
      return GetImplObject();; 
     } 
    }; 
} 

是否有另一种方式来获得一切的二级AppDomain中运行,或者是IDispatch指针可用在调用Start方法?

回答

1

我已经设法让大部分.NET的东西在另一个域中运行。似乎没有办法让CLI层在默认的AppDomain以外的其他任何地方运行。

为了使这项工作,我需要使两个应用程序域内的类派生自MarshalByRefObject。在我上面的例子中,这意味着我必须更改MyInternalCSharpClass,以便它从MarshalByRefObject派生。使从MyInternalCSharpClass发送并返回的对象也从MarshalByRefObject派生出来也是不必要的。最后,我传递和返回的这些相同的对象必须具有[Serializable]属性,并且还要将所有的私有变量标记为public。请注意,如果通过AppDomain传输的类已经使用Serializable属性,则可以在每个正式的私有变量上使用[XmlIgnore],以避免更改正在执行的序列化。

bool CreateInstanceInAppDomain(const char *pcAppDomainName) 
{ 
    bool bRtn = false; 

    gcroot<String^> csStrAppDomainName (gcnew String(pcAppDomainName)); 
    mAppDomain = AppDomain::CreateDomain(csStrAppDomainName); 
    delete csStrAppDomainName; 
    Object^ MyInternalObject = mAppDomain->CreateInstanceAndUnwrap("AssemblyName", "ClassNameSpace.MyInternalCSharpClass"); 
    mInternalClassAccess = dynamic_cast<MyInternalCSharpClass^>(MyInternalObject); 
    if (mInternalClassAccess) 
    { 
     bRtn = true; 
    } 

    return bRtn; 
} 

现在,一切都可以在应用程序域之间移动我通过执行以下操作创建第二的AppDomain