简体   繁体   中英

Defining buffer size correct “Stack around the variable String was corrupted”

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM