The size parameter from function

239 Views Asked by At

I see that a lot of functions need you to set a size for the string which is the output.

GetComputerNameW needs:

WCHAR wStrName[16U];
DWORD uSize = 16U;
GetComputerNameW(wStrName, &uSize);

RegSetValueExW needs:

WCHAR wStrExec[1024U];
RegSetValueExW(..., (wcslen(wStrExec) + 1U) * sizeof(WCHAR));

GetWindowTextW needs:

WCHAR wStrText[1024U];
GetWindowsTextW(..., sizeof(wStrText));

GetModuleBaseNameW needs:

WCHAR wStrName[1024U];
GetModuleBaseNameW(..., sizeof(wStrName) / sizeof(WCHAR));

My question is, how to make the difference between the sizes set? The strings are always defined as WCHAR and the sizes set differ so much.

1

There are 1 best solutions below

2
On

If you carefully read the documentation, you'll see that the size parameter is the size of the output buffer in bytes typically:

cbData [in]

The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters.

Which of course yields that you need to:

WCHAR wStrExec[1024U];
RegSetValueExW(..., sizeof (wStrExec));