I’m trying to add proper error reporting to a legacy C++ Win32 DLL, after not having programmed in C++ for more than a decade.
This has been a forcible reminder of the incredible tedium of working with null terminated strings in C/C++ – not to mention the potential buffer overrun and security problems.
I’m considering using a more advanced string library – maybe MFC CString or the STL library.
Edit
DLL is used by both older versions of our software – written in C++; and newer versions – written in C#.
Compiler is VS2008.
For the first pass, I’m logging to the standard Windows Event Log, because passing error messages back to the DLLs clients will be a big architectural change (current architecture is terrible, with multiple failure modes being collapsed into a single boolean pass/fail return value that is passed all the way back up the call stack. Needless to say, this makes problem diagnoses a nightmare). But I’ll want to improve this at some later date.
String Library Questions
Since this is a DLL, is this even a good idea?
Are there any special considerations for DLLs?
Gotchas I should be aware of?
Is one library better than the other for this purpose?
Null String Questions
If I’m stuck with null terminated, which set of string functions should I be using? The MS help docs discourage the use of the old C functions (e.g. strcat, etc). But there seem to be many, many other options available now (e.g. _tcscat, _mbscat, wcscat, etc). Which should I use, and why?
Sharing any kind of template class from a DLL is dangerous. Since the template is in the header files, you have no control over whether the program is using the same versions of the header as your DLL – a mismatch can lead to deadly bugs.
As for the differences between _tcscat, _mbscat, and wcscat, you’ll need to decide on a character set for your API. The
_mbfunctions use the mostly obsolete multi-byte character sets, and thewcfunctions use wchar_t Unicode. The_tfunctions will resolve to one or the other depending on how you set up your project definitions.