Discussion:
converting char to LPCTSTR?
(too old to reply)
Danko McBudnugitt
2007-09-05 20:30:46 UTC
Permalink
I am using some winsock code in a GUI app written in Win32 C. The
winsock network functions handle strings as standard ol' fashioned
character arrays, but I need to output this text on a listbox in a
dialog box. Here's the function I'm using from the Win32 API:

BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString
);

Notice the 4th parameter is the actual string, but its data type is
LPCTSTR, which is a long pointer to a unicode string. Does anyone
know how to convert a standard char type into a LPCTSTR type? There
has to be a way to do it. This seems like it would be a common thing
to do when writing code C for a windows GUI app.

(My code works fine in a console app since there is no use of
unicode. If I were to use printf() or cout, its fine.)
David Wilkinson
2007-09-05 20:52:52 UTC
Permalink
Post by Danko McBudnugitt
I am using some winsock code in a GUI app written in Win32 C. The
winsock network functions handle strings as standard ol' fashioned
character arrays, but I need to output this text on a listbox in a
BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString
);
Notice the 4th parameter is the actual string, but its data type is
LPCTSTR, which is a long pointer to a unicode string. Does anyone
know how to convert a standard char type into a LPCTSTR type? There
has to be a way to do it. This seems like it would be a common thing
to do when writing code C for a windows GUI app.
(My code works fine in a console app since there is no use of
unicode. If I were to use printf() or cout, its fine.)
Danko:

You can use MultiByteToWideChar().

If you know that all your characters are ASCII (< 128) then you can just
create a wchar_t array of the same size and copy the characters one-by one.

I believe this will also work for all the 8-bit characters in
ISO-8859-1, which is almost the same as Windows-1252.

In MFC/ATL there are some more convenient ways of doing this (using
CString or A2W for example).
--
David Wilkinson
Visual C++ MVP
Alexander Nickolov
2007-09-06 16:36:35 UTC
Permalink
No, some characters like the euro have 0x2xxx codepoints.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Post by David Wilkinson
Post by Danko McBudnugitt
I am using some winsock code in a GUI app written in Win32 C. The
winsock network functions handle strings as standard ol' fashioned
character arrays, but I need to output this text on a listbox in a
BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString
);
Notice the 4th parameter is the actual string, but its data type is
LPCTSTR, which is a long pointer to a unicode string. Does anyone
know how to convert a standard char type into a LPCTSTR type? There
has to be a way to do it. This seems like it would be a common thing
to do when writing code C for a windows GUI app.
(My code works fine in a console app since there is no use of
unicode. If I were to use printf() or cout, its fine.)
You can use MultiByteToWideChar().
If you know that all your characters are ASCII (< 128) then you can just
create a wchar_t array of the same size and copy the characters one-by one.
I believe this will also work for all the 8-bit characters in ISO-8859-1,
which is almost the same as Windows-1252.
In MFC/ATL there are some more convenient ways of doing this (using
CString or A2W for example).
--
David Wilkinson
Visual C++ MVP
Brian Muth
2007-09-05 21:15:45 UTC
Permalink
USES_CONVERSION;
LPCTSTR n;
n = A2CT ("Example string");

Brian
Alexander Nickolov
2007-09-06 16:37:54 UTC
Permalink
Should be A2T, since this is C, not C++. I'm not sure that
atlconv.h is C-compatible though, so this might not work...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Post by Brian Muth
USES_CONVERSION;
LPCTSTR n;
n = A2CT ("Example string");
Brian
Giovanni Dicanio
2007-09-06 22:09:53 UTC
Permalink
Post by Alexander Nickolov
Should be A2T, since this is C, not C++. I'm not sure that
atlconv.h is C-compatible though, so this might not work...
I think that A2CT that Brian has suggested is OK also for C.
The C++ version of newer ATL (>= 7) is CA2CT (and it does not require
USES_CONVERSION).

http://msdn2.microsoft.com/en-us/library/87zae4a3(vs.71).aspx

