Question

Are the StringCch* functions considered safer then the safe versions of the CRT string functions.

Is StringCchCatW safer then wcscat_s? Or StringCchCopyW vs wcscpy_s?

Was it helpful?

Solution

This is tagged C++, so you should not use either of them, but std::wstring instead.

OTHER TIPS

It really depends on how you are dealing with strings in your application.

With the Windows SDK many people end up mixing a lot of c-runtime, and windows api calls - because the windows api is a C API layered on top of the c-runtime it is difficult for windows programmers to know they are doing this, and why it is (potentially) wrong.

But, basically, at some point of your application development, you really should choose where you are going to get your basic data types from. Especially in the case of string data as the localization apis, that effect sorting, collation and so on are quite different.

You can choose to use the c-runtimes string support. This involves using strings that are typed as "const char*" or wchar_t. Microsoft's c-runtime specifically (it is possible to develop windows applications with GCC, so this distinction can be important if you want to write code that compiles on multiple compilers) offers a set of functions in , based off storing text data in _TCHAR's, and using the _MBCS and _UNICODE macros to switch the functions from single character, to multi byte character to wide character supporting versions.

If you choose to use the c-runtimes string abstractions, then using the c-runtimes "safe" string routnes would make the most sense.

Alternatively, Windows GUI applications frequently choose to use the windows SDK data types: CHAR, INT, WCHAR, TCHAR, DWORD etc. Strings, would be represented by LPCTSTR variables usually, and in this case, continuing to use the windows abstractions, SafeStringCch etc. make the most sense.

Really, the worst thing you can do in a program is keep on bouncing between using windows API calls, and c-runtime calls, as they have different rules sometimes for how they deal with edge cases... and this can lead to some very hard to spot bugs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top