Setting aside whether the _s functions are evil, I think you are worrying about the Windows version too much. strcpy_s, etc are CRT functions, not Win32 API calls, so their availability depends on the CRT you are linking against, not the version of Windows you run on.
The macros proposed above should really be switching on the version of Visual C++, not Windows. The secure functions turned up with Visual Studio 2005.
The ban referred to in the article mentioned above must refer to Microsoft's coding standards. Windows 7 runs our legacy code -- with loads of old CRT calls -- without complaint!
Andy
PS If you do use (eg) strcpy_s, note that the extra parameter is the buffer size, not the string length. And it's the second, not the third parameter. (And dest comes before src in the param list!)
1 2 3 4 5
|
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
|
numberOfElements is the size the destination string buffer.
so something like this would work for arrays but NOT char*/wchar_t* pointers to new-ed buffer.
#define MY_STRCPY(dest, src) strcpy_s(dest,sizeof(dest),src);
(wchar_t and TCHAR versions left as exercise for reader)
To handle dynamically allocated buffer, you will need a macro which allows you to provide the buffer size (and just ignores it in the non-_s case).
It might be better to provide a MY_STRCPY_S and then map it to strcpy when strcpy_s is not available.