Reputation: 17
The "sz" part of the prefix is important, because some strings in the Windows world (especially when talking about the DDK) are not zero-terminated.reading this in STR,LPSTR section
can anyone tell me what are those non null terminated string?
Upvotes: 0
Views: 2523
Reputation: 2884
In windows kernel programming, the most often used string type is UNICODE_STRING, a non-null terminated string type:
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING
The purpose of this data structure is to efficiently processing string along the stack drivers. Each driver in the stack may append text or modify the string in the range of "MaximumLenth" without allocating a new buffer.
For example, below is a typical unicode string stored in a continuous 64 bytes buffer:
address + 0 : 22 (Length)
address + 4 : 48 (MaximumLength)
address + 8 : buffer + 16 (Buffer)
address + 16: "Hello World" (UTF16 string, may without null terminated)
The standard string manipulating function can not use on UNICODE_STRING instead you should use the Rtl*UnicodeString() functions.
Upvotes: 1
Reputation: 2769
It would be easier to answer when we can use non null terminated strings.
Some API functions take only string pointer (SetWindowText
, CreateFile
) and strings have to be terminated with null character. Other functions (ExtTextOut
, WriteConsole
) take pointer and some form of length (usually number of char
s, TCHAR
s or wchar_t
s. These strings don't have to be terminated by null character.
// No termination NUL charcter bellow.
TCHAR hello[] = { 'H','E','L','L','O' };
ExtTextOut( hdc, 100, 100, 0, hello, 5, 0 );
TCHAR hello2[] = _T("HELLO WORLD!");
ExtTextOut( hdc, 100, 100, 0, hello2, 5, 0 );
In second ExtTextOut
we don't have to artificially cut hello2
string (or copy it to temporary buffer). This function allows to use parts of string without null termination requirements.
Upvotes: -1
Reputation: 222323
In computer science, a string is a sequence of characters. A sequence has some length—there are some number of characters in it. To work with a string, one generally has to know the length of the string.
The length may be indicated in various ways. One way is to indicate the end of the sequence with a sentinel value, which is simply a chosen value that is not used in the sequence. With character strings, it is common to use zero as a sentinel: The string continues from its start until a zero character is found. When using a sentinel, the sentinel value cannot appear inside the string, since it marks the end.
Another way to indicate the length is to keep it separately from the string. For example, the length is passed to the C memcmp
routine as a separate parameter. This allows memcmp
to compare arbitrary sequences of bytes in memory, including sequences that contain zero bytes.
Sometimes the length is treated as part of the data structure for the string. It might be in the first byte or first several bytes of the string. So software using the string would get the length by reading the first byte, and the bytes after that would contain the characters of the string.
Another method, related to the sentinel method, is to use delimiters. For example, we commonly write strings such as "abc"
in source code, text, and in shell commands. The quote marks are delimiters that mark the beginnings and ends of strings. Various methods are used to allow the delimiters themselves to be characters in the strings, such as “quoting” the delimiters with other special characters, as in: "This is a quote mark: \"."
.
In summary, the concept of a string that is not null-terminated is broad and open: Any method of indicating the length of a string other than marking the end with a null character is a string that is not null-terminated.
Upvotes: 3