Last active
November 4, 2015 13:14
-
-
Save yuba/b8c47e8a59234f3db78c to your computer and use it in GitHub Desktop.
バッファが足りないと結果が切り捨てられる文字列取得関数を自動リトライするユーティリティ関数 ref: http://qiita.com/yuba/items/fc9c677d45ed7889c884
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// バッファが足りないと結果が切り捨てられる文字列取得関数を、バッファを拡大しながら呼び出し続けて全内容を取得します | |
template<typename TChar, typename PFunc, typename... TParams> | |
std::basic_string<TChar> challenge_get_string(PFunc f, TParams... params) | |
{ | |
vector<TChar> buf(256); | |
size_t result_len; | |
for (;; buf.resize(buf.size() * 2)) | |
{ | |
result_len = f(params..., buf.data(), buf.size()); | |
if (result_len < buf.size() - 1) break; | |
} | |
return std::basic_string<TChar>(buf.data(), result_len); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ウィンドウのキャプションを取得します | |
std::wstring get_window_text(HWND hWnd) | |
{ | |
return challenge_get_string<wchar_t>(&GetWindowTextW, hWnd); | |
} | |
// リソースから文字列を取得します | |
std::wstring load_resource_string(UINT id) | |
{ | |
return challenge_get_string<wchar_t>(&LoadStringW, GetModuleHandle(NULL), id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment