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=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
L"System.Windows.Hosting.HostAppDomainManager",5, appdomainSetup,activationData,

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

Monday, February 2, 2009

Taming CoreClr Part-2

Hope you did not get bore with first blog of this series :)

I will be more technical going forward with code samples.
After successfully getting the Guid for CoreClr it was time to host CoreClr in custom host.
here is the code for it..

HMODULE clrmodule = LoadLibrary (TEXT ( "..//Coreclr.dll"));
GetCLRRuntimeHost _clrMethod;
_clrMethod = (GetCLRRuntimeHost)GetProcAddress( clrmodule , "GetCLRRuntimeHost" );

You will be eager to know the signature of GetCLRRuntimeHost.

typedef int (__stdcall *GetCLRRuntimeHost)(const IID &CLRGUID,PVOID* clrHost);

We got the method handler(method pointer).
Let us get the SilverlightCLR now:

ISilverlightCLRRuntimeHost *pCLR = NULL;
HRESULT a = _clrMethod(CLRGUID,(PVOID*) &pCLR);

One of the main question is still unanswered definition of ISilverlightCLRRuntimeHost?

As told earlier initial thought was it should be similar to (or same) ICLRRuntimeHost.
I used same interface and tried to call ExecuteApplication.
boom!!!! it crashed.
tried calling Start() again failed...with -2146234334 error code, HOST_E_INVALIDOPERATION.
Tried other methods from ICLRRuntimeHost but none worked.
So again i decided to debug Silverlight with IDA.
After taking the CLRHOST interface pointer they are making a call to
call eax //ECX+38h

where ECX is pointing to the vptr of coreclr object.
Now if i calculate 38h it becomes 12th method of interface,
here was a catch.. ICLRRuntimeHost only has 9 methods.
So it clears that SilverlightCLRRuntime host interface is having additional methods.
This method takes two parameters of int type


I called this method Magic because the reason was not known for this call.
After making this call again i tried pCLR->Start()

bingo CLR was up :)

to be concluded.......