breakdown of WinAPI string types:
LP = 'long pointer' or just pointer (the 'long' bit is a legacy thing)
C = const
T = TCHAR
W = wchar_t
[neither T nor W] = char
STR = 'string'
so:
1 2 3 4 5 6
|
LPCSTR = const char*
LPCTSTR = const TCHAR*
LPCWSTR = const wchar_t*
LPSTR = char*
LPTSTR = TCHAR*
LPWSTR = wchar_t*
|
The "normal" version of WinAPI functions take TCHAR strings. But they have alternative forms which take more sane string types:
ends in 'A' = takes a char string
ends in 'W' = takes a wchar_t string
"normal" = takes a TCHAR string
So:
1 2 3
|
MessageBox = takes TCHAR / LPCTSTR strings
MessageBoxA = takes char / LPCSTR strings
MessageBoxW = takes wchar_t / LPCWSTR strings
|
What you need to know about TCHARs: they're retarded. I recommend avoiding them.
If you want to use Unicode for everything (good on you), use the 'W' form of WinAPI functions and structs. IE: call MessageBoxW instead of MessageBox... use OPENFILENAMEW instead of OPENFILENAME, etc.
using wstrings will work for ouputting data to WinAPI... but you'll need to cal the c_str member to get a const pointer to the data:
1 2 3
|
std::wstring message = L"Some text";
MessageBoxW( NULL, message.c_str(), NULL, MB_OK );
|
EDIT:
To "fix" your program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include "stdafx.h" // <- I would get rid of this since you clearly do not need a PCH for
// this small project. protip: when creating new projects, choose the "empty project"
// option.
#include <string>
#include <Windows.h>
using namespace std;
//int _tmain(int argc, _TCHAR* argv[])
int main() // <- use a standard main -- not that _tmain crap
{
wstring message = L"c++ jungle warfare";
MessageBoxW(NULL, message.c_str(), L"Title", MB_OK); // <- if using wide strings
// then use the W form of the function
return 0;
}
|
Or if you're a masochist and like TCHAR strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include "stdafx.h"
#include <string>
#include <Windows.h>
using namespace std;
// make a TCHAR string type
typedef std::basic_string<TCHAR> tstring;
int main()
{
tstring message = TEXT("c++ jungle warfare"); // <- TCHAR literals must be in a TEXT
// macro... or a _T macro if you #include <tchar.h> would also work
MessageBox(NULL, message.c_str(), TEXT("Title"), MB_OK); // <- can use the normal
// 'MessageBox' now because we have TCHAR strings
return 0;
}
|