How can I use strcpy_s in my Visual Studio code?

Bhatt, Nirav - Xylem 1 Reputation point
2021-09-09T05:47:39.017+00:00

Hello,

We are using standard string functions like strcpy, memcpy, strcat in our embedded projects, We came to know there are more safer functions available called strcpy_s, memcpy_s, strcat_s etc. that can be supported by C11 standards, I have done my compiler's setting accordingly, even though I am unable to use the safe string operation functions strcpy_s and others.

May I know, which header files or library files I need to include for using these safe functions in my project?

Which of the header file will have the function defination of these safe functions?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,572 questions
{count} votes

3 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2021-09-09T06:46:42.01+00:00

    First off note that you have used the "tag" for
    Visual Basic. But you are asking about C language
    functions so you should change the tag to C++ which
    covers C as well.

    The documentation should provide all that you need to know:

    strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l

    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-160

    When you say:

    I am unable to use the safe string operation functions
    strcpy_s and others.

    you should be more specific. What exactly happens when
    you try to use them?

    Note also that in C many of the safe versions of functions
    require an extra argument (or two) to provide a size, so you
    can't just change the function name. Read the documentation
    to see what is needed, and note that for C++ there may be
    defaults that simplify the switch and allow you to omit
    the extra arguments but C won't usually do that.

    • Wayne
    1 person found this answer helpful.

  2. Bhatt, Nirav - Xylem 1 Reputation point
    2021-09-09T09:08:48.847+00:00

    Thanks,

    Please see the attached image for your reference, It seems we are using Windows SDK v10 in our case.

    What should be expected version of SDK to be used?

    May I know How can I check the Windows SDK Version in the visual studio project settings?

    130560-4.png


  3. WayneAKing 4,921 Reputation points
    2021-09-09T10:54:24.717+00:00

    It appears you may be using Clang not VC++, so there may be
    questions of the C11 implementation of these Standard
    functions in Clang. It also appears that the implementation
    of these functions as shown in the Doc at the link I posted is
    by MS in VC++ and is a different implementation than the one
    found in C11. See:

    using c11 standard with clang for use of strcpy_s
    https://stackoverflow.com/questions/57915149/using-c11-standard-with-clang-for-use-of-strcpy-s

    • Wayne
    0 comments No comments