C-style Strings to LPCWSTR

M

Mike Copeland

How can I convert a C-style string variable to LPCWSTR? I have code
that used to work on VC6.0 that didn't seem to care that a function call
that accepts an LPCWSTR argument wasn't that type, but now that I'm
trying to convert to VC10.0, these calls don't compile. 8<{{
So, I have calls such as:

MessageBox(NULL, emsg, L"Console Error", MB_OK);

where I can use the "L" in the literal, but the reference to "emsg"
(which is declared as "char[2048]") fails to compile. I can't find any
information about casting or converting such variables to something that
will compile. Please advise. TIA
 
M

Mike Copeland

What's LPCWSTR?

Assuming its a windows thing, try asking on a windows group. Or google.
It's something in VC10.0, but as there seems to be absolutely no
activity in any of the MicroSoft C groups (and I wasn't aware that it
isn't "standard"), I tried to ask here.
I've already tried Google, but I can't find anything on this subject
there. <sigh...>
 
I

Ian Collins

It's something in VC10.0, but as there seems to be absolutely no
activity in any of the MicroSoft C groups (and I wasn't aware that it
isn't "standard"), I tried to ask here.
I've already tried Google, but I can't find anything on this subject
there.<sigh...>

Well I just searched on "LPCWSTR" and the first few hits looked very
relevant.
 
B

Bo Persson

Ian Collins skrev 2012-05-26 05:19:
What's LPCWSTR?

Assuming its a windows thing, try asking on a windows group. Or google.

Assuming it's a Windows thing, the type would be wchar_t. Using plain
char went away with Windows 98.


Bo Persson
 
N

Nobody

What's LPCWSTR?

Assuming its a windows thing, try asking on a windows group. Or google.

It's the Windows name for "const wchar_t *".

LP = long (i.e. far) pointer
C = const
W = wide
STR = string

Arguably it should be "const wchar_t __far *" due to the "LP", but 16-bit
memory models (i.e. distinct near/far pointers) and wide characters are
mutually exclusive. The "LP" has stuck around for historical reasons (much
like WORD being a 16-bit type even on a 64-bit system).
 
M

Mike Copeland

How can I convert a C-style string variable to LPCWSTR?

With MultiByteToWideChar(), but you probably won't need this, see below.
So, I have calls such as:

MessageBox(NULL, emsg, L"Console Error", MB_OK);

where I can use the "L" in the literal, but the reference to "emsg"
(which is declared as "char[2048]") fails to compile. I can't find any
information about casting or converting such variables to something
that will compile. Please advise. TIA

Assuming you don't need or care about any non-ASCII characters, the fix
is easy, just call MessageBoxA(), this takes char* arguments:

MessageBoxA(NULL, emsg, "Console Error", MB_OK);

However, if this worked in VC6.0 then maybe the error is not what you
describe. If the problem persists, please post copy-paste of the error
message!

Perfect! This is just the sort of answer I was looking for:
something simple I can change in my code that didn't involve defining
and using new data types throughout my code. I have no need for non-
ASCII data in my applications, and this seems to do the job for me.
Thank you!
 
T

Tobias Müller

Mike Copeland said:
How can I convert a C-style string variable to LPCWSTR? I have code
that used to work on VC6.0 that didn't seem to care that a function call
that accepts an LPCWSTR argument wasn't that type, but now that I'm
trying to convert to VC10.0, these calls don't compile. 8<{{
So, I have calls such as:

MessageBox(NULL, emsg, L"Console Error", MB_OK);

where I can use the "L" in the literal, but the reference to "emsg"
(which is declared as "char[2048]") fails to compile. I can't find any
information about casting or converting such variables to something that
will compile. Please advise. TIA

Almost all C APIs use a special character type called TCHAR. It is defined
like:

#ifdef UNICODE // or was it _UNICODE?
#define TCHAR wchar_t
#else
#define TCHAR char
#endif

The messagebox function has conceptually a signature like:

MessageBox(HWND, const TCHAR*, const TCHAR*, DWORD); // or similar

However in reality it is defined as:

#ifdef UNICODE // or was it _UNICODE?
#define MessageBox MessageBoxW // uses wchar_t
#else
#define MessageBox MessageBoxA // uses char
#endif

So you can use either MessageBoxA directly or use MessageBox and convert
the string with A2T like:

USES_CONVERSION
::MessageBox(NULL, A2T(...), A2T(...), ...);

If you don't define UNICODE in your project settings (it is defined per
default, I think), A2T does actually nothing at all.
But you still can enable UNICODE later without changing the code if you
really need it.

Tobi
 
B

Bo Persson

Mike Copeland skrev 2012-05-27 05:35:
How can I convert a C-style string variable to LPCWSTR?

With MultiByteToWideChar(), but you probably won't need this, see below.
So, I have calls such as:

MessageBox(NULL, emsg, L"Console Error", MB_OK);

where I can use the "L" in the literal, but the reference to "emsg"
(which is declared as "char[2048]") fails to compile. I can't find any
information about casting or converting such variables to something
that will compile. Please advise. TIA

Assuming you don't need or care about any non-ASCII characters, the fix
is easy, just call MessageBoxA(), this takes char* arguments:

MessageBoxA(NULL, emsg, "Console Error", MB_OK);

However, if this worked in VC6.0 then maybe the error is not what you
describe. If the problem persists, please post copy-paste of the error
message!

Perfect! This is just the sort of answer I was looking for:
something simple I can change in my code that didn't involve defining
and using new data types throughout my code. I have no need for non-
ASCII data in my applications, and this seems to do the job for me.

Just note that using the A version of the functions will have Windows
translate the strings to wide characters at runtime.

Even if you don't seem to use non-ASCII codes in your program, Windows
still uses wide characters internally.


Bo Persson
 
V

Victor Bazarov

From your syntax, it looks like Microsoft's bastardization of a wide
character string. From the little I recall of Microsoft's naming
conversion, "WSTR" means a wide string, and "LP" is a long pointer. "C"
means character, although heavens know why that needs to be spelled out,
when you already have a WSTR…

'C' means "constant", although it's really OT here. Please try
Microsoft's online forums.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top