Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / Win32

Secure String Handling in Windows Applications

Rate me:
Please Sign up or sign in to vote.
4.82/5 (12 votes)
9 Mar 2014CPOL11 min read 61.3K   974   46  
This article explains about Strsafe, Secured C run time library (CRT) and safe STL functions.
// StringCopy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <atlstr.h>
#define MAX_CHAR 255
#define BUFSIZE 85
#include <strsafe.h>

int _tmain(int argc, _TCHAR* argv[])
{

  /* declare unsafe copy variable */
  wchar_t unsafe_copy_str1[]=L"Hello world";
  wchar_t unsafe_copy_str2[MAX_CHAR];

  /* demo unsafe wcscpy without buffer check */
  wprintf(L"#### String Copy demo using wcscpy #### \n");
  wcscpy (unsafe_copy_str2, unsafe_copy_str1);
  wprintf (L"After copy string = %s\n\n", unsafe_copy_str2);
   
 	
  /* declare safe CRT variable */
  wchar_t safe_copy_str1[]=L"Hello world";
  wchar_t safe_copy_str2[MAX_CHAR];

  /* demo safe CRT variable */
  wprintf(L"#### String Copy demo using wcscpy_s #### \n");
  wcscpy_s( safe_copy_str2, _countof(safe_copy_str1),safe_copy_str1 );
  wprintf (L"After copy string = %s\n\n", safe_copy_str2);


  /* declare strsafe variable */
  wchar_t safe_copy_str3[]=L"Hello world";
  wchar_t safe_copy_str4[MAX_CHAR];

  /* demo strsafe variable */
   wprintf(L"#### String Copy demo using StringCchCopy #### \n");
    HRESULT Res;
    Res=StringCchCopy(STRSAFE_LPWSTR(safe_copy_str4), sizeof(safe_copy_str4), STRSAFE_LPWSTR(safe_copy_str3));
    if (Res != S_OK) {
      wprintf(L"StringCbCopy Failed: %s\n", safe_copy_str4);
      exit(-1);
   }

	wprintf (L"After copy string = %s\n\n", safe_copy_str4);


	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Selvam has worked on several technologies like Java, Python, Big data, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoys writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Big Data
o Google Professional Data Engineer 2021
o Confluent Certified Developer for Apache Kafka 2020
o Datastax Apache Cassandra 3.x Developer Associate Certification 2020
✓ Cloud
o Google Professional Cloud Architect 2021
o Microsoft Certified: Azure Solutions Architect Expert 2020
o AWS Certified Solutions Architect - Associate 2020
✓ Oracle Certified Master, Java EE 6 Enterprise Architect (OCMEA) 2018

Github : https://github.com/selvamselvam
Web site: http://www.careerdrill.com
Linkedin: https://www.linkedin.com/in/selvamselvam/

Comments and Discussions