Thursday, February 19, 2009

Taming CoreClr - 3

Remember mscorlib.dll Assembly must be in same folder of coreclr.dll.
Ok, Now CLR is up what Next!
We have to create new Appdomain where we can execute our code.
Signature for New Appdomain create method:

virtual HRESULT STDMETHODCALLTYPE InitializeAppdoamin(
/* [in] */ LPCWSTR pwzAppdomainname,
/* [in] */ DWORD pwzUnknown,
/* [in] */ LPCWSTR pwzManagerAssemblyName,LPCWSTR pwzMAppdomainmanagerName,
DWORD pwzUnknown2,
/* [in] */ LPCWSTR* appdomainSetup,LPCWSTR* activationData,
/* [out] */ DWORD *appDomainID) = 0;

First parameter (pwzAppdomainname) can be any name you like.
Second parameter (pwzUnknown) is unknown as I don't know its purpose but it should be 13 always to get work. Third parameter is Assembly name where Assembly manager class is defined. Remember if you want to use your own Assembly Manager compile the assembly as Silverlight assembly (Remove reference of mscorlib from project and add explicitly reference of mscorlib of Silverlight).pwzMAppdomainmanagerName is fourth parameter and should be name of manager class.pwzUnknown2 is again unknown but it should be 5 everytime. Sixth parameter (AppdomainSetup) is data to setup the Appdomain. ActivationData is data required by CLR to initialize and setup security policies etc, such as the manifest file name where list of files to be loaded by CLR is given etc. appDomainID will contain ID of new Appdomain.

full example

LPCWSTR* appdomainSetup = new LPCWSTR[4];
LPCWSTR* activationData = new LPCWSTR[4];
appdomainSetup[0] = L"TRUSTEDPATH";
appdomainSetup[1] = L"VERSIONING_MANIFEST_BASE";
appdomainSetup[2] = L"MANIFEST_FILE_PATH";
appdomainSetup[3] = L"LOADER_OPTIMIZATION";
appdomainSetup[4] = L"LOCATION_URI";
activationData[0] = L"..\\Debug";
activationData[1] = L"default:\"..\\Debug\"";
activationData[2] = L"..\\slr.dll.managed_manifest";
activationData[3] = L"SingleDomain";
activationData[4] = L"file://../Debug";
a1 = pCLR->InitializeAppdoamin(L"AppDomain",13,
L"System.Windows.Browser, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
L"System.Windows.Hosting.HostAppDomainManager",5, appdomainSetup,activationData,
&appDomainID
);


Here is link to get ISilverlightCLRRuntimeHost interface.
Only few methods will work rest are not implemented in coreclr.

