2012-04-24 107 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