Table of contents
1.
Introduction
2.
Memory Management Class
2.1.
CComPtr Class
2.1.1.
What is COM
2.1.2.
What is COM Interface
2.1.3.
Constructor
2.1.4.
Assignment Operator
2.1.5.
Implementation
2.2.
CComPtrBase Class 
2.2.1.
Public Methods
2.2.2.
Public Operators
3.
MMC Snap-In Classes
3.1.
What is MMC?
3.2.
MMC Snap-In Classes
3.2.1.
CSnapInItemImpl  Class
3.2.2.
CSnapInPropertyPageImpl
4.
Object Safety Classes
4.1.
Understanding IObjectSafety Interface
5.
Registry Support Classes
5.1.
Functions of CRegKey
6.
Running Objects Classes
6.1.
IRunnableObjectImpl Class
7.
Frequently Asked Questions
7.1.
What are object safety classes?
7.2.
What are Registry support classes?
7.3.
What is COM?
7.4.
What is an interface in computer programming?
7.5.
What is a registry?
8.
Conclusion
Last Updated: Mar 27, 2024
Hard

Object Safety Classes

Author Vaibhav Mani
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello, ninjas. Today we are going to study object safety classes. Earlier, we studied classes and objects in C++, and now we will be learning about this new concept known as object safety classes along with other concepts. An object is marked as safe to initialize or for scripting accordingly. We will be learning other concepts too related to this topic. Let's get started.

Introduction

Memory Management Class

Usually, In c++, we manage memory by allocating new and deleting the allocated memory. We call it dynamic memory allocation. In this, we will learn about these classes and how they manage memory in c++.

In general, these classes, such as CComPtr and CComPtrBase, provide support for heap pointers, smart pointers, and other memory management requirements.

Smart pointers

We know about the 'delete' and 'new' keywords used in our C codes. New is used to allocate a new memory in a heap, and delete is used to delete or free up the memory created in a heap.

Smart pointers are a way to automate this process, which means when we call a new memory allocation, we don't have to call for the delete because smart pointers do it automatically. They are a wrapper around actual raw pointers.

CComPtr Class

CComPtr class is a smart pointer class for managing COM interface pointers. This class manages the COM interface pointers. What are COM interface pointers? Understanding first what COM is. 

What is COM

The full form of COM is the Component Object Model (COM). It is a binary-interface standard for software components.It allows inter-process communication object creation in a large range of programming languages. Hence, it is used in c++ also. Microsoft introduced COM in 1993.

What is COM Interface

Com Class implements a predefined group of related functions and doesn't need to represent all functions that offer a particular class. This is known as the COM interface.

The interface is the nearest equal to a pure virtual class; it contains only pure virtual methods and no other members. By convention, interface names start with "I."

Here is an example of an interface:

interface IDriveable
{
    void Drive();
}


The IDriveable interface defines the operations that any driveable object must support. In this example, the IDriveable interface defines a single operation: Drive.

CComPtr Class - It is a smart pointer class for managing COM interface pointers. It is derived from CComPtrBase class and does automatic reference counting. It helps in preventing memory leakage by doing reference counting.

Constructor

CComPtr() throw ();

Assignment Operator

CComPtr::operator =

Implementation

// Making a function which has return type success or failure of the function
HRESULT CheckComBug()
{
	// Declaration of the variable of HRESULT which return success or error
	HRESULT bg;
	CComBSTR bstrInformation;
	CComBSTR bstrSource;
	CComBSTR bstrHelp;
	
	// Making use of CComPtr class, IBugInfo is the interface
	CComPtr<IBugInfo> pBugInfo;
	
	// Assigning value of the method GetErrorInfo to bg
	bg = ::GetErrorInfo(0, &pBugInfo);
	
	// Checking if bg is success or failure S_OK = success
	if (bg != S_OK)
		return bg;
		
	// Assigning value of the method GetDescription to bg
	bg = pBugInfo->GetDescription(&bstrInformation);
	if (FAILED(bg))
	{
		// If failed then decrease the reference count for an interface of an object.
		pBugInfo->Release();
		return bg;
	}
	
	// Assigning the value of the method GetSource to bg
	bg = pBugInfo->GetSource(&bstrSource);
	if (FAILED(bg))
	{
		// If failed, then decrease the reference count for an interface of an object.
		pBugInfo->Release();
		Return bg;
	}
	
	// Assigning a value of the method GetHelpFile to bg
	bg = pBugInfo->GetHelpFile(&bstrHelp);
	if (FAILED(bg))
	{
		// If failed, then decrease the reference count for an interface of an object.
		pBugInfo->Release();
		return bg;
	}

	pBugInfo->Release();
	
	// Returning the overall function type
	return S_OK;
}

