1. It's a long topic and you should read a decent tutorial on Windows programming. I'll just summarize. If you don't understand it, proceed to a thorough tutorial.
The Windows API is for the most part doubled: There is a set of functions that take and manage ANSI strings and another set that take and manage Unicode strings. These functions have different names: The ANSI ones' names will end with uppercase A, and the Unicode ones' names will end with uppercase W. Example: RegOpenKeyExA(), RegOpenKeyExW().
On top of that, the Windows header files #define macros for each of these functions like this:
1 2 3 4 5
|
#ifdef UNICODE
#define RegOpenKeyEx RegOpenKeyExW
#else
#define RegOpenKeyEx RegOpenKeyExA
#endif //UNICODE
|
So, when you write code for Windows, you must always know which function you are using (if the -A or the -W) so as to pass along the right type of string. Your code is currently working fine for you under Code::Blocks, but if you ever pass along this code to, say Visual Studio 2010, you'll notice it doesn't compile! This is because by default, Visual Studio C++ projects are Unicode builds, meaning the UNICODE macro is automatically #defined. So there goes your code down to the drain.
In order to properly code for Windows, you MUST use one of the following combinations ONLY. Any other combination is just WRONG:
A. If you use -W functions, you must use wchar_t-based string types like LPWSTR, WCHAR[], LPCWSTR, std::wstring, std::wostringstream, std::wstringstream, and L-prepended string literals (i. e. L"Hello world!"). You also output to the console using std::wcout and you input using std::wcin.
B. If you use -A functions, you must use char-based string types like LPSTR, char[], LPCSTR, std::string, std::ostringstream, std::stringstream and regular string literals (i. e. "Hello world!"). You also output to the console using std::cout and you input using std::cin.
C. If you use function names with neither termination, you must aid yourself with some data types for the STL types:
1 2 3 4 5 6 7 8 9 10 11 12
|
typedef std::basic_string<TCHAR> tstring;
typedef std::basic_ostringstream<TCHAR> tostringstream;
typedef std::basic_stringstream<TCHAR> tstringstream;
//Etc. Add more if you use more, like std:basic_istringstream<TCHAR> for input string streams.
#ifdef UNICODE
#define tcout std::wcout
#define tcin std::wcin
#else
#define tcout std::cout
#define tcin std::cin
#endif
|
So now I state case C: If you use function names with neither termination, you must use TCHAR-based string types like LPTSTR, TCHAR[], LPCTSTR, tstring, tostringstream, tstringstream and TEXT- OR _T-macro-prepended string literals (i. e. TEXT("Hello world!") or _T("Hello world") ). You also output to the console using tcout and you input using tcin.
In your code, you use function names that don't end with A or W, meaning you must fall into case C above, and as you can see, you are mixing them with std::string and naked literal strings. Because of all this, your code will fail to compile inside a Visual Studio 2010 C++ project with default settings.
2. You still do need to check it, you just don't see it.
3. You don't get the point. Hopefully, you'll get it someday.