미분류

null termiate 로 std::string::append 가 동작하지 않게 된다?

bemaru 2020. 11. 21. 00:01

아래 샘플 코드는 마소에서 퍼온것이다.

마소 샘플도 너무 믿어선 안된다.

요약하면 win32api의 버퍼로 std::wstring을 사용하는 예제에 대한 내용이다.

 

아래처럼 사용하면 어떤 문제가 있을까?

 

// Get the length of the text string
// (Note: +1 to consider the terminating NUL)
const int bufferLength = ::GetWindowTextLength(hWnd) + 1;
// Allocate string of proper size
std::wstring text;
text.resize(bufferLength);
// Get the text of the specified control
// Note that the address of the internal string buffer
// can be obtained with the &text[0] syntax
::GetWindowText(hWnd, &text[0], bufferLength);
// Resize down the string to avoid bogus double-NUL-terminated strings
text.resize(bufferLength - 1);

ref : docs.microsoft.com/en-us/archive/msdn-magazine/2015/july/c-using-stl-strings-at-win32-api-boundaries

 

null termiate 로 std::string::append 가 동작하지 않게 된다?

 

 

 

 

 

반응형

'미분류' 카테고리의 다른 글

정리  (0) 2020.11.22
goto is evil?  (0) 2020.11.21
compressed by cab  (0) 2020.06.04
TDD  (0) 2020.05.07
[백준][2156번] 포도주 시식  (0) 2020.05.05