ArcObjects Library Reference  

AddEXTCLSID

About the Tabbed feature inspector Sample

[C#]

AddEXTCLSID.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Framework;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.esriSystem;

namespace TabbedFeatureInspector
{
  /// <summary>
  /// Summary description for AddEXTCLSID.
  /// </summary>
  [Guid("ffa4371f-59ca-4545-99db-8580a7445b12")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("TabbedFeatureInspector.AddEXTCLSID")]
  public sealed class AddEXTCLSID : BaseCommand
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);

      //
      // TODO: Add any COM registration code here
      //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType);

      //
      // TODO: Add any COM unregistration code here
      //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Register(regKey);

    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Unregister(regKey);

    }

    #endregion
    #endregion

    private IApplication m_application;
    public AddEXTCLSID()
    {

      base.m_category = "Developer Samples"; 
      base.m_caption = "Add EXTCLSID";  
      base.m_message = "This command adds the GUID of the project to the EXTCLSID";  
      base.m_toolTip = "Adds EXTCLSID to feature class";  
      base.m_name = "TabbedFeatureInspector_AddEXTCLSID";   

      try
      {
         string bitmapResourceName = GetType().Name + ".bmp";
        base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this command is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (hook == null)
        return;

      m_application = hook as IApplication;

      //Disable if it is not ArcMap
      if (hook is IMxApplication)
        base.m_enabled = true;
      else
        base.m_enabled = false;     
    }

    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
      try
      {
        IGxDialog gxDialog = new GxDialogClass();
        IGxObjectFilter mdbFilter = new GxFilterPGDBFeatureClasses();
        gxDialog.ObjectFilter = mdbFilter;
        object temp = "C:\\";
        gxDialog.set_StartingLocation(ref temp);
        gxDialog.Title = "Pick the feature class you want to add the ext clsid to";
        gxDialog.RememberLocation = true;
        IEnumGxObject pEnumGx;
      
        if (!gxDialog.DoModalOpen(0, out pEnumGx))
          return;
     
        IGxObject gdbObj = pEnumGx.Next();
        //Make sure there was only one GxObject in the enum.
        if (pEnumGx.Next() != null) 
          return;
        //Get the Name for the internal object that this GxObject represents.
        IName fcName  = gdbObj.InternalObjectName;
        //Opens the object referred to by this name
        IFeatureClass featClass = (IFeatureClass)fcName.Open();
        //Procedure to add the class id to the feature class internally.
        IClassSchemaEdit_Example(featClass);
        }
        catch (Exception ex)
        {
          MessageBox.Show("Error: Could open the feature class. Original error: " + ex.Message);
        }
      }
    
    public void IClassSchemaEdit_Example(IObjectClass objectClass) 
    {        
      //This function shows how you can use the IClassSchemaEdit   
      //interface to alter the COM class extension for an object class.    
      //cast for the IClassSchemaEdit      
      IClassSchemaEdit classSchemaEdit = (IClassSchemaEdit)objectClass;
        //set and exclusive lock on the class     
      ISchemaLock schemaLock = (ISchemaLock)objectClass;  
      schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
        ESRI.ArcGIS.esriSystem.UID classUID = new ESRI.ArcGIS.esriSystem.UIDClass();
      //GUID for the C# project.
        classUID.Value = "{65a43962-8cc0-49c0-bfa3-015d0ff8350e}";
        classSchemaEdit.AlterClassExtensionCLSID(classUID, null);    
      //release the exclusive lock     
      schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);  
    }
    #endregion
  }
}

[Visual Basic .NET]

AddEXTCLSID.vb

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.CatalogUI
Imports ESRI.ArcGIS.Catalog
Imports ESRI.ArcGIS.esriSystem
<ComClass(AddEXTCLSID.ClassId, AddEXTCLSID.InterfaceId, AddEXTCLSID.EventsId), _
 ProgId("TabbedFeatureInspectorVBNet.AddEXTCLSID")> _
Public NotInheritable Class AddEXTCLSID
  Inherits BaseCommand

