char* stheParameterFileName = argv[1]; //I'm passing the file name as a parameter.
TCHAR szName [512];
我该如何将
char*
转换为TCHAR []
?char* stheParameterFileName = argv[1]; //I'm passing the file name as a parameter.
TCHAR szName [512];
char*
转换为TCHAR []
?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);
GetCommandLine
函数的结果传递给CommandLineToArgv
函数来自己生成数组。 - Cody GrayUSES_CONVERSION;
宏。 - Tadej Mali来自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应用程序。_UNICODE
,则_TCHAR
被定义为wchar_t;
否则,对于单字节和MBCS构建,它被定义为char。(wchar_t是基本的Unicode宽字符数据类型,是8位有符号char的16位对应项。)对于国际应用程序,请使用_tcs函数族,这些函数以_TCHAR单位而不是字节进行操作。例如,_tcsncpy复制n个_TCHARs,而不是n个字节。你的项目可能已经设置为使用Unicode。Unicode是为了处理地球上大多数语言的程序而设计的。如果你不需要它,可以前往项目属性/常规/字符集,并从Unicode切换到多字节。
_tmain(int argc, TCHAR *argv[])
的原因是什么?也就是说,当你第一次创建项目时,Visual Studio 最初设置的方式。 - WhozCraig