2016-07-14 118 views
-1

msdnのTCHARのサイズはどういう意味ですか?例えばTCHARのサイズはsizeofまたは_tcslenですか?

:TCHAR単位で

lpFilenameバッファのサイズ、。

WCHAR buf[256]; 

は、だから私は256、または512に合格する必要があります:私はバッファを持っている場合は

? _tsclen(buf)またはsizeof(buf)?

+1

256、、又はのsizeofを言うことができる(BUF)/はsizeof(TCHAR) – RbMm

+1

これは[ハンガリー表記](HTTPSの場合、です。 wikipedia.org/wiki/Hungarian_notation)が役立ちます。ほとんどのWindows API呼び出しでは、 'cch'接頭辞を使用して* Count of Characters *を指定し、* cb接頭辞は* Count of Bytes *に使用します。 * "TCHARのサイズ[...]"は*文字数*と同じです。つまり、 '_tcslen'を使います。 – IInspectable

答えて

3

sizeofは常にcharにあります。これは常にバイトであると言うことに相当します。 TCHARは必ずしもcharsではないので、sizeofは間違った答えです。あなたは、バッファ内の文字列の(TCHAR秒)の長さを渡したい場合tsclen(buf)を使用して

正しいです。バッファ自体の長さを渡す場合は、sizeof(buf)/sizeof(TCHAR)、またはより一般的にはsizeof(buf)/sizeof(buf[0])を使用できます。 WindowsはARRAYSIZEと呼ばれるマクロを提供し、この共通のイディオムを入力しないようにしています。

しかしあなただけbufが実際にバッファではなく、バッファへのポインタである場合には(そうでないsizeof(buf)はあなたが必要なものではないポインタの大きさを与える)ことを行うことができます。

いくつかの例:// EN:

(BUF)_tsclen
TCHAR buffer[MAX_PATH]; 
::GetWindowsDirectory(buffer, sizeof(buffer)/sizeof(buffer[0])); // OK 
::GetWindowsDirectory(buffer, ARRAYSIZE(buffer)); // OK 
::GetWindowsDirectory(buffer, _tcslen(buffer)); // Wrong! 
::GetWindowsDirectory(buffer, sizeof(buffer)); // Wrong! 

TCHAR message[64] = "Hello World!"; 
::TextOut(hdc, x, y, message, _tcslen(message)); // OK 
::TextOut(hdc, x, y, message, sizeof(message)); // Wrong! 
::TextOut(hdc, x, y, message, -1); // OK, uses feature of TextOut 

void MyFunction(TCHAR *buffer) { 
    // This is wrong because buffer is just a pointer to the buffer, so 
    // sizeof(buffer) gives the size of a pointer, not the size of the buffer. 
    ::GetWindowsDirectory(buffer, sizeof(buffer)/sizeof(buffer[0])); // Wrong! 

    // This is wrong because ARRAYSIZE (essentially) does what we wrote out 
    // in the previous example. 
    ::GetWindowsDirectory(buffer, ARRAYSIZE(buffer)); // Wrong! 

    // There's no right way to do it here. You need the caller to pass in the 
    // size of the buffer along with the pointer. Otherwise, you don't know 
    // how big it actually is. 
} 
+0

私の質問をもう一度読んでください。長さを渡したいと思っているのですが、あなたはtsclen(buf)と言っています。私は尋ねている:必要なものは何か:文字列の長さ、またはbufの長さ。だから私がする必要がある: " のlpFilenameバッファのサイズ、TCHARsのサイズが "であれば、tsclen(buf)またはsizeof(buf)/ sizeof(buf [0])。 ??? –

+0

これは良い答えであり、Adrianは質問をもう一度読む必要はありません。 –

+0

'_countof'もあります - これと' ARRAYSIZE'の違いは分かりません。 –

関連する問題