0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LCMapStringは変換文字列を終端してくれない場合がある

Posted at

Win32APIのLCMapStringはある文字列をロケール依存な別の文字列にマッピング(変換)してくれる関数。(全角ひらがな→半角カタカナとか)

MSDNページのlpDestStr(変換された文字列を格納するバッファ)の解説には

cchSrc パラメータに渡す値にかかわらず、並び替えキーの最後には NULL が付けられます。
と書いてありますがどうもcchSrcに-1以外を指定すると、終端文字が付加されない模様。

std::wstring wstrSrc(L"きょうはいいてんきです");
std::unique_ptr<WCHAR[]> wzDest(new WCHAR[64]);

int iRet = 
    LCMapString(0x0411, LCMAP_KATAKANA, wstrSrc.c_str(), -1, wzDest.get(), 64);

キョウハイイテンキデスをちゃんと終端して返してくれますが、

std::wstring wstrSrc(L"きょうはいいてんきです");
std::unique_ptr<WCHAR[]> wzDest(new WCHAR[64]);

int iRet = 
    LCMapString(0x0411, LCMAP_KATAKANA, wstrSrc.c_str(), wstrSrc.length(), wzDest.get(), 64);

wzDestが終端されていません。戻り値を見て終端しましょう。

wzDest[iRet] = L'\0';
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?