Giovanni
Alexander Nickolov
2007-09-07 16:49:58 UTC
Permalink
Ah, yes, my bad...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Post by Giovanni Dicanio
Post by Alexander Nickolov
Should be A2T, since this is C, not C++. I'm not sure that
atlconv.h is C-compatible though, so this might not work...
I think that A2CT that Brian has suggested is OK also for C.
The C++ version of newer ATL (>= 7) is CA2CT (and it does not require
USES_CONVERSION).
http://msdn2.microsoft.com/en-us/library/87zae4a3(vs.71).aspx
Giovanni
Jeff☠Relf
2007-09-06 00:19:25 UTC
Permalink
SetDlgItemText() works with plain ol' ASCII,
just add “ #undef UNICODE ” before any “ #include ”.

If it's an NNTP server or POP server you're working with,
you should use the “ Content-Type ” “ CharSet= ” fields in the headers
along with “ IMultiLanguage3 * Outlook ; MIMECSETINFO _Encoding ; ”
and “ Outlook->GetCharsetInfo( CharSet, & _Encoding ); ”.

I do this in X.CPP, which you can find in:
“ www.Cotse.NET/users/jeffrelf/Games.ZIP ”.
Ron Francis
2007-09-06 22:50:21 UTC
Permalink
Jeff,

Can you please not change the subject line of the thread.
It only serves to confuse rather than help.

Regards,
Ron Francis
www.RonaldFrancis.com
Post by Jeff☠Relf
SetDlgItemText() works with plain ol' ASCII,
just add " #undef UNICODE " before any " #include ".
If it's an NNTP server or POP server you're working with,
you should use the " Content-Type " " CharSet= " fields in the headers
along with " IMultiLanguage3 * Outlook ; MIMECSETINFO _Encoding ; "
and " Outlook->GetCharsetInfo( CharSet, & _Encoding ); ".
" www.Cotse.NET/users/jeffrelf/Games.ZIP ".
Jeff☠Relf
2007-09-08 00:49:41 UTC
Permalink
The header of each and every post contains the immediate subthread.
Going up the root, most recent first, this thread is:

“ Ron_Francis news:***@TK2MSFTNGP05.phx.gbl
Jeff☠Relf news:***@Cotse.NET
Danko news:***@r34g2000hsd.googlegroups.com
”.

So it's not my fault if you can't see it, Mr. Francis.
Either upgrade your newsreader or suffer, I don't care which.

My newsreader, X.EXE, handles it correctly; it's in:
“ www.Cotse.NET/users/jeffrelf/Games.ZIP ”.
Miguel Guedes
2007-09-08 13:41:09 UTC
Permalink
Post by Jeff☠Relf
The header of each and every post contains the immediate subthread.
”.
So it's not my fault if you can't see it, Mr. Francis.
Either upgrade your newsreader or suffer, I don't care which.
“ www.Cotse.NET/users/jeffrelf/Games.ZIP ”.
We can all see the hierarchy of the posts. That's not the point.

I think you need to read up on newsgroup good practice guidelines. See here:
http://www.kent.ac.uk/is/computing/regulations/good_practice/news.html

Especially read under "Creating a posting" and the effective usage of the
subject line.
Ron Francis
2007-09-09 05:14:13 UTC
Permalink
Yes, I can see the hierarchy as Miguel said and I never suggested that I
couldn't.
You are the only one here that I have seen that changes the subject line.
Do you actually think that it is helping anyone or making the posts easier
to understand?

One problem with Outlook Express, (which I imagine many here use), is if you
search for a topic, it will bring up the post without the hierarchy.
You then have to physically scan the particular newsgroup with your eyes for
the same subject line to find the whole thread.
Finding the beginning of one of your threads that way is very difficult.
But you, (Mr Relf), say "Either upgrade your newsreader or suffer, I don't
care which."
I'm afraid that may sum up your attitude here, but I'm hoping not.
You are obviously a talented man and have much to offer if you choose to.

Regards,
Ron Francis
www.RonaldFrancis.com
Post by Jeff☠Relf
The header of each and every post contains the immediate subthread.
".
So it's not my fault if you can't see it, Mr. Francis.
Either upgrade your newsreader or suffer, I don't care which.
" www.Cotse.NET/users/jeffrelf/Games.ZIP ".
Ben Voigt [C++ MVP]
2007-09-06 02:13:53 UTC
Permalink
Post by Danko McBudnugitt
I am using some winsock code in a GUI app written in Win32 C. The
winsock network functions handle strings as standard ol' fashioned
character arrays, but I need to output this text on a listbox in a
BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString
);
Notice the 4th parameter is the actual string, but its data type is
LPCTSTR, which is a long pointer to a unicode string. Does anyone
know how to convert a standard char type into a LPCTSTR type? There
has to be a way to do it. This seems like it would be a common thing
to do when writing code C for a windows GUI app.
(My code works fine in a console app since there is no use of
unicode. If I were to use printf() or cout, its fine.)
What's this world coming to? Jeff came the closest to a good answer.

