question

BhattNiravXylem-1404 avatar image
0 Votes"
BhattNiravXylem-1404 asked SimpleSamples commented

How can I use strcpy_s in my Visual Studio code?

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++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you asking about Visual Studio Code (not the same thing as Visual Studio) or code a Visual Studio programming language? Visual Studio code is not a programming language. You have changed the tag to C++ and C++ is a programming language but VS is not.

0 Votes 0 ·
WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered Viorel-1 edited

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://docs.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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the response!!!

I have updated the tag as C++.

When I say, I am unable to use the strcpy_s function means, while declaring this function in my source code, I can see the error like undefined function declaration even though I have selected C11 as a source code language and string.h is included.

What I came to know after opening the header file string.h, there isn't any function declaration for these safe string operation functions like strcpy_s etc. I am not simply replacing the function from strcpy to strcpy_s, I wanted to introduce the fresh line of code with strcpy_s function.

So I wanted to know, if other project settings should also be modified? Any other header file or library file needed before compilation of existing project? If yes, please let me know the steps that I can follow or files to be added.

I am attaching the image of my project setting for VisualGDB and the error message I got from the IDE while compilation.130649-2.png130667-1.png130650-3.png


0 Votes 0 ·
2.png (28.4 KiB)
1.png (54.8 KiB)
3.png (30.1 KiB)
Viorel-1 avatar image Viorel-1 BhattNiravXylem-1404 ·

It seems that you do not use the right string.h header file. Check if the “Windows SDK Version” is set correctly in your Project Properties.

The recent file is usually located in a folder like "C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0".

0 Votes 0 ·
BhattNiravXylem-1404 avatar image
0 Votes"
BhattNiravXylem-1404 answered BhattNiravXylem-1404 commented

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



4.png (14.2 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


I think that you should specify “Windows SDK Version: 10.0 (latest installed version)” and “Platform Toolset: Visual Studio 2019 (v142)”.

Did you check if strcpy_s is present inside the string.h file from your ucrt directory?

Also check the “VC++ Directories” from Project Properties. Go to “Include Directories” field, select “<Edit…>” and see the list of folders that are scanned during compilation. Maybe your project uses an older file from a different folder.

Also perform some experiments in a new default Console project.

0 Votes 0 ·

Hello,

I can see the strcpy_s function in the string.h file located at : C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt

So I have added the path in addition include directory for my project setting. Please see the attached image for reference.

On top of that I can see one macro should be defined before including the string.h header file so I did that too.

Even though, I haven't got any success and issue still persist. Let me know if I am doing anything wrong here.130713-5.png130763-6.png130781-7.png


0 Votes 0 ·
5.png (64.6 KiB)
6.png (28.2 KiB)
7.png (4.3 KiB)
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.