CComPtrBase Class 

This class serves as the basis for classes that use smart pointers and COM-based memory routines. Other smart pointers that make use of COM memory management are built on the basis of this class. The derived classes also have new constructors and operators of their own.

Public Methods

This has various public methods.

Name Description
CComPtrBase::Advise This method creates a connection between the CComPtrBase connection point and a client's sink.
CComPtrBase::Attach This method takes ownership of an existing pointer.
CComPtrBase::CoCreateIinstance This method creates an object of the class associated with a specified Class ID or Program ID.
CComPtrBase::CopyTo This method copies the CComPtrBase pointer to another pointer variable.
CComPtrBase::Detach This method releases the right of the pointer.
CComPtrBase::IsEqualObject This method checks if the specified IUnknown points to the same object associated with the CComPtrBase object.

Public Operators

This has various public operators.

Name Description
CComPtrBase::operator T* The cast operator.
CComPtrBase::operator ! The NOT operator.
CComPtrBase::operator & The address-of & operator.
CComPtrBase::operator * The pointer-to * operator.
CComPtrBase::operator < The less-than operator.
CComPtrBase::operator == The equality operator.
CComPtrBase::operator -> The pointer-to-members operator.

MMC Snap-In Classes

Ninjas now talking about MMC snap-in classes, but before moving to MMC snap-in classes we will understand what is MMC.

What is MMC?

MMC stands for Memory Management Classes. Their function is to provide support for heap pointers, smart pointers, and other memory allocation routines.
Some of these classes are mentioned below, along with their functions.

  • CAutoPtr   A smart pointer class that represents a smart pointer object.
     
  • CAutoPtrList   A smart pointer class that provides helpful methods when creating a list of smart pointers.
     
  • CAutoVectorPtr A smart pointer class that depicts a smart pointer object which operates on vector new and deletes operators.
     
  • CComAllocator A smart pointer class that provides methods for handling memory using COM memory routines.
     
  • CComHeapPtr A smart pointer class that manages heap pointers.
     
  • CComPtr A smart pointer class that controls COM interface pointers.
     
  • CComPtrBase A smart pointer class that provides a basis for smart pointer classes using COM-based memory routines.
     
  • CComQIPtr A smart pointer class that manages COM interface pointers.

MMC Snap-In Classes

In MMC snap-in classes, MMC has a common framework and a snap-in is a tool that is hosted in MMC\. It has various snap-ins that can run to manage many services by operating on a single interface. 

The below classes provide help for growing Microsoft Management Console (MMC) snap-in components: 

  • CSnapInItemImpl Executes a snap-in node object, such as adding items and toolbars, etc.
     
  • CSnapInPropertyPageImpl Executes a snap-in property page object.
     

CSnapInItemImpl  Class

This class gives us methods for executing a snap-in node object.

This class has many public methods, some are tabulated below

Name Description
CSnapInItemImpl::AddMenuItems This method adds menu items to a context menu.
CSnapInItemImpl::Command This method is called by the console when a custom menu item is selected.
CSnapInItemImpl::CreatePropertyPages This method adds pages to the property sheet of the snap-in.
CSnapInItemImpl::FillData This method copies the information on the snap-in object into a specified stream.
CSnapInItemImpl::Notify This method is called by the console to notify the snap-in of actions taken by the user.

CSnapInPropertyPageImpl

This class gives us methods for executing a snap-in property page object.

This class has many public methods, some are tabulated below

Name Description
CSnapInPropertyPageImpl::Create This method initializes a newly created object.
CSnapInPropertyPageImpl::OnApply This method calls the framework when the user clicks the Apply Now button while using a wizard-type property sheet.
CSnapInPropertyPageImpl::OnHelp This method is called by the framework when the user clicks the Help button while using a wizard-type property sheet.
CSnapInPropertyPageImpl::OnKillActive This method is called by the framework when the current page is no longer active.
CSnapInPropertyPageImpl::OnQueryCancel This method is called by the framework when the user clicks the Cancel button and before the cancel has taken place.
CSnapInPropertyPageImpl::OnReset This method is called by the framework when the user clicks the Reset button while using a wizard-type property sheet.

Object Safety Classes

The classes under this topic provide support for object safety means a particular object should be initialized or not, is it safe to do so? That’s why it is called object safety classes.
One of the classes that come under this is IObjectSafetyImpl Class.

