Given a WinAPI function which returns it's result via a C style string OUT parameter e.g.:
int WINAPI GetWindowTextW(
_In_ HWND hWnd,
_Out_ LPTSTR lpString,
_In_ int nMaxCount
);
Is there a better way of using the function than what I'm doing below?
HWND handle; // Assume this is initialised to contain a real window handle
std::wstring title;
wchar_t buffer[512];
GetWindowTextW(handle, buffer, sizeof(buffer));
title = buffer;
The above code works, but I have the following issues with it:
The buffer size is completely arbitrary since I have no way to know the length of the string that the function might return. This "feels" wrong to me - I have always tried to avoid magic numbers in my code.
If the function returns a string which is larger than the buffer, it will get truncated - this is bad!
Whenever the function returns a string which is smaller than the buffer, I will be wasting memory. This is not as bad as (2), but I'm not thrilled about the idea of setting aside large chunks of memory (e.g. 1024 bytes in my example above) for something that might only need a few bytes in practice.
Are there any other alternatives?