Click here to Skip to main content
12,953,263 members (82,299 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Gurus,

Can anyone please explain the difference between strcpy_s and StringCchCopy?

I would also like to know the difference between the functions found in StrSafe.h and the other safe string functions like wcscpy_s etc.

Please advice which function is best to use.

Thanks in advance.
Posted 17-Mar-11 3:50am
Updated 17-Mar-11 4:17am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

If you really want to use the "safe" ones (they are not really safe, and are slow), I'd go with strcpy_s. It is provided with CRT, whereas StringCchCopy depends on Windows version - it is supported only in XP SP2 and above.
Ryan Zahra 17-Mar-11 10:27am
In my case, support is not a problem as I'm using this for Windows Server 2003 SP2 and over. In your opinion, which function should I use and why?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

They both do the same thing. strcpy_s is standard C, and the other is MFC a native Windows API.

Google really does work. Try this link:[^]
Ryan Zahra 17-Mar-11 10:11am
Agreed, but which should I use? Which one is the best?
Whatever blows up your skirt.
Emilio Garavaglia 17-Mar-11 13:53pm
We don't have their respective sources, but I'll not be surprised if we found that one just calls the other!
Please don't edit my posts. If there's a mistake, let me know, and I'll edit it.
Emilio Garavaglia 17-Mar-11 16:46pm
Now you know ...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

strcpy_s is the secure version of strcpy :

the secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler

while StringCchCopy is a replacement for the following functions :

* strcpy, wcscpy, _tcscpy
* lstrcpy
* StrCpy
Ryan Zahra 17-Mar-11 10:08am
According to MSDN both functions are secure. See
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Both Versions are "Sercure".
I personally prefer the "StringCchCopy" versions since they are cleaner and more consistent then the *_S versions.



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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 17 Mar 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100