IObjectSafetyImpl Class

  • Object safety classes. Helps in the default implementation of the IObjectSafety Interface. 
     
  • The IObjectSafety interface of object safety classes. Allows a client to retrieve and set an object's safety levels.
     
  • For example, a web browser can call IObjectSafety::SetInterfaceSafetyOptions to check if the object is safe to initialize or not or safe for scripting.
     

Some of the object safety classes. Public methods are listed below.

Name Description
IObjectSafetyImpl::GetInterfaceSafetyOptions Checks for the safety options supported by the object and also for the safety options currently set for the object.
IObjectSafetyImpl::SetInterfaceSafetyOptions Helps in making the object safe for initialization or scripting.

Understanding IObjectSafety Interface

  •  The IObjectSafety interface of object safety classes allows clients to recover and set an object's easy levels.
     
  •  For example, That’s a web browser called IObjectSafety::SetInterfaceSafetyOptions to check if the object is safe to initialize or not or safe for scripting.
     
  •  The IObjectSafety provides object safety classes; objects should execute it with interfaces that support "untrusted" clients; allow scripts.

Registry Support Classes

  • Registry support classes are a kind of object safety class that gives us the collection of standard keys found in the registry on devices operating on Windows.
     
  • The registry is the storage of information about users, default system settings, etc.
      
  • For example, applications use the registry for storing data; if the application gets closed, data is conserved.
     
  • It works for reserving color choices, screen locations, or window sizes.
     

The registry class uses some keys, which are read-only types. Some keys are given below.

  • CurrentUser is used to store data about user preferences.
     
  • LocalMachine is used to configure data for the local machine.
     
  • ClassesRoot is used to store data about types (and classes) and their properties.
     
  • Users is used to storing data regarding the default user configuration.
     
  • PerformanceData is used to store performance information for software components.
     
  • CurrentConfig is used to store non-user-specific hardware information.
     
  • DynkData is used to store dynamic data.
     

The class provides registry support: CRegKey class provides methods for manipulating values in the system registry.

Functions of CRegKey

  • CRegKey provides methods for manipulating entries in the system registry.
     
  • It gives us methods on how to add and remove keys and values from the system registry.
     
  • It provides a programming interface to the system registry for a given application.
     
  • Taking an example to open a particular registry key, we execute the CRegKey::Open. 
     
  • To open or modify a data value, call CRegKey::QueryValue or CRegKey::SetValue, respectively.
     
  • To close a key, call CRegKey::Close.
     

Some of its public methods are listed below.

Name Description
CRegKey::DeleteSubKey This method removes the specified key from the registry.
CRegKey::DeleteValue This method eliminates a value field from m_hKey.
CRegKey::EnumKey This method enumerates the subkeys of the open registry key.
CRegKey::Flush This method writes all of the attributes of the open registry key into the registry.
CRegKey::GetKeySecurity This method checks for a copy of the security descriptor and saves the open registry key.
CRegKey::NotifyChangeKeyValue This method alerts the caller about changes to the attributes or contents of the open registry key.
CRegKey::Open This method opens the specified key and sets m_hKey to the handle of this key.

Running Objects Classes

A running object class is a type of object safety class that supports running objects. Running objects are those which are currently active in a particular program. One of its classes is IRunnableObjectImpl.

Running Objects Classes

IRunnableObjectImpl Class

This class is a part of running object classes; it determines if an object is running in a program, forces it to run, or locks it into the running state.

Some of its public methods are listed below.

Name Description
IRunnableObjectImpl::IsRunning It checks if the control is running and returns TRUE.
IRunnableObjectImpl::LockRunning It locks the control into the running state and returns S_OK.
IRunnableObjectImpl::Run It forces the control to run and returns S_OK.
IRunnableObjectImpl::SetContainedObject It indicates that the control is embedded. The ATL implementation returns S_OK.

Frequently Asked Questions

What are object safety classes?

The object safety classes are the classes that tell whether a particular object should be initialized or not.

What are Registry support classes?

Registry support classes are object safety classes that contain standard root keys found in the registry on devices operating on Windows.

What is COM?

A binary-interface standard for software components called COM (Component Object Model) was first introduced by Microsoft in 1993. It allows inter-process communication object creation in an extensive range of programming languages. Hence, it is used in C++ also.

What is an interface in computer programming?

In programming, an interface is a shared boundary across which two or more pieces of information are exchanged. This exchange can be between software, computer hardware, peripheral devices, humans, and combinations.

What is a registry?

The registry stores information about applications, users, default system settings, etc.  

Conclusion

We have studied different classes of object safety in C++. Studied various classes and their derived classes. We learned and Understood other methods related to each class. And also read about their functioning.

Refer to the Below topics for more understanding

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass