Get CLSID by PIA Type

Edit: It looks like Jon Skeet had similar questions: How does the C # compiler detect COM types?

How can I get the CLSID for a given interface in the primary interop assembly? Here's what I'm talking about:

// The c# compiler does some interesting magic.
// The following code ...
var app = new Microsoft.Office.Interop.Outlook.Application();

// ... is compiled like so (disassembled with Reflector):
var app =((Microsoft.Office.Interop.Outlook.Application)
  Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("0006F03A-0000-0000-C000-000000000046"))));

      


Microsoft.Office.Interop.Outlook.Application

is an interface and therefore cannot be created directly. Interestingly, C # allows you to treat these COM interfaces as if they were classes that you can create using a keyword new

.

What I want to know, since System.Type

for a given interface, how can I get the CLSID?

Note. Ultimately I want to create an instance with an interface System.Type

- I don't care how to do that. I guess the easiest way to do this is to get the type-aware CLSID like the C # compiler does.

+2


a source to share


2 answers


Here's my current solution:

// Get the PIA assemby name, using the GUID of the typlib
string asmName;
string asmCodeBase;
var conv = new System.Runtime.InteropServices.TypeLibConverter();
conv.GetPrimaryInteropAssembly(new Guid("00062FFF-0000-0000-C000-000000000046"), 9, 3, 0, out asmName, out asmCodeBase);

// Load the PIA, and get the interface type
var assembly = System.Reflection.Assembly.Load(asmName);
var type = assembly.GetType("Microsoft.Office.Interop.Outlook.Application");

// Get the coclass
var coClassAttr = (System.Runtime.InteropServices.CoClassAttribute)
    type.GetCustomAttributes(typeof(System.Runtime.InteropServices.CoClassAttribute), false)[0];
var coClass = coClassAttr.CoClass;

// Instantiate the coclass
var app = Activator.CreateInstance(coClassAttr.CoClass);

// If needed, the CLSID is also available:
var clsid = coClass.GUID;

      

I figured it out by taking apart the GAC'd PIA. I noticed that I Outlook.Application

was decorated CoClassAttribute

, for example:

[ComImport, Guid("00063001-0000-0000-C000-000000000046"), CoClass(typeof(ApplicationClass))]
public interface Application : _Application, ApplicationEvents_11_Event
{
}

      



ApplicationClass

looks something like this:

[ComImport, ClassInterface((short) 0), ComSourceInterfaces("Microsoft.Office.Interop.Outlook.ApplicationEvents_11\0Microsoft.Office.Interop.Outlook.ApplicationEvents\0Microsoft.Office.Interop.Outlook.ApplicationEvents_10\0"), Guid("0006F03A-0000-0000-C000-000000000046"), TypeLibType((short) 11)]
public class ApplicationClass : _Application, Application, ApplicationEvents_11_Event, ApplicationEvents_Event, ApplicationEvents_10_Event
{
    //...
}

      

Let me know what you all think, so I can decide whether to mark this as the selected answer.

+2


a source


Try the GUID

property.



0


a source







All Articles