GetWindowTextのパラメータは文字数?戻り値はバイト数?

この記事は2年以上前に書いたものです。
そのため情報が古い可能性があります。ご了承ください。m(_ _)m

GetWindowText 関数を見ると、

int GetWindowText(
  HWND hWnd,        // ウィンドウまたはコントロールのハンドル
  LPTSTR lpString,  // テキストバッファ
  int nMaxCount     // コピーする最大文字数
);

パラメータ
hWnd
ウィンドウ( またはテキストを持つコントロール)のハンドルを指定します。
lpString
バッファへのポインタを指定します。このバッファにテキストが格納されます。
nMaxCount
バッファにコピーする文字の最大数を指定します。テキストのこのサイズを超える部分は、切り捨てられます。NULL 文字も数に含められます。
戻り値

関数が成功すると、コピーされた文字列の文字数が返ります( 終端の NULL 文字は含められません)。タイトルバーやテキストがない場合、タイトルバーが空の場合、および hWnd パラメータに指定したウィンドウハンドルまたはコントロールハンドルが無効な場合は 0 が返ります。拡張エラー情報を取得するには、 関数を使います。

他のアプリケーションのエディットコントロールのテキストをこの関数で取得することはできません。

とあった。nMaxCountと戻り値のどちらも、文字数とあるので、
↓こんなソース書いて動かしてみた。(hWndのタイトルには、結構眺めの日本語49文字)

char tmpBuffer[128];
int ret = GetWindowText(hWnd, tmpBuffer, 64);

すると、、、retに、98が!?

戻り値はバイト数・・・なの??(結果を見るとそうなんだけど。)

GetWindowText Function (Windows)には、

nMaxCount [in]
int

Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated.

Return Value

int

If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating NULL character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, callGetLastError.

This function cannot retrieve the text of an edit control in another application.

・maximum number of characters

・the return value is the length, in characters, of the copied string, …

ま、分からないけど。。

Microsoft Visual 2008にて。

(追記)

何が悪いって訳ではないですが、へぇ~。って思っただけ。バイト数返ってこないといろいろと面倒そうだけどね。

カテゴリー: プログラム パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です