#Region "COM GUIDs"
  ' These  GUIDs provide the COM identity for this class 
  ' and its COM interfaces. If you change them, existing 
  ' clients will no longer be able to access the class.
  Public Const ClassId As String = "6d73c609-8f19-4231-81a1-ba1c6b40ff12"
  Public Const InterfaceId As String = "98a7646a-d5ff-4424-a279-9626f1b83d81"
  Public Const EventsId As String = "5a687ef0-b670-4b4b-b072-199612f862f9"
#End Region

#Region "COM Registration Function(s)"
  <ComRegisterFunction(), ComVisibleAttribute(False)> _
  Public Shared Sub RegisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryRegistration(registerType)

    'Add any COM registration code after the ArcGISCategoryRegistration() call

  End Sub

  <ComUnregisterFunction(), ComVisibleAttribute(False)> _
  Public Shared Sub UnregisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryUnregistration(registerType)

    'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

  End Sub

#Region "ArcGIS Component Category Registrar generated code"
  Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    MxCommands.Register(regKey)

  End Sub
  Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    MxCommands.Unregister(regKey)

  End Sub

#End Region
#End Region

  Private m_application As IApplication

  ' A creatable COM class must have a Public Sub New() 
  ' with no parameters, otherwise, the class will not be 
  ' registered in the COM registry and cannot be created 
  ' via CreateObject.
  Public Sub New()
    MyBase.New()
    MyBase.m_category = "Developer Samples"
    MyBase.m_caption = "Add EXTCLSID"
    MyBase.m_message = "This command adds the GUID of the project to the EXTCLSID"
    MyBase.m_toolTip = "Adds EXTCLSID to feature class"
    MyBase.m_name = "TabbedFeatureInspectorVBNet_AddEXTCLSID"
   
    Try
      Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
      MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
    End Try

  End Sub


  Public Overrides Sub OnCreate(ByVal hook As Object)
    If Not hook Is Nothing Then
      m_application = CType(hook, IApplication)

      'Disable if it is not ArcMap
      If TypeOf hook Is IMxApplication Then
        MyBase.m_enabled = True
      Else
        MyBase.m_enabled = False
      End If
    End If
  End Sub

  Public Overrides Sub OnClick()
    Try
      Dim pGxdialog As IGxDialog = New GxDialogClass()
      Dim mdbFilter As IGxObjectFilter = New GxFilterPGDBFeatureClasses
      pGxdialog.Title = "Pick the feature class you want to add the ext clsid to"
      Dim pEnumGx As IEnumGxObject
      pGxdialog.ObjectFilter = mdbFilter
      pGxdialog.StartingLocation = "c:\"
      If Not pGxdialog.DoModalOpen(0, pEnumGx) Then
        Exit Sub
      End If

      Dim gdbObj As IGxObject = pEnumGx.Next
      'Make sure there was only one GxObject in the enum.
      If Not pEnumGx.Next Is Nothing Then
        Return
      End If
      'Get the Name for the internal object that this GxObject represents.
      Dim fcName As IName = gdbObj.InternalObjectName
      'Opens the object referred to by this name
      Dim featClass As IFeatureClass = fcName.Open
      'Procedure to add the class id to the feature class internally.
      IClassSchemaEdit_Example(featClass)
    Catch Ex As AccessViolationException
            MessageBox.Show("Attempt to read or write protected memory. Original error: " & Ex.Message)
    Catch sEx As Exception
      MessageBox.Show("Cannot read file from disk. Original error: " & sEx.Message)
    End Try
  End Sub

  Public Sub IClassSchemaEdit_Example(ByVal featClass As IObjectClass)
    'This function shows how you can use the IClassSchemaEdit   
    'interface to alter the COM class extension for an object class.    
    'cast for the IClassSchemaEdit      
    Dim classSchemaEdit As IClassSchemaEdit = CType(featClass, IClassSchemaEdit)
    'set and exclusive lock on the class     
    Dim schemaLock As ISchemaLock = CType(featClass, ISchemaLock)
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock)
    Dim classUID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
    'GUID for the VBNet project.
    classUID.Value = "{2cfe1569-8732-4e73-ac8b-31b87be9631b}"
    classSchemaEdit.AlterClassExtensionCLSID(classUID, Nothing)
    'release the exclusive lock     
    schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock)
    MessageBox.Show("Successfully added the extension class id to the feature class")
  End Sub
End Class