I have issues understanding the GetWindowTextA()
function from the Win32 API.
According to the Microsoft documentation, nMaxCount
is the maximum characters to copy to buffer lpString
including the null character.
So, does GetWindowTextA()
automatically write the null termination character into to buffer lpString
or do I have to manually add the null termination character into lpString
?
This is the GetWindowTextA()
definition from Microsoft Documentation:
int GetWindowTextA(
[in] HWND hWnd,
[out] LPSTR lpString,
[in] int nMaxCount
);
Here is the code:
char M_Buff[20];
SetWindowTextA(M_SEND_EDIT_TEXT,"Hello World");
GetWindowTextA(M_SEND_EDIT_TEXT,M_Buff,20); //Do i have to add the null termination caracter into M_Buff myself or it is put automatically ?
printf("String is %s\n",M_Buff);
CodePudding user response:
There isn't much guesswork involved as long as you look at the right documentation (the SDK header files, winnt.h in particular). The interesting part here is, that LPSTR
isn't just a pointer, it also comes with an SAL annotation:
typedef _Null_terminated_ CHAR *NPSTR, *LPSTR, *PSTR;
Since GetWindowTextA
is using LPSTR
as it's _Out_
paramter type, it inherits the SAL annotations along with it.
If there were a path through the implementation of GetWindowTextA
that didn't zero-terminate the output buffer, then that would produce a build error, and there would be no User32.dll. If there is a User32.dll module in your system, you can safely assume that its implementation meets the SAL annotations. Specifically, the buffer passed into GetWindowTextA
will always be zero-terminated on return (irrespective of whether the function call succeeded or failed).
CodePudding user response:
Although the documentation for the GetWindowTextA
function is not completely unambiguous about what happens if the returned string does not get truncated (if it does, then it is clearly stated that a terminating null character is included), we can deduce that it is (or should be) always null-terminated from the definition of the argument type for lpString
, which is LPSTR
.
From the documentation for that type:
LPSTR
A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.
CodePudding user response:
If the function GetWindowTextA
reports success by returning a non-zero value, then it will also wrote a terminating null character to the memory buffer. There is no need to add one yourself.
The official documentation of that function does not clearly specify that it will always write a null character to the string. It only specifies this clearly for the edge case of the string being truncated:
If the string is as long or longer than the buffer, the string is truncated and terminated with a null character.
However, this statement does not make sense if it does not also do this for the general case of the string not being truncated. Therefore, it is safe to assume that it will also write a terminating null character when the string is not being truncated.
Also, the documentation states the following:
If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character.
This statement also implies that it will write a terminating null character to the buffer.