Problem with wcscpy_s function

I use the MSDN example with the create ComboBox but I have a problem with this

 
wcscpy_s(A, sizeof(A)/sizeof(TCHAR), (TCHAR*)Planets[k]);


the Compiler says ..
||=== Build: Debug in ComboBox (compiler: GNU GCC Compiler) ===|
|In function 'void ComboBox(HWND)':|
|39|error: 'wcscpy_s' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I add the <cstring> and even the <wchar> to load the funcions, but is not working either with these 2 headers
Hello Mif,

1
2
I use the MSDN example with the create ComboBox but I have a problem with this

What MSDN example? Where did you find it?

If you really want an answer then answer the above questions so everyone can know what you are working with.

Next post enough code to compile and duplicate the problem. Some may see the problem without even compiling.

With just 1 line of code it is hard to say if "cstring" is need or not.

"wchar.h" is a C header file and it appears that "cwchar" is the C++ header file. On my VS2017 IDE "wchar" does not exist.

Because of this and with out more code I do not know what you have done.

Andy
Are you compiling this as C or C++? There are C++ templates for the C functions available.

The wide string example from here, https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019 compiles without a hitch in C::B 20.03. Of course the code works fine with MSVC.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstring>  // for wcscpy_s, wcscat_s
#include <cstdlib>  // for _countof
#include <iostream> // for cout, includes <cstdlib>, <cstring>
#include <errno.h>  // for return values

int main(void)
{
   wchar_t string[80];
   // using template versions of wcscpy_s and wcscat_s:
   wcscpy_s(string, L"Hello world from ");
   wcscat_s(string, L"wcscpy_s ");
   wcscat_s(string, L"and ");
   // of course we can supply the size explicitly if we want to:
   wcscat_s(string, _countof(string), L"wcscat_s!");

   std::wcout << L"String = " << string << std::endl;
}


TCHAR is not a good choice to use when dealing with wide-char/Unicode strings and functions. It is a generic type that can change whether Unicode is defined or not. Use WCHAR or wchar_t instead.

Notice in the C version example getting the size of a C string is done using the _countof macro.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/countof-macro?view=vs-2019
According to MS wcscpy_s requires either sting.h or wchar.h. Since this example uses TCHAR you also need tchar.h.
It also might be the problem with GCC not fully implementing the Win SDK.

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019
Ohh right Andy sorry about short describe, here is the link with the example from MSDN

https://docs.microsoft.com/en-us/windows/win32/controls/create-a-simple-combo-box

But i resolve it.
As they use wcscpy_s, that is declared in string.h on Windows, but may not be declared in gcc compilers, and when they wrote this code they didn't specify I have to include <wchar.h> or <cstring>, or whatever.
I tryed wcscpy also but it takes only 2 arguments. Also, they are using TCHAR for wcscpy_s, as it is Unicode only.

MS doesn't specify what you need to include when using non-MS compilers, Visual Studio is their standard.
Topic archived. No new replies allowed.