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

如何将char*转换为TCHAR []如何将char *转换为TCHAR []?

+2

是否有一个特定的原因,你不只是使用'_tmain(int argc,TCHAR * argv [])',即Visual Studio第一次创建时首先设置项目的方式? – WhozCraig 2013-05-02 16:24:10

+0

TCHAR szName [512]; hMapFile =的CreateFileMapping( INVALID_HANDLE_VALUE,//使用分页文件 NULL,//默认安全 PAGE_READWRITE,//读/写访问 0, BUF_SIZE, szName的); //映射对象的名字SO,我只想传递我已经拥有的文件名,但是在char *中,作为类型为TCHAR的szName [] – 2013-05-02 16:33:04

+1

是什么让你如此确信2的一些幂会神奇地使你的缓冲区足够大,而没有溢出你的堆栈?也就是说,如果你想明确使用CHAR或WCHAR,就有* A和* W函数。 – 2013-05-02 17:19:32

回答

10

如果包含头文件:

#include "atlstr.h" 

然后你就可以使用A2T宏如下:

// 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

+0

@ John Sibly:这工作。谢谢!! – 2013-05-02 16:49:00

+0

是的,这会起作用,但这意味着你的应用程序无法处理路径中的Unicode字符。一个更好的解决方案是完成WhozCraig建议的内容并正确创建入口点的原型,或者通过将'GetCommandLine'函数的结果传递给'CommandLineToArgv'函数来自己生成数组。 – 2013-05-02 17:44:36

+4

(使用VS 2013,C++)我收到此错误:错误错误C2065:'_lpa':未声明的标识符\t 对于此行:_tcscpy(szName,A2T(stheParameterFileName)); – qqqqq 2015-06-09 18:08:00

0

形式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; 
... 
} 

的定义经常TCHAR取决于您是否使用Unicode或ANSI。

here参见:

通过使用TCHAR.H,您可以构建单字节,多字节字符集(MBCS),并从同一来源Unicode应用程序。
Tchar.h定义了具有正确预处理器定义的宏(其前缀为_tcs),并根据情况映射到str,_mbs或wcs函数。要构建MBCS,请定义符号_MBCS。要构建Unicode,请定义符号_UNICODE。要构建单字节应用程序,请不要定义(默认)。
默认情况下,为MFC应用程序定义_MBCS。 _TCHAR数据类型在Tchar.h中有条件地定义。如果为您的版本定义了符号_UNICODE,则_TCHAR被定义为wchar_t;否则,对于单字节和MBCS版本,将其定义为char。 (wchar_t是基本Unicode宽字符数据类型,与8位有符号字符是16位对应)。对于国际应用程序,请使用_tcs系列函数,它们以_TCHAR单位而不是字节运行。例如,_tcsncpy复制n _TCHAR,而不是n个字节。

0

您的项目可能被设置为使用Unicode。 Unicode适用于想要处理地球上大多数语言的程序。如果你不需要这个,去项目属性/常规/字符集,并从Unicode切换到多字节。