Convert char * to wchar_t * using mbstowcs_s

I tried converting char * to wchar_t *, but I am having problems using mbstowcs, and Visual Studio wants mbstowcs_s ...

char *port; size_t size = strlen(port) + 1; wchar_t* portName = new wchar_t[size]; mbstowcs(portName, port, size); 

How do I change a function to mbstowcs_s?

+8
c ++ char visual-studio-2013
source share
1 answer

I would not recommend turning off protected code warnings when the fix for using protected methods is so simple, so here you are:

  const char *port="8080"; size_t size = strlen(port) + 1; wchar_t* portName = new wchar_t[size]; size_t outSize; mbstowcs_s(&outSize, portName, size, port, size-1); std::wcout << portName << std::endl; 

Tested with cl /W3 /EHsc on VS2013.

+19
source share

All Articles