How to convert char* to TCHAR[ ]? [duplicate]
Asked Answered
V

3

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

How can I convert char* to TCHAR []?

Visualize answered 2/5, 2013 at 16:19 Comment(5)
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?Deshawndesi
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[]Visualize
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.Isaacs
I always liked these twoVitek
If you're using Unicode strings, use them everywhere.Stichometry
W
12

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

Wingspan answered 2/5, 2013 at 16:34 Comment(3)
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.Stichometry
(using VS 2013, C++)I am getting this error: Error 1 error C2065: '_lpa' : undeclared identifier for this line: _tcscpy(szName, A2T(stheParameterFileName));Bardo
To get rid of C2065 you need to add USES_CONVERSION; in front of the macros somewhere in you code block.Palladin
S
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.

Shadchan answered 2/5, 2013 at 16:27 Comment(0)
C
-1

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.

Chokeberry answered 2/5, 2013 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.