7 comments:

  1. How are you I have a couple of question?
    1)ISilverlightCLRRuntimeHost which function are implemented for the coreCLR I have tried a few with no luck?
    2)Do I need to call magic4 before using any functions

    Thanks
    AP

    ReplyDelete
  2. Hi AP thanks for interest :)
    remeber few things.. copy silverlight binaries in folder of your host.
    As i mentioned always call Magic3 with following params Magic3(2483181568,29805167);
    Please let me know if it does not work on your end. i will send you complete source.
    Thanks :)

    ReplyDelete
  3. Thanks for the quick turn around,

    I used your post to create a function GetCLRRuntimeHost(&m_pCoreCLR) which return S_OK.

    With my pointer to the CoreCLR I was able to make the following calls
    hr = m_pCoreCLR->Magic3(2483181568,29805167);
    //hr = m_pCoreCLR->Magic4();
    hr = m_pCoreCLR->Start();
    hr = m_pCoreCLR->GetCurrentAppDomainId(&dwAppDomainId);

    But when I called ExecuteInDefaultAppDomain
    Run, GetCLRControl they did not appear to be implemented.


    Then I attempted to call CreateSilverlightAppDomain() (I Just put your code into a function) I recieved Invalid Args, so I know the function is implemented. So I am working on that I believe that I set the parameters up incorrectly. Any assistance would be greatly appreciated.

    What I am trying to do is
    Create a new appdomain and load a C# assembly. But I am trying to load the assembly from C++. I would love any ideas you have.

    ReplyDelete
  4. Oh! here is the definition of CreateSilverlightAppDomain()


    HRESULT CDotNetAssemblyLoader
    ::CreateSilverlightAppDomain()
    {
    DWORD dwAppDomainID;
    LPCWSTR* appdomainSetup = new LPCWSTR[4];
    appdomainSetup[0] = L"TRUSTEDPATH";
    appdomainSetup[1] =L"VERSIONING_MANIFEST_BASE";
    appdomainSetup[2] = L"MANIFEST_FILE_PATH";
    appdomainSetup[3] = L"LOADER_OPTIMIZATION";
    appdomainSetup[4] = L"LOCATION_URI";

    LPCWSTR* activationData = new LPCWSTR[4];
    activationData[0] =

    L"F:\\SilverlightClassLibrary1\\Bin\\Debug\\";

    activationData[1] = L"default:\"F:\\SilverlightClassLibrary\\Bin\\Debug\\\"";

    activationData[2] = L"..\\slr.dll.managed_manifest";
    activationData[3] = L"SingleDomain";
    activationData[4] = L"file://F:/SilverlightClassLibrary1/Bin/Debug";

    HRESULT hResult = m_pCoreCLR->InitializeAppdoamin(L"NewAppDomain",13,
    L"System.Windows.Browser, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
    L"System.Windows.Hosting.HostAppDomainManager",5, appdomainSetup,activationData,
    &dwAppDomainID);

    return hResult;
    }


    this it the directory were I created my C# assembly but like I said I think I set up the parameters for the function incorrectly.

    Thanks,

    AP
    F:\\SilverlightClassLibrary\\Bin\\Debug\\\""

    ReplyDelete
  5. Here is the updated version of CreateSilverlightAppDomain() now I am returning the strange -2146233054 (not InvalidArg) result that I got with the other functions I tried to call. Now I am thinking the function are implemented I am just doing something wrong, not performing a step, or dont have permission to call the functions

    HRESULT CDotNetAssemblyLoader::CreateSilverlightAppDomain()
    {
    DWORD dwAppDomainID;
    LPCWSTR* appdomainSetup = new LPCWSTR[4];
    appdomainSetup[0] = L"TRUSTEDPATH";
    appdomainSetup[1] =L"VERSIONING_MANIFEST_BASE";
    appdomainSetup[2] = L"MANIFEST_FILE_PATH";
    appdomainSetup[3] = L"LOADER_OPTIMIZATION";
    appdomainSetup[4] = L"LOCATION_URI";

    LPCWSTR* activationData = new LPCWSTR[4];

    //F:\SilverlightClassLibrary1\Bin\Debug
    activationData[0] =

    L"F:\\SilverlightClassLibrary1\\Bin\\Debug";
    //activationData[0] = L"..\\Debug";

    activationData[1] = L"default:\"F:\\SilverlightClassLibrary1\\Bin\\Debug\"";
    //activationData[1] = L"default:\"..\\Debug\"";

    activationData[2] = L"F:\\SilverlightClassLibrary1\\Bin\\slr.dll.managed_manifest";
    //activationData[2] = L"..\\slr.dll.managed_manifest";

    activationData[3] = L"SingleDomain";


    activationData[4] = L"file://F:/SilverlightClassLibrary1/Bin/Debug";
    //activationData[4] = L"file://../Debug";


    HRESULT hResult = m_pCoreCLR->InitializeAppdoamin(L"TPLoadAppDomain",13,
    L"System.Windows.Browser, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
    L"System.Windows.Hosting.HostAppDomainManager",5, appdomainSetup,activationData,
    &dwAppDomainID);
    return hResult;



    }

    In the method I tried to convert your parameters to my versions but I still thinking I am dojng somthing wrong

    Thanks,
    Ap

    ReplyDelete
  6. I understand my problems now, I was recieving the CORE_E_TYPELOAD exception. I need to create my host , however one question I take it you are creating your host in managed C++.

    I would love to take a look at your source

    thanks
    AP

    ReplyDelete
  7. I created my host but I cannot call

    ExecuteInDefaultAppDomain
    Run

    did you see the same problem?

    ReplyDelete