Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32
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
Edited 17-Mar-11 4:17am
v2
Rate this: bad
good
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.
  Permalink  
Comments
Ryan Zahra at 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
good
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:
 
http://msdn.microsoft.com/en-us/library/ms646979(v=VS.85).aspx[^]
  Permalink  
v2
Comments
Ryan Zahra at 17-Mar-11 10:11am
   
Agreed, but which should I use? Which one is the best?
John Simmons / outlaw programmer at 17-Mar-11 13:52pm
   
Whatever blows up your skirt.
Emilio Garavaglia at 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!
John Simmons / outlaw programmer at 17-Mar-11 13:54pm
   
Please don't edit my posts. If there's a mistake, let me know, and I'll edit it.
Emilio Garavaglia at 17-Mar-11 16:46pm
   
Now you know ...
Rate this: bad
good
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
  Permalink  
Comments
Ryan Zahra at 17-Mar-11 10:08am
   
According to MSDN both functions are secure. See http://msdn.microsoft.com/en-us/library/ms647466(v=VS.85).aspx
Rate this: bad
good
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.
 
Regards,
 
K,D
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 410
1 Maciej Los 258
2 CHill60 222
3 OriginalGriff 210
4 Sinisa Hajnal 125


Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 17 Mar 2011
Copyright © CodeProject, 1999-2014
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