Creating a Derived Class

[This is preliminary documentation and subject to change.]

Creating a derived class is similar to creating a base class. The main difference is that before the class is created, the parent class must be located as shown in the following fragment. The class Example2 is created as a subclass to the Example class.

IWbemClassObject *pNewDerivedClass = 0;
  IWbemClassObject *pExampleClass = 0;

  BSTR PathToClass = SysAllocString(L"Example");
  HRESULT hRes = g_pNamespace->GetObject(PathToClass, 0, 
    &pExampleClass, 0);

  SysFreeString(PathToClass);

  if (hRes != 0)
    return;

  pExampleClass->SpawnDerivedClass(0, &pNewDerivedClass);
  pExampleClass->Release();  // Don't need the superclass any more

  VARIANT v;
  VariantInit(&v);

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

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

  // Create another property
  // =======================
  V_VT(&v) = VT_BSTR;
  V_BSTR(&v) = SysAllocString(L"<default>");
  BSTR OtherProp = SysAllocString(L"OtherInfo2");
  pNewDerivedClass->Put(OtherProp, 0, &v);

  SysFreeString(OtherProp);
  VariantClear(&v);

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