Questions tagged [tchar]

A #define for either char or wchar_t, used for porting ancient windows applications

TCHAR and related preprocessor-defines are used in two places:

  1. Porting ancient windows application to modern UTF-16 APIs.
  2. Describing APIs available in both ANSI and UTF-16 flavor.

If neither of the above fits for you, you really should not be using it:
Is TCHAR still relevant?

#ifdef _UNICODE
    #define TCHAR wchar_t
    // many others
#else
    #define TCHAR char
    // many others
#endif
164 questions
34
votes
6 answers

How to convert a TCHAR array to std::string?

How do I convert a TCHAR array to std::string (not to std::basic_string)?
ashmish2
  • 2,885
  • 8
  • 40
  • 54
24
votes
2 answers

tchar.h on linux

I am trying to write cross platform i18n C++ code. Since most linux system prefer to use UTF-8 as the character encoding, I thought that I should use string on linux and wstring on Windows. Is tchar.h available on linux? What is an equivalent…
Sudarshan
  • 263
  • 1
  • 2
  • 6
23
votes
5 answers

Converting TCHAR to string in C++

I'm trying to convert a TCHAR to a string as in: std::string mypath; TCHAR path[MAX_PATH]; GetModuleFileName( NULL, path, MAX_PATH ); I need to set mypath to that of path. I did a simple loop and concatenated path[index] to mypath and this works…
David
  • 241
  • 1
  • 2
  • 3
18
votes
8 answers

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms)?

This seems like a pretty softball question, but I always have a hard time looking up this function because there seem there are so many variations regarding the referencing of char and tchar.
CrashCodes
  • 3,237
  • 12
  • 38
  • 42
17
votes
2 answers

How to assign a value to a TCHAR array

I have a TCHAR array in my C++ code which I want to assign static strings to it. I set an initial string to it via TCHAR myVariable[260] = TEXT("initial value"); Everything works fine on this. However, when I split it in two lines as in TCHAR…
Etan
  • 17,014
  • 17
  • 89
  • 148
14
votes
4 answers

Convert TCHAR array to char array

How to convert to TCHAR[] to char[] ?
maxy
  • 438
  • 3
  • 10
  • 20
9
votes
4 answers

Concatenating a stack string with a heap string gives odd results

I am sure the following has a rational explanation but I am nevertheless a bit baffled. The issue is with a function which creates a _TCHAR[CONSTANT], a _TCHAR*, concatenates them and returns the result. For some reason the call to whatTheHeck()…
Saul
  • 17,973
  • 8
  • 64
  • 88
9
votes
2 answers

Is it advisable to use strcmp or _tcscmp for comparing strings in Unicode versions?

Is it advisable to use strcmp or _tcscmp for comparing strings in Unicode versions?
ckv
  • 10,539
  • 20
  • 100
  • 144
8
votes
5 answers

should I eliminate TCHAR from Windows code?

I am revising some very old (10 years) C code. The code compiles on Unix/Mac with GCC and cross-compiles for Windows with MinGW. Currently there are TCHAR strings throughout. I'd like to get rid of the TCHAR and use a C++ string instead. Is it still…
vy32
  • 28,461
  • 37
  • 122
  • 246
8
votes
4 answers

Is there a format specifier that always means char string with _tprintf?

When you build an app on Windows using TCHAR support, %s in _tprintf() means char * string for Ansi builds and wchar_t * for Unicode builds while %S means the reverse. But are there any format specifiers that always mean char * string no matter if…
hippietrail
  • 15,848
  • 18
  • 99
  • 158
8
votes
2 answers

Differentiate between TCHAR and _TCHAR

What are the various differences between the two symbols TCHAR and _TCHAR type defined in the Windows header tchar.h? Explain with examples. Briefly describe scenarios where you would use TCHAR as opposed to _TCHAR in your code. (10 marks)
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
8
votes
3 answers

Errors using TCHAR,cannot convert to wchar_t

I've been asked to add functionality to an existing old project but i cannot get it to build. It handles unicode strings but i get a lot of errors regarding the use of TCHAR.Specifically almost every error is TCHAR cannot be converted to or used as…
yiannis
  • 440
  • 1
  • 6
  • 19
8
votes
4 answers

How do I convert from _TCHAR * to char * when using C++ variable-length args?

We need to pass a format _TCHAR * string, and a number of char * strings into a function with variable-length args: inline void FooBar(const _TCHAR *szFmt, const char *cArgs, ...) { //... } So it can be called like so: char *foo = "foo"; char…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
7
votes
2 answers

Why is there a macro which defines _tmain?

I am new to C++ coding, coming from Java and C# background. I'm puzzled by the proliferation of #define terms starting with the most basic: #define _tmain wmain When I first learned a smattering of C ages ago, the main function was: int main(int…
Sam Goldberg
  • 6,711
  • 8
  • 52
  • 85
7
votes
4 answers

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

I get a compilation error on the line: MessageBox(e.getAllExceptionStr().c_str(), _T("Error initializing the sound player")); Error 4 error C2664: 'CWnd::MessageBoxA' : cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' …
gornvix
  • 3,154
  • 6
  • 35
  • 74
1
2 3
10 11