Creating a Base Class

[This is preliminary documentation and subject to change.]

Classes can be created using the Managed Object Format (MOF) language or the CIMOM COM API. To create a class with MOF, see Declaring Classes.

To create a class with the COM interfaces of the CIMOM API, use code similar to the following sample. This sample creates a class called Example. The g_pNamespace pointer is an IWbemServices pointer bound to the Root\Default namespace and obtained from IWbemLocator::ConnectServer. Some of the error-checking code is omitted for clarity:

void CreateClass()
{
  IWbemClassObject *pNewClass = 0;
  HRESULT hRes = g_pNamespace->GetObject(0, 0, &pNewClass, 0);
  VARIANT v;
  VariantInit(&v);

  // Create the class name
  // =====================

  V_VT(&v) = VT_BSTR;
  V_BSTR(&v) = SysAllocString(L"Example");
  BSTR Class = SysAllocString(L"__CLASS");
  pNewClass->Put(Class, 0, &v);
  SysFreeString(Class);
  VariantClear(&v);

  // Create the key property
  // =======================

  V_VT(&v) = VT_BSTR;
  V_BSTR(&v) = SysAllocString(L"<default>");
  BSTR KeyProp = SysAllocString(L"Index");
  pNewClass->Put(KeyProp, 0, &v);
  VariantClear(&v);
  SysFreeString(KeyProp);

  // Mark the "Index" property as the 'key'.
  // =======================================
  IWbemQualifierSet *pQual = 0;
  pNewClass->GetPropertyQualifierSet(KeyProp, &pQual);

  V_VT(&v) = VT_BOOL;
  V_BOOL(&v) = VARIANT_TRUE;
  BSTR Key = SysAllocString(L"Key");

  pQual->Put(Key, 0, &v);   // Qualifier flavors not required for KEY 
  SysFreeString(Key);

  pQual->Release();   // No longer need the qualifier set for "Index"  
  VariantClear(&v);

  // Create another property
  // =======================
  V_VT(&v) = VT_BSTR;
  V_BSTR(&v) = SysAllocString(L"<default>");
  BSTR OtherProp = SysAllocString(L"OtherInfo");
  pNewClass->Put(OtherProp, 0, &v, NULL);
  SysFreeString(OtherProp);
  VariantClear(&v);

  // Register the class with CIMOM
  // ============================
  hRes = g_pNamespace->PutClass(pNewClass, 0, 0);
  pNewClass->Release();
}

Note  The class name is specified by setting the system property __CLASS to Example. Because properties marked as keys or indexes cannot be changed after a class has been created, it is important to specify key and index properties by calling IWbemServices::PutClass or IWbemServices::PutClassAsync before creating the class.