7

I am converting windows library to linux. I need to find LPTSTR and LPCSTR in linux.

I get to know that i can use wchar_t can be used but I am not really sure about using it.

one of the method that uses LPTSTR is as follows:

void ErrorExit(LPTSTR zFunction)
{
}

Any help will be appreciated!

jparthj
  • 1,606
  • 3
  • 20
  • 44
  • 1
    If you are working in C, you will get compilation errors with wchar_t. It will work with C++ but not that on Windows wchar_t is 2 bytes; on Linux it is 4 bytes. – cup Feb 05 '14 at 11:09
  • @cup When was `wchar_t` removed from C? As far as I know, it exists in C. – glglgl Feb 05 '14 at 11:27
  • @glglgl: I don't know if it was removed from C. It doesn't work on C programs in gcc 4.7.2. Works on Windows. – cup Feb 05 '14 at 11:58
  • @cup Good that my gcc doesn't know this: `#include ` // `int main() { wchar_t w; }` works here. – glglgl Feb 05 '14 at 12:52

2 Answers2

13

In Linux you don't usually use wchar_t for library API functions. Most libraries use UTF-8 encoded strings, so they take as strings plain arrays of NUL-terminated chars (IMO that is far better than duplicating all the functions with ANSI and Unicode versions).

So, with that in mind:

  • LPCTSTR, LPCSTR, LPCWSTR -> const char *.
  • LPTSTR, LPSTR, LPWSTR -> char *.

If you insist in using Unicode functions, MS style, you have to be aware that they actually use UTF-16 encoded strings, and wchar_t is not a portable type, as its size is not specified by the language. Instead you can use uint16_t:

  • LPCWSTR -> const uint16_t *.
  • LPWSTR -> uint16_t *.

And if you want to be extra MS compatible, you can use the UNICODE macro to conditionally typedef the LPTSTR and LPTCSTR into one of the others, but that's probably unneeded for your problem.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • IBTD. Most libraries use just plain strings. Whether they are encoded in UTF-8 or in latin1 or in whatever is subject to the locale settings. If you need converting, you can take [`mbtowc()` et al.](http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/MAN/MAN3/2461____.HTM) – glglgl Feb 05 '14 at 11:28
  • @glglgl: Well, the _most_ part is arguably. But _most_ modern Linux distributions use UTF-8 locale by default (and little reason to change it), and _quite a few_ high level libraries (see GTK, Qt, Pango, Cairo...) use UTF-8 regardless of the configured locale. – rodrigo Feb 05 '14 at 11:39
5

LPTSTR - L‌ong P‌ointer to T‌CHAR STR‌ing (Don't worry, a long pointer is the same as a pointer.)

LPCSTR - L‌ong P‌ointer to a C‌onst STR‌ing

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

from msn page LPCSTR

typedef const char* LPCSTR;

from msn page LPTSTR=>

#ifdef UNICODE
 typedef LPWSTR LPTSTR;
#else
 typedef LPSTR LPTSTR;
#endif
sujin
  • 2,813
  • 2
  • 21
  • 33