开发者

Defining buffer size correct "Stack around the variable String was corrupted"

开发者 https://www.devze.com 2023-02-09 06:27 出处:网络
I have these two lines in my code: TCHAR String[400] = {0}; SendMessageW(hwnd, WM_GETTEXT,sizeof(String), (LPARAM)String);

I have these two lines in my code:

TCHAR String[400] = {0};
SendMessageW(hwnd, WM_GETTEXT,sizeof(String), (LPARAM)String);

When I use it that way I sometimes get a runtime error: "Stack around the variable String was corrupted"

When I use

TCHAR String[400] = {0};
SendMessageW(hwnd, WM_GETTEXT,10, (LPARAM)String);

it works without problems. But I 开发者_运维问答thought that the whole thing about using sizeof() is to make sure that there is no overflow problems.... Why is sizeof(String) not secure and what could I use instead??

Thanks! and have a nice day!


You are using wchar_t, sizeof(String) will be 800, not 400. Fix:

wchar_t String[400] = 0;
SendMessageW(hwnd, WM_GETTEXT, sizeof(String) / sizeof(wchar_t), (LPARAM)String);
0

精彩评论

暂无评论...
验证码 换一张
取 消