win-capture: Use stack buffer for small window titles

Avoids expensive malloc calls that might also contribute to excessive
heap fragmentation.
This commit is contained in:
Richard Stanway 2022-01-18 00:22:39 +01:00
parent ecf8c1239d
commit d458780f02
No known key found for this signature in database
GPG Key ID: 4F96FCA24BCE7BA1

View File

@ -105,17 +105,29 @@ fail:
void get_window_title(struct dstr *name, HWND hwnd)
{
wchar_t *temp;
int len;
len = GetWindowTextLengthW(hwnd);
if (!len)
return;
temp = malloc(sizeof(wchar_t) * (len + 1));
if (GetWindowTextW(hwnd, temp, len + 1))
dstr_from_wcs(name, temp);
free(temp);
if (len > 1024) {
wchar_t *temp;
temp = malloc(sizeof(wchar_t) * (len + 1));
if (!temp)
return;
if (GetWindowTextW(hwnd, temp, len + 1))
dstr_from_wcs(name, temp);
free(temp);
} else {
wchar_t temp[1024 + 1];
if (GetWindowTextW(hwnd, temp, len + 1))
dstr_from_wcs(name, temp);
}
}
void get_window_class(struct dstr *class, HWND hwnd)