StringCchCat Function example
StringcCchCat Function:
Operation: Concatenates one striing to another string.
Syntax: HRESULT StringCchCat(_Inout_ LPTSTR pszDest,
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 Code1:
#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:
#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:
#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.
Comments
Post a Comment