Type.GetTypeFromCLSID Method (Guid, String)

Gets the type associated with the specified class identifier (CLSID) from the specified server.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetTypeFromCLSID ( _
	clsid As Guid, _
	server As String _
) As Type

Parameters

clsid
Type: System.Guid

The CLSID of the type to get.

server
Type: System.String

The server from which to load the type. If the server name is Nothing, this method automatically reverts to the local machine.

Return Value

Type: System.Type
System.__ComObject regardless of whether the CLSID is valid.

The GetTypeFromCLSID method supports late-bound access to COM objects from .NET Framework apps when you know the object's class identifier (CLSID). The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. You can retrieve the value of the IsCOMObject property to determine whether the type returned by this method is a COM object.

TipTip

You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

Instantiating a COM object from its CLSID is a two-step process:

  1. Get a Type object that represents the __ComObject that corresponds to the CLSID by calling the GetTypeFromCLSID method.

  2. Call the Activator.CreateInstance(Type) method to instantiate the COM object.

Notes to Callers

This method is intended for use when working with COM objects, not with .NET Framework objects. Although the method returns a Type object that corresponds to the GUID for .NET Framework objects, you can't use that Type object to create a type instance by calling the Activator.CreateInstance method, as the following example shows.

Imports System.Runtime.InteropServices

<Assembly:ComVisible(True)>

' Define two classes, and assign one an explicit GUID.
<GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")>
Public Class ExplicitGuid
End Class 

Public Class NoExplicitGuid
End Class 

Module Example
   Public Sub Main()
      Dim explicitType As Type = GetType(ExplicitGuid)
      Dim explicitGuid As Guid = explicitType.GUID

      ' Get type of ExplicitGuid from its GUID. 
      Dim explicitCOM As Type = Type.GetTypeFromCLSID(explicitGuid)
      Console.WriteLine("Created {0} type from CLSID {1}",
                        explicitCOM.Name, explicitGuid)

      ' Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        explicitType.Name, explicitCOM.Name,
                        explicitType.Equals(explicitCOM))                  

      ' Instantiate an ExplicitGuid object. 
      Try  
         Dim obj As Object = Activator.CreateInstance(explicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try  

      Dim notExplicit As Type = GetType(NoExplicitGuid)
      Dim notExplicitGuid As Guid = notExplicit.GUID

      ' Get type of ExplicitGuid from its GUID. 
      Dim notExplicitCOM As Type = Type.GetTypeFromCLSID(notExplicitGuid)
      Console.WriteLine("Created {0} type from CLSID {1}",
                        notExplicitCOM.Name, notExplicitGuid)

      ' Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        notExplicit.Name, notExplicitCOM.Name,
                        notExplicit.Equals(notExplicitCOM))                  

      ' Instantiate an ExplicitGuid object. 
      Try  
         Dim obj As Object = Activator.CreateInstance(notExplicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try  
   End Sub 
End Module 
' The example displays the following output: 
'       Created __ComObject type from CLSID d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4 
'       ExplicitGuid and __ComObject equal: False 
'       COM Exception: 
'       Retrieving the COM class factory for component with CLSID  
'       {D055CBA3-1F83-4BD7-BA19-E22B1B8EC3C4} failed due to the following error:  
'       80040154 Class not registered  
'       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 
'        
'       Created __ComObject type from CLSID 74f03346-a718-3516-ac78-f351c7459ffb 
'       NoExplicitGuid and __ComObject equal: False 
'       COM Exception: 
'       Retrieving the COM class factory for component with CLSID  
'       {74F03346-A718-3516-AC78-F351C7459FFB} failed due to the following error:  
'       80040154 Class not registered  
'       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

The following example uses the CLSID of the Microsoft Word Application object to retrieve a COM type that represents the Microsoft Word application from a server named computer17.central.contoso.com. It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method.

Imports System.Reflection
Imports System.Runtime.InteropServices

Module Example
   Private Const WORD_CLSID As String = "{000209FF-0000-0000-C000-000000000046}" 

   Public Sub Main()
      ' Start an instance of the Word application. 
      Dim word As Type = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), "computer17.central.contoso.com")
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID)
      Try 
         Dim wordObj As Object = Activator.CreateInstance(word)
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName)

         ' Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, 
                           wordObj, New Object() { 0, 0, False } )
      Catch e As COMException
         Console.WriteLine("Unable to instantiate object.")
      End Try 
   End Sub 
End Module 
' The example displays the following output: 
'    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046} 
'    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft