2013-05-02 113 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切換到多字節。