L (again) new problem!

Started by
19 comments, last by stenny 17 years, 9 months ago
Quote:Original post by stenny
ok. So we'll have to scrap that option. Wait...Can't I just create LPCWSTR's from scratch? Like:

const LPCWSTR g_szClass = "Classname";

- Stenny


const LPCWSTR g_szClass = L"Classname"; [smile]
Advertisement
Quote:Original post by stenny
ok. So we'll have to scrap that option. Wait...Can't I just create LPCWSTR's from scratch? Like:

const LPCWSTR g_szClass = "Classname";

- Stenny


Yes, but you need an 'L' infront to indicate the 'W' in LPCWSTR, meaning "wide" characters.

EDIT: Nevermind, Emmanuel Deloget got it. I should actually refresh the page to see what new replies there are instead of just deciding no one will post before me :).
Thanks, but what if I don't declare it from start, like in AppError? I have the same problem here again. I'll try something out.

Quote:EDIT: Nevermind, Emmanuel Deloget got it. I should actually refresh the page to see what new replies there are instead of just deciding no one will post before me :).


Well, you know what confucius said :P.

-Stenny
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
Sounds to me like you would be better taking M Delagot's suggestion and making a firm decision whether you want to use UNICODE or not through your compiler settings.

If you want to write portable code, but STILL use specific string handling functions, the only way I can see you could do it would be to detect manually whether or not UNICODE was on (by examining a macro, I guess) in your AppError function, then use the conversion functions to create a single-byte version of your string if it was to pass to said functions.

Sounds like a bit of a nightmare to me.
what are the advantages and disadvantages of unicode or non-unicode then?

-Stenny
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
Quote:Original post by stenny
Thanks, but what if I don't declare it from start, like in AppError? I have the same problem here again. I'll try something out.

If you need to convert char to wchar_t, you have to use conversion functions, like dhm said:
Quote:Original post by dhm
Hi,

Sounds like you need some string conversion functions. Have a look at http://msdn2.microsoft.com/en-US/library/ms235631.aspx for some inspiration.

Cheers,
dhm

This is important ONLY IF you need to convert strings from one type to another type. If you control all the strings in your application, TCHAR is the way to go, and the _T() macro might help:
// (Long) Pointer to a Constant Tchar STRingLPCTSTR myString = _T("teh string content");


Depending on your setup configuration, you migh need to be able to convert from a LPCTSTR to a LPWSTR (+ back conversion: LPWCSTR to LPTSTR) or from LPCTSTR to LPSTR (+ back conversion: LPCSTR to LPTSTR) [in the previous type names, the L can be forgotten: PSTR, PTSTR, PWSTR, PCSTR, PCTSTR and PCWSTR are aliases to the corresponding types).
#include <windows.h>#include <tchar.h>#include <string.h>// PWSTR is assumed to be already allocatedvoid tstr2wstr(PCTSTR tstr, PWSTR wstr){#ifdef UNICODE  // really: unicode to unicode; we only need to copy the string  wcscpy(wstr, tstr);#else  // not in unicode: conversion needed  mbstowcs(wstr, tstr, _tcslen(tstr)+1);#endif}// PTSTR is assumed to be already allocatedvoid wstr2tstr(PCWSTR wstr, PTSTR tstr){#ifdef UNICODE  // really: unicode to unicode; we only need to copy the string  wcscpy(tstr, wstr);#else  // not in unicode: conversion needed  wcstombs(tstr, wstr, wcslen(wstr)+1);#endif}// PSTR is assumed to be already allocatedvoid tstr2str(PCTSTR tstr, PSTR str){#ifdef UNICODE  // in unicode: conversion needed  mbstowcs(str, tstr, _tcslen(tstr)+1);#else  // not in unicode, no conversion needed  _tcscpy(str, tstr);#endif}// PWSTR is assumed to be already allocatedvoid str2tstr(PCSTR str, PTSTR tstr){#ifdef UNICODE  // in unicode: conversion needed  wcstombs(tstr, wstr, wcslen(wstr)+1);#else  // not in unicode, copy the data  _tcscpy(tstr, str);#endif}

Direct wchar_t to/from char string is done using wcstombs() or mbstowcs() so I don't need to write these functions again.

Note that wcstombs() fills the destination parameter with a multi-byte string. This is not really a full char* string (some special characters may be encoded using more than one byte on a MB string). However, if you restrict your character set to the ASCII set, you won't have any problem.

HTH,
Quote:Original post by EasilyConfused
... M Delagot's ...

Oh My Gee you mispelled my n4m3!!!!!111one [grin]
BTW you can call me Emmanuel [smile]
Quote:Original post by stenny
what are the advantages and disadvantages of unicode or non-unicode then?

-Stenny


* ASCII and extended ASCII is a very limited set of characters. However, it can fully represent the English language as well as a bunch of other languages.

* Unicode is a character set which contains all the characters of the world (ie all the japanese and chines Kanji, as well as everything in the arab and cyrillic alphabet). It is a must have if your application is going to be released world wide, because it is as simple as ASCII (each character is made of two bytes, not one) and very powerfull.

* MBCS (multi-byte character set) is a mixed character set. It contains some characters that may be encoded using two bytes and some characters that may be encoded using only one byte (a particular byte, called the leading byte, signals which character is two byte long). MBCS is rather difficult to use but can help if you must port your very large application to support foreign languages (I did this some years ago with a code base containing 70 MB of code; changing everything to TCHAR or wchar_t would have been too painfull).
I'll stay for english for some time I think, and certainly no languages like japanese, arabic or runescript[grin]. Is this the only disadvantage?

-Stenny
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
Quote:Original post by Emmanuel Deloget
Quote:Original post by EasilyConfused
... M Delagot's ...

Oh My Gee you mispelled my n4m3!!!!!111one [grin]
BTW you can call me Emmanuel [smile]


Jeez, sorry. I'm normally so careful about this sort of thing. I thought I was being so clever with the 'M'. [grin]

This topic is closed to new replies.

Advertisement