How to use the PIA

Note: The interfaces defined in the PIA cannot be passed across thread apartment boundaries at this time. The Managed API does not have this limitation. (see How to use the managed wrapper API)

After obtaining or building Microsoft.Runtime.Hosting.Interop.dll, add a reference to it in your project. By default, Visual Studio will recognize it as a PIA and will make use of the Type Embedding" feature.

At this point, you have the interop definitions, but you'll need a bit of code to help you get instances of one of the "metahost" interfaces that serve as the starting points for using the APIs. First, you'll need a p/Invoke signature for mscoree!CLRCreateInstance:

        [DllImport("mscoree.dll", PreserveSig = false, EntryPoint = "CLRCreateInstance")]
        [return: MarshalAs(UnmanagedType.Interface)]
        private static extern object ClrCreateInterface(
                [MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
                [MarshalAs(UnmanagedType.LPStruct)] Guid riid);

Then, you'll need the Guid's required to create IClrMetaHost or IClrMetaHostPolicy:

        private static Guid MetaHostClsIdGuid =
            new Guid(0x9280188D, 0xE8E, 0x4867, 0xB3, 0xC, 0x7F, 0xA8, 0x38, 0x84, 0xE8, 0xDE);

        private static Guid MetaHostGuid =
            new Guid(0xD332DB9E, 0xB9B3, 0x4125, 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16);

        private static Guid MetaHostPolicyClsIdGuid =
            new Guid(0x2EBCD49A, 0x1B47, 0x4A61, 0xB1, 0x3A, 0x4A, 0x3, 0x70, 0x1E, 0x59, 0x4B);

        private static Guid MetaHostPolicyGuid =
            new Guid(0xE2190695, 0x77B2, 0x492e, 0x8E, 0x14, 0xC4, 0xB3, 0xA7, 0xFD, 0xD5, 0x93);

Now you can obtain the interfaces with calls like this:

IClrMetaHost metahost = (IClrMetaHost)ClrCreateInstance(MetaHostClsIdGuid, MetaHostGuid);

Last edited Feb 16, 2010 at 7:33 PM by markmiller, version 4


No comments yet.