String functions like strcpy,strcat,itoa...shows warning as "declared deprecated" in Visual Studio 8.0 instead it suggests to use functions which are enhanced secure versions of the same such as strcpy_s,strcat_s,_itoa_s.....using this in my file removes those warnings but i am using this same file in visual studio 6.0 here it gives compile time error as "undeclared identifier" which is correct as this old version of visual studio is not provided with the new added versions of above functions....so what is the solution for this as i want to use this same file in my VC6 as well as VC8 projects....plz help

Dani AI

Generated

You can keep one codebase and compile cleanly on both VC6 and VC8 without disabling warnings or changing call sites. The trick is to provide VC6-only shims that match the secure CRT signatures and behavior closely enough. Then your code can always call strcpy_s, strcat_s, and _itoa_s, while VC8 uses the real functions and VC6 uses your fallbacks. This also complements ’s suggestion to move toward std::string over time, but does not require it today.

Drop this header into your project and include it early:

// compat_securecrt.h
#include <string.h>
#include <stddef.h>
#include <errno.h>

#if defined(_MSC_VER) && _MSC_VER < 1400  // VC6 and older

#ifndef errno_t
typedef int errno_t;
#endif

static __inline errno_t strcpy_s(char* dst, size_t dstsz, const char* src) {
    if (!dst || !src || dstsz == 0) return EINVAL;
    size_t n = strlen(src);
    if (n >= dstsz) { dst[0] = '\0'; return ERANGE; }
    memcpy(dst, src, n + 1);
    return 0;
}

static __inline errno_t strcat_s(char* dst, size_t dstsz, const char* src) {
    if (!dst || !src || dstsz == 0) return EINVAL;
    size_t d = strlen(dst), s = strlen(src);
    if (d + s >= dstsz) { dst[0] = '\0'; return ERANGE; }
    memcpy(dst + d, src, s + 1);
    return 0;
}

static __inline errno_t _itoa_s(int value, char* buf, size_t bufsize, int base) {
    if (!buf || bufsize == 0 || base < 2 || base > 36) { if (buf) buf[0] = '\0'; return EINVAL; }
    const char* digits = "0123456789abcdefghijklmnopqrstuvwxyz";
    char tmp[70]; size_t i = 0; unsigned int v; int neg = (value < 0);
    if (neg) { int t = value + 1; v = (unsigned int)(-t) + 1; } else { v = (unsigned int)value; }
    do { tmp[i++] = digits[v % (unsigned)base]; v /= (unsigned)base; } while (v && i < sizeof(tmp)-1);
    if (neg) tmp[i++] = '-';
    if (i + 1 > bufsize) { buf[0] = '\0'; return ERANGE; }
    for (size_t j = 0; j < i; ++j) buf[j] = tmp[i - 1 - j];
    buf[i] = '\0'; return 0;
}
#endif

#ifndef COUNTOF
#define COUNTOF(a) (sizeof(a) / sizeof((a)[0]))
#endif

Now your existing calls compile on VC8 (real secure CRT) and on VC6 (the shims). As you refactor, prefer ’s std::string advice and, where possible, compute sizes with COUNTOF(arr) when passing buffer lengths to avoid off-by-one errors.

Recommended Answers

All 5 Replies

#define strcpy_s strcpy
:D

#define strcpy_s strcpy
:D

.....thanks for this quick help..,,,, but both of these funtions have different parameters,,,strcpy_s has a additional parameter to strcpy which is the size of buffer,,,..hence #define strcpy_s strcpy gives compile time error in Visual Studio 8 itself,,,,,so nw wht to do??

use std::string

I agree, using strings would probably solve your problem. But since that wasn't your question, you could try:

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE

#include <......> etc

which would take care of the "declared deprecated" warnings.

Or you could add #pragma warning (disable:4996) to your code

Here's a list of all the functions that are "deprecated"

I agree, using strings would probably solve your problem. But since that wasn't your question, you could try:

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE

#include <......> etc

which would take care of the "declared deprecated" warnings.

Or you could add #pragma warning (disable:4996) to your code

Here's a list of all the functions that are "deprecated"

Thanks a lot ....#pragma warning(disable:4996) has solved my problem temporarily.....but later it may give problem as suppressing warning will give compile time errors for these functions as the newer version will remove these functions....but its ok!! for time being the problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.