wstring pointer (or something)

hello

i am trying to cut trough this windows jungle of character encodings and diffrent string types and what not. i want to make it easy for me by using wstring for everything so memory stuff gets done for me and should be clean since windows works internally with unicode.

but now i am trying to play around with a messagebox and it asks for some pointer thing (LPCWSTR or w/e)

now i found out you can just define lscprprpr value but all those diffrent types make me crazy. so i found out that you can make a wchar_t pointer thing and it accepts that. but as far as i know wstring is a wchar_t which just handles memory stuff. but for wstring i can find nowhere how to do the pointer trick so that you can pass its name to a windows api function, like messagebox which ask for LPCWSTR input

so i look for some clean way to pass wstring value to winapi functions which ask for LPCWSTR parameter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include <string>
#include <Windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	wstring message = L"c++ jungle warfare";          

	MessageBox(NULL, message, L"Title", MB_OK); // this don't work because second parameter 'message' is supposed to be pointer thing 

	return 0;
}


greeting

Last edited on
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;
}
Last edited on
man this post is so wonderfull i want to thank you and shake your hand. headache is gone !

btw in visual studio(2012) without #include "stdafx.h" it doesn't compile (atleast for console app)

i checked it works in empty project, i will use that.
Last edited on
btw in visual studio(2012) without #include "stdafx.h" it doesn't compile (atleast for console app)


Your project is set to use stdafx as a precompiled header file. You'd have to go in your project settings and disable it.

In my experience, unless your header is very big PCH files cause more problems than they solve. For such a trivial program, you won't see any improvement in compile time by using them so IMO it's better not to.

But anyway I'm glad you got it working. =)
If you want to get rid of stdafx.h then disable precompiled headers from project properties.
Topic archived. No new replies allowed.