LPCTSTR does not mean a unicode string, that would be LPCWSTR or LPWSTR or
BSTR or OLESTR. It means there are two versions of the function as follows:

BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, const char* lpString);
and
BOOL SetDlgItemTextW(HWND hDlg, int nIDDlgItem, const wchar_t* lpString);

Both versions are available in all Windows programs (on Windows
NT/2000/XP/Vista -- 95/98/Me have no W version and CE has no A version).
SetDlgItem is a macro which points to one or the other depending on
_UNICODE.

Since your data is always ASCII regardless of the OS settings use
SetDlgItemTextA and go. It will work for all Windows versions from 95 and
up.
Alexander Nickolov
2007-09-06 16:51:00 UTC
Permalink
Post by Ben Voigt [C++ MVP]
Since your data is always ASCII regardless of the OS settings use
SetDlgItemTextA and go. It will work for all Windows versions from 95 and
up.
... except for Windows CE. WinCE does not implement the
narrow form of the Win32 API. Admittedly OP probably doesn't
care about WinCE, but still - that wasn't stated explicitly...

Additionally, it's just not true that WinSock is narrow only.
Most functions don't deal with strings at all. Only the DNS-
related functions like gethostname use strings. The older BSD-
style functions (like gethostname) are indeed narrow only, but
they are deprecated anyway. The replacement GetAddrInfo
function is TCHAR-mapped.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Post by Ben Voigt [C++ MVP]
Post by Danko McBudnugitt
I am using some winsock code in a GUI app written in Win32 C. The
winsock network functions handle strings as standard ol' fashioned
character arrays, but I need to output this text on a listbox in a
BOOL SetDlgItemText(
HWND hDlg,
int nIDDlgItem,
LPCTSTR lpString
);
Notice the 4th parameter is the actual string, but its data type is
LPCTSTR, which is a long pointer to a unicode string. Does anyone
know how to convert a standard char type into a LPCTSTR type? There
has to be a way to do it. This seems like it would be a common thing
to do when writing code C for a windows GUI app.
(My code works fine in a console app since there is no use of
unicode. If I were to use printf() or cout, its fine.)
What's this world coming to? Jeff came the closest to a good answer.
LPCTSTR does not mean a unicode string, that would be LPCWSTR or LPWSTR or
BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, const char* lpString);
and
BOOL SetDlgItemTextW(HWND hDlg, int nIDDlgItem, const wchar_t* lpString);
Both versions are available in all Windows programs (on Windows
NT/2000/XP/Vista -- 95/98/Me have no W version and CE has no A version).
SetDlgItem is a macro which points to one or the other depending on
_UNICODE.
Since your data is always ASCII regardless of the OS settings use
SetDlgItemTextA and go. It will work for all Windows versions from 95 and
up.
Ben Voigt [C++ MVP]
2007-09-06 17:50:01 UTC
Permalink
Post by Alexander Nickolov
Post by Ben Voigt [C++ MVP]
Since your data is always ASCII regardless of the OS settings use
SetDlgItemTextA and go. It will work for all Windows versions from 95
and up.
... except for Windows CE. WinCE does not implement the
narrow form of the Win32 API. Admittedly OP probably doesn't
care about WinCE, but still - that wasn't stated explicitly...
I thought I covered that by saying CE doesn't provide the *A version of
functions.
Post by Alexander Nickolov
Additionally, it's just not true that WinSock is narrow only.
Most functions don't deal with strings at all. Only the DNS-
related functions like gethostname use strings. The older BSD-
style functions (like gethostname) are indeed narrow only, but
they are deprecated anyway. The replacement GetAddrInfo
function is TCHAR-mapped.
I didn't say anything about winsock. The OP's data, which arrives through a
socket but could as easily be gotten from a file or any other binary
transfer, is multibyte, not wide characters. At least that was my reading
of the post....
Post by Alexander Nickolov
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
Loading...