Albin Sunnanbo - 2009-03-21

You can use the following code to get the TypeLib GUID from a file

// Copyright Albin Sunnanbo
// This code is public domain and free to use without any resitrictions, including commercial use.
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace GetComGuidLib
{
public class GetComGuid
{
[DllImport("oleaut32", CharSet = CharSet.Unicode)]
private extern static void LoadTypeLib(string dllFilePath, out ITypeLib typeLibrary);

/// <summary>
/// Returns the TypeLib Guid for the typelib in the file fileName
/// </summary>
/// <param name="fileName">The dll, ocx, tlb or other file containing a TypeLib</param>
/// <returns></returns>
public static Guid GetGuid(string fileName)
{
ITypeLib typeLibrary;
Guid guidIUnknown = Guid.Empty;
LoadTypeLib(fileName, out typeLibrary);

IntPtr pTypeLibAtt;
typeLibrary.GetLibAttr(out pTypeLibAtt);
var typeLibAtt = (System.Runtime.InteropServices.ComTypes.TYPELIBATTR)
Marshal.PtrToStructure(pTypeLibAtt, typeof(System.Runtime.InteropServices.ComTypes.TYPELIBATTR));
return typeLibAtt.guid;
}
}
}