StringCchCat Function example

StringCchCat is used to concatenate one string to another string. It is also important to remember that the Strsafe functions, such as StringCchCopy() and StringCchCat(), do not have the same semantics as the strncpy_s() and strncat_s() functions. When strncat_s() detects an error, it sets the destination string to a null string while StringCchCat() fills the destination with as much data as possible, and then null-terminates the string.

Operation: Concatenates one striing to another string.

Syntax:

HRESULT StringCchCat(_Inout_ LPTSTR  pszDest,
                     _In_    size_t  cchDest,
                     _In_    LPCTSTR pszSrc
                    );

Arguments:

  • pszDest: Destination buffer to which pszSrc will be concatenated.
  • cchDest: cch stands for count of characters. This argument is mainly provided so that we will not be writing after the end of buffer.
  • pszSrc: Null terminated string which you want to concatenate.

Header File:

#include <strsafe.h>

Return Value:

Type: HRESULT 
S_OK    If successful

We will see the other return values also.

Sample Code 1

Consider the example shown below:

#include "stdafx.h"
#include "stdlib.h"
#include "winerror.h"
#include "windows.h"
#include "strsafe.h"

int _tmain(int argc, _TCHAR* argv[])
{
 TCHAR pszDest[100]= _T("Hello");
 TCHAR pszSrc[100] = _T("World");
 HRESULT hresult;

 hresult = StringCchCat(pszDest, _countof(pszDest), pszSrc);

 if (SUCCEEDED(hresult))
 {
  printf("String Succesfully concatenated:%ls\n", pszDest);
 }
 else
 {
  printf("Concatenation Failed:%d\n", GetLastError());
 }
 return 0;
}

O/P:

String Successfully concatenated:HelloWorld

What happens if I passed 0 as the second argument in the StringCchCat function.

Sample Code 2

Consider the example shown below:

#include "stdafx.h"
#include "stdlib.h"
#include "winerror.h"
#include "windows.h"
#include "strsafe.h"

int _tmain(int argc, _TCHAR* argv[])
{
 TCHAR pszDest[100]= _T("Hello");
 TCHAR pszSrc[100] = _T("World");
 HRESULT hresult;

 hresult = StringCchCat(pszDest, 0, pszSrc);

 if (SUCCEEDED(hresult))
 {
  printf("String Succesfully concatenated:%ls\n", pszDest);
 }
 else
 {
  if (hresult == STRSAFE_E_INVALID_PARAMETER)
   printf("Invalid Parameter Passed\n");
  else if(hresult == STRSAFE_E_INSUFFICIENT_BUFFER)
   printf("Destination Size not sufficient\n");
  else
   printf("Some unknown error happened\n");
 }
 return 0;
}

O/P:

Invalid Parameter Passed.

So whenever u pass 0 as count you will get STRSAFE_E_INVALID_PARAMETER error. We will also make some more modifications to look what happens when the destination buffer size is not enough to add all the contents of the source buffer.

Sample Code 3

Consider the example shown below:

#include "stdafx.h"
#include "stdlib.h"
#include "winerror.h"
#include "windows.h"
#include "strsafe.h"

int _tmain(int argc, _TCHAR* argv[])
{
 TCHAR pszDest[8]= _T("Hello");
 TCHAR pszSrc[100] = _T("World");
 HRESULT hresult;

 hresult = StringCchCat(pszDest, _countof(pszDest), pszSrc);

 if (SUCCEEDED(hresult))
 {
  printf("String Succesfully concatenated:%ls\n", pszDest);
 }
 else
 {
  if (hresult == STRSAFE_E_INVALID_PARAMETER)
   printf("Invalid Parameter Passed\n");
  else if(hresult == STRSAFE_E_INSUFFICIENT_BUFFER)
   printf("Destination Size not sufficient\n");
  else
   printf("Some unknown error happened\n");
 }
 return 0;
}

O/P:

Destination Size not sufficient.

If you want to perform string concatenation on char variables instead of TCHAR you have to use StringCchCatA(ANSI) function arguments will be same. If you want to perform string concatenation on WCHAR variables you have to use StringCchCatW(Unicode0)function.

Related Post