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

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

我该如何将char*转换为TCHAR []

2
你不使用 _tmain(int argc, TCHAR *argv[]) 的原因是什么?也就是说,当你第一次创建项目时,Visual Studio 最初设置的方式。 - WhozCraig
TCHAR szName[512]; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // 使用分页文件 NULL, // 默认安全性 PAGE_READWRITE, // 读/写访问权限 0, BUF_SIZE, szName); // 映射对象的名称。因此,我只想传递我已经拥有但是在char*中的文件名作为TCHAR[]类型的szName。 - parth patel
1
你怎么能确定某个二的幂次方会在不溢出堆栈的情况下,神奇地使你的缓冲区变得足够大呢?话虽如此,如果你想显式地使用CHAR或WCHAR,那么就有A和W函数。 - Ulrich Eckhardt
1
我一直喜欢这两个链接:http://coliru.stacked-crooked.com/view?id=8c9c31ee79357616dc38839df2611671-50d9cfc8a1d350e7409e81e87c2653ba - Mooing Duck
1
如果您正在使用Unicode字符串,请在任何地方都使用它们 - Cody Gray
3个回答

11
如果您包含头文件:

If you include the header file:

#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);

请参阅MSDN上的详细信息


没错,这样做可以起作用,但意味着你的应用程序无法处理路径中的Unicode字符。更好的解决方案是按照WhozCraig的建议正确地原型化入口点,或通过将GetCommandLine函数的结果传递给CommandLineToArgv函数来自己生成数组。 - Cody Gray
5
我遇到了这个错误:错误1 错误C2065: '_lpa' : 未声明的标识符,对应于以下代码行:_tcscpy(szName, A2T(stheParameterFileName)); - qqqqq
3
为了消除C2065错误,你需要在代码块的某个位置前加上USES_CONVERSION;宏。 - Tadej Mali

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。

另请参见此处

通过使用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位有符号char的16位对应项。)对于国际应用程序,请使用_tcs函数族,这些函数以_TCHAR单位而不是字节进行操作。例如,_tcsncpy复制n个_TCHARs,而不是n个字节。

0

你的项目可能已经设置为使用Unicode。Unicode是为了处理地球上大多数语言的程序而设计的。如果你不需要它,可以前往项目属性/常规/字符集,并从Unicode切换到多字节。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接