mirror of
https://github.com/janbodnar/Windows-API-examples.git
synced 2024-11-24 11:25:30 +00:00
Create safe_length.c
This commit is contained in:
parent
a52d337904
commit
1448149ea2
37
strings/safe_length.c
Normal file
37
strings/safe_length.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <windows.h>
|
||||
#include <strsafe.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int wmain(void) {
|
||||
|
||||
wchar_t str[] = L"ZetCode";
|
||||
size_t target_size = 0;
|
||||
|
||||
size_t size = sizeof(str);
|
||||
|
||||
HRESULT r = StringCbLengthW(str, size, &target_size);
|
||||
|
||||
if (SUCCEEDED(r)) {
|
||||
|
||||
wprintf(L"The string has %lld bytes\n", target_size);
|
||||
} else {
|
||||
|
||||
wprintf(L"StringCbLengthW() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size = sizeof(str)/sizeof(wchar_t);
|
||||
|
||||
r = StringCchLengthW(str, size, &target_size);
|
||||
|
||||
if (SUCCEEDED(r)) {
|
||||
|
||||
wprintf(L"The string has %lld characters\n", target_size);
|
||||
} else {
|
||||
|
||||
wprintf(L"StringCchLengthW() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user