• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

StringCchCat Function example

by admin

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.

Filed Under: C, C Library

Some more articles you might also be interested in …

  1. C Operators
  2. Linux Device Driver example for dump_stack() to print the stack trace of module loading
  3. C Arrays
  4. oe-pkgdata-util utility in Yocto
  5. Yocto recipetool tutorial
  6. pthread_yield example in c
  7. Basics of C Functions
  8. make: Nothing to be done for `default’
  9. Basics of C Preprocessors
  10. C Looping Statements

You May Also Like

Primary Sidebar

Recent Posts

  • qm Command Examples in Linux
  • qm wait Command Examples in Linux
  • qm start Command Examples in Linux
  • qm snapshot Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright