【问题标题】:How to convert char* to TCHAR[ ]? [duplicate]如何将 char* 转换为 TCHAR[ ]? [复制]
【发布时间】:2013-05-02 16:19:06
【问题描述】:
char*  stheParameterFileName = argv[1]; //I'm passing the file name as  a parameter.
TCHAR szName [512];

如何将char* 转换为TCHAR []

【问题讨论】:

  • 您不只是使用 _tmain(int argc, TCHAR *argv[]) 是否有特定原因,即 Visual Studio 在首次创建项目时首先设置项目的方式?
  • TCHAR szName[512]; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // 使用分页文件 NULL, // 默认安全 PAGE_READWRITE, // 读/写访问 0, BUF_SIZE, szName); // 映射对象的名称 SO,我只想将我已经拥有但在 char* 中的文件名作为 TCHAR[] 类型的 szName 传递
  • 是什么让您如此确定,2 的幂会神奇地使您的缓冲区足够大,同时不会溢出堆栈?也就是说,如果您想明确使用 CHAR 或 WCHAR,则有 *A 和 *W 函数。
  • 我一直很喜欢these two
  • 如果您使用的是 Unicode 字符串,请在任何地方使用它们

标签: c++ winapi visual-studio-2012 tchar


【解决方案1】:

如果包含头文件:

#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

【讨论】:

  • @John Sibly:这行得通。谢谢!!
  • 是的,这会起作用,但这意味着您的应用无法处理路径中的 Unicode 字符。更好的解决方案是按照 WhozCraig 的建议做正确的入口点原型,或者通过将 GetCommandLine 函数的结果传递给 CommandLineToArgv 函数自己生成数组。
  • (使用 VS 2013,C++)我收到此错误:错误 1 ​​错误 C2065: '_lpa' : undeclared identifier for this line: _tcscpy(szName, A2T(stheParameterFileName));
  • 要摆脱 C2065,您需要在代码块中某处的宏前面添加 USES_CONVERSION;
【解决方案2】:

表格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。要构建单字节应用程序,两者都不定义(默认)。
默认情况下,_MBCS 是为 MFC 应用程序定义的。 _TCHAR 数据类型在 Tchar.h 中有条件地定义。如果符号 _UNICODE 为您的构建定义,_TCHAR 定义为 wchar_t; 否则,对于单字节和 MBCS 构建,它被定义为 char。 (wchar_t 是基本的 Unicode 宽字符数据类型,是 8 位有符号字符的 16 位对应物。)对于国际应用程序,使用 _tcs 系列函数,它以 _TCHAR 单位而不是字节进行操作。例如,_tcsncpy 复制 n 个 _TCHAR,而不是 n 个字节。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多