4
char*  stheParameterFileName = argv[1]; //I'm passing the file name as  a parameter.
TCHAR szName [512];

How can I convert char* to TCHAR []?

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
parth patel
  • 435
  • 2
  • 6
  • 15
  • 2
    Is there a specific reason you're not just using `_tmain(int argc, TCHAR *argv[])`, i.e. the way Visual Studio first setup your project to begin with when first created? – WhozCraig May 02 '13 at 16:24
  • TCHAR szName[512]; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, BUF_SIZE, szName); // name of mapping object SO, I just want to pass file name which I already have but in the char* as a szName which is of type TCHAR[] – parth patel May 02 '13 at 16:33
  • 1
    What makes you so sure that some power of two will magically make your buffer large enough while not overflowing your stack? That said, there are *A and *W functions if you want to use CHAR or WCHAR explicitly. – Ulrich Eckhardt May 02 '13 at 17:19
  • 1
    I always liked [these two](http://coliru.stacked-crooked.com/view?id=8c9c31ee79357616dc38839df2611671-50d9cfc8a1d350e7409e81e87c2653ba) – Mooing Duck May 02 '13 at 17:30
  • 1
    If you're using Unicode strings, *use them everywhere*. – Cody Gray - on strike May 02 '13 at 17:45

3 Answers3

11

If you include the header file:

#include "atlstr.h"

Then you can use the A2T macro as below:

// You'd need this line if using earlier versions of ATL/Visual Studio
// USES_CONVERSION;

char*  stheParameterFileName = argv[1];
TCHAR szName [512];
_tcscpy(szName, A2T(stheParameterFileName));
MessageBox(NULL, szName, szName, MB_OK);

Details on MSDN

John Sibly
  • 22,782
  • 7
  • 63
  • 80
  • Yeah, this will work, but it means your app can't handle Unicode characters in paths. A better solution is to do what WhozCraig suggested and prototype the entry point correctly, or generate the array yourself by passing the results of the `GetCommandLine` function to the `CommandLineToArgv` function. – Cody Gray - on strike May 02 '13 at 17:44
  • 5
    (using VS 2013, C++)I am getting this error: Error 1 error C2065: '_lpa' : undeclared identifier for this line: _tcscpy(szName, A2T(stheParameterFileName)); – qqqqq Jun 09 '15 at 18:08
  • 3
    To get rid of C2065 you need to add `USES_CONVERSION;` in front of the macros somewhere in you code block. – Tadej Mali Aug 31 '16 at 14:54
0

Form MSDN:

// convert_from_char.cpp
// compile with: /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{    
// Create and display a C style string, and then use it 
// to create different kinds of strings.
char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;

// newsize describes the length of the 
// wchar_t string called wcstring in terms of the number 
// of wide characters, not the number of bytes.
size_t newsize = strlen(orig) + 1;

// The following creates a buffer large enough to contain 
// the exact number of characters in the original string
// in the new format. If you want to add more characters
// to the end of the string, increase the value of newsize
// to increase the size of the buffer.
wchar_t * wcstring = new wchar_t[newsize];

// Convert char* string to a wchar_t* string.
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, orig, _TRUNCATE);
// Display the result and indicate the type of string that it is.
wcout << wcstring << _T(" (wchar_t *)") << endl;
...
}

The definition oft TCHAR is depending on whether you are using Unicode or ANSI.

See also here:

By using the Tchar.h, you can build single-byte, Multibyte Character Set (MBCS), and Unicode applications from the same sources.
Tchar.h defines macros (which have the prefix _tcs) that, with the correct preprocessor definitions, map to str, _mbs, or wcs functions, as appropriate. To build MBCS, define the symbol _MBCS. To build Unicode, define the symbol _UNICODE. To build a single-byte application, define neither (the default).
By default, _MBCS is defined for MFC applications. The _TCHAR data type is defined conditionally in Tchar.h. If the symbol _UNICODE is defined for your build, _TCHAR is defined as wchar_t; otherwise, for single-byte and MBCS builds, it is defined as char. (wchar_t, the basic Unicode wide-character data type, is the 16-bit counterpart to an 8-bit signed char.) For international applications, use the _tcs family of functions, which operate in _TCHAR units, not bytes. For example, _tcsncpy copies n _TCHARs, not n bytes.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

Your project may be setup to use Unicode. Unicode is for programs that want to handle most languages on planet earth. If you do not need this, go to project properties / general / character set and switch from Unicode to multi-byte.

brian beuning
  • 2,836
  • 18
  • 22