Table of contents
1.
Introduction
2.
CComBSTR Class
3.
Methods of CComBSTR Class
4.
Issues
4.1.
Conversion Issues
4.2.
Scope Issues
4.3.
Memory Leak Issues
5.
Frequently Asked Questions
5.1.
What is ATL?
5.2.
What is CComBSTR class?
5.3.
What is an ATL application?
5.4.
What is the benefit of using ATL?
5.5.
What is COM?
6.
Conclusion
Last Updated: Mar 27, 2024

Programming with CComBSTR in ATL

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

Introduction

Window development used to be very difficult. This is because we have to rewrite the same codes again and again. Then Microsoft Foundation Class (MFC) came into existence. Using MFC, developers can save their codes and use them when required. Thus, MFC saves a lot of time previously wasted in rewriting. But everything cannot be saved. Developers have to write some codes from scratch as well. And so, Active Template Library(ATL) comes in handy. The ATL window class is a mediator between writing code from scratch and MFC. 

Introduction

This article will discuss CComBSTR in ATL. We will also discuss several issues associated with CComBSTR. 

CComBSTR Class

Com stands for Component Object Model. It is a technique for exchanging binary code between programs and languages. To use all the methods of CComBSTR class, we must include the ‘atlcomcli.h’ file. The CComBSTR class is a special utility ATL class that helps encapsulate TR. The basic definition of CComBSTR class contains only one member variable, i.e., a string of BSTR type. The definition of this class is as follows - 

class CComBSTR {                                    
public:                                             
  BSTR my_string;                                       
...                                                 
};


The BSTR is a COM string data type. It stands for Binary String or Basic String.  Is a pointer that points to the first character of the data string. The COM model uses it in Interoperable and Automation functions. BSTR consists of three components:-

  • Length prefix - It appears before the first character of the data string. The length prefix specifies the number of bytes in the data string. The debugger automatically examines the value of the length prefix. 
     
  • Data String - A series or string of characters. It can also contain multiple null characters. 
     
  • Terminator - This specifies the end of the entity or data string. It is a NULL character. 

Methods of CComBSTR Class

The CComBSTR class provides a number of public methods. These are listed below:-

Method 

Description

AppendBSTR Appends a given BSTR to my_string.
AppendBytes Append the given no. of bytes to my_string.
ArrayToBSTR Create an array containing the first character of each element of BSTR.
AssignBSTR Assign a given BSTR to my_string.
Attach This method attaches the object of the CComBSTR class to BSTR.
Append To append a given string to my_string. 
ByteLength This method provides the length of my_string in bytes.
Copy This method creates a copy of my_string. 
Detch This method detaches the object of CComBSTR class from my_string. 
Length To get the length of my_string, the length function is used. 
ReadFromStream To read the BSTR object from the stream. 
ToLower Convert the whole string to Lower case. 
ToUpper Convert the whole string to Upper case.
WriteToStream Write the my_string to a stream.
BSTRToArray Create an array containing the characters from the CComBSTR object. 

Issues

There are several issues associated with CComBSTR class. Thee are addressed below:-

Conversion Issues

Conversion is also known as typecasting or type conversion. Conversion refers to changing the datatype of an entity. In CComBSTR class, a few methods automatically convert the ANSI strings to Unicode. These methods also return Unicode format strings. The issue arises when we have to convert back these Unicode strings to ANSI format. For that, we have to use the ATL conversion class method.

Conversion

This method creates a copy of the old structure and converts the string to the new structure. The ATL Conversion method used for this is - MACRONAME.

Scope Issues

In a program using CComBSTR class, if a method returns a pointer to the CComBSTR string, then the program will run into errors because CComBSTR class frees the resources which are out of scope. And, so when a pointer points to a freed resource, scope issues will arise. The problem can be solved using the 'Copy' method. We can create a copy of the pointer that can be used while returning. Let's understand this using an example:-

BSTR* wrong_Function()
{
   /*
       Create object of CComBSTR class
   */
   CComBSTR obj(L"Coding Ninjas");
   
   /*
      Convert the string to lowercase
   */
   HRESULT result = obj.ToLower();

   /*
       Wrong way to return string
       Return a pointer to the BSTR
   */
   return &obj;
}

HRESULT my_function (BSTR* pointer)
{
   /*
      Create the object of CComBSTR class
   */
   CComBSTR obj(L"Coding Ninjas");

   /*
      Convert the string to lowercase
   */
   HRESULT result = obj.ToLower();
   
   /* 
     Check and return the result
   */
   if (result != S_OK)
       return result;

   /*
      Correct way o return
      Return copy of string
   */
   return obj.CopyTo(pointer);
}

Memory Leak Issues

The object of CComBSTR class allocates memory internally. So, if we pass a memory where CComBSTR entities are initialized to a function as an ‘OUT’ parameter, then it would lead to memory leak issues. To avoid this issue, you can call the ‘EMPTY’ method before passing the memory address to exiting objects of CComBSTR class. 

Frequently Asked Questions

What is ATL?

ATL stands for Active Template Library. It is a library containing numerous classes that can be used to manipulate and develop Windows.

What is CComBSTR class?

The CComBSTR class is a special utility ATL class that helps encapsulate BSTR. The basic definition of CComBSTR class contains only one member variable, i.e., a string of BSTR type.

What is an ATL application?

Microsoft created a collection of template-based C++ classes called the Active Template Library (ATL) to make programming Component Object Model (COM) objects easier. Developers can create different COM objects, OLE Automation servers, and ActiveX controls thanks to the COM support in Microsoft Visual C++.

What is the benefit of using ATL?

We need ATL to provide efficient and easy-to-implement COM objects.

What is COM?

COM is a platform-independent, object-oriented, and distributed system. It is used for creating interactive binary software components. It is the foundation technology for Microsoft's ActiveX and OLE technologies.

Conclusion

In this article, we have discussed CComBSTR class. We enlisted the useful methods of CComBSTR class. After that, we discussed various issues associated with this class.

Do not stop learning! We recommend you read these articles -

  1. Introduction to ATL Window Classes
  2. ATL Control Containment
  3. ATL Module Classes
     

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 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.

Keep learning, and Keep Growing!

Do upvote our blogs if you find them engaging and knowledgeable.

Live masterclass