6 Replies Latest reply on May 19, 2015 6:53 AM by Neil Etheridge

    How do I get the GUID of a COM Object in C#?

    Neil Etheridge

      I am writing a geoprocessing tool to assign a validation rule to a class.  To do the assignment you need to know the GUID of the specific rule you want to apply.  In the tool I want to present the list of available Validation Rules in a dropdown list - with the Validation Rule name displayed and the GUID as the returned value.  It is possible to do this by using the Esri CategoryFactoryClass.

       

      However I have hit a problem ... when I call comObject.GetType().GUID (line 12 below) it returns the GUID as {00000000-0000-0000-0000-000000000000}.  If the validation rule was developed in .Net then there is no problem and the correct GUID is returned.

       

      Does anyone know how to query a COM object and return the GUID?

       

      SortedDictionary<string, string> valRules = new SortedDictionary<string, string>();
      IUID valRulesCategoryUID = new UIDClass();
      valRulesCategoryUID.Value = MMValidationRules.CatID;
      ESRI.ArcGIS.esriSystem.ICategoryFactory valRulesCategoryFactory = new ESRI.ArcGIS.esriSystem.CategoryFactoryClass();
      valRulesCategoryFactory.CategoryID = (UID)valRulesCategoryUID;
      object vrObj = valRulesCategoryFactory.CreateNext();
      while (vrObj != null) {
          if (vrObj is IMMValidationRule) {
              IMMValidationRule vr = vrObj as IMMValidationRule;
              IMMExtObject eo = vrObj as IMMExtObject;
              string vrName = eo.Name;
              string vrGuid = string.Format("{{{0}}}", vr.GetType().GUID.ToString());
              valRules.Add(vrName, vrGuid);
          }
          vrObj = valRulesCategoryFactory.CreateNext();
      }
      

       

       

      Many thanks,

      Neil