<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Windows API 中的字符串 > 原文: [http://zetcode.com/gui/winapi/strings/](http://zetcode.com/gui/winapi/strings/) 在 C 語言中,沒有字符串數據類型。 程序中的字符串字面值是字符數組。 每當我們說字符串時,都是指一個字符數組。 我們有五組處理字符串的函數; 在 C 運行時庫(CRT)和 Windows API 中: * ANSI C 標準函數 * 安全性增強的 CRT 函數 * Windows API 內核和用戶函數 * Windows API Shell 輕量級工具函數 * Windows API StrSafe 函數 建議您使用安全性增強的標準函數或 Windows API 安全函數。 ## ANSI C 字符串函數 C 運行時(CRT)庫函數的開銷很小,因為它們在下面調用 Windows API 函數。 這些函數提供了可移植性,但有一些限制。 如果使用不當,可能會導致安全隱患。 這些函數失敗時不會返回錯誤值。 `ansic_functions.c` ```c #include <windows.h> #include <wchar.h> #define STR_EQUAL 0 int wmain(void) { wchar_t str1[] = L"There are 15 pines"; wprintf(L"The length of the string is %lld characters\n", wcslen(str1)); wchar_t buf[20]; wcscpy(buf, L"Wuthering"); wcscat(buf, L" heights\n"); wprintf(buf); if (wcscmp(L"rain", L"rainy")== STR_EQUAL) { wprintf(L"rain and rainy are equal strings\n"); } else { wprintf(L"rain and rainy are not equal strings\n"); } return 0; } ``` 在示例中,我們介紹了 CRT 庫中的一些字符串函數。 ```c wprintf(L"The length of the string is %lld characters\n", wcslen(str1)); ``` `wcslen()`返回字符串中的寬字符數。 ```c wcscpy(buf, L"Wuthering"); ``` `wcscpy()`將字符串復制到字符串緩沖區。 ```c wcscat(buf, L" heights\n"); ``` `wcscat()`函數將字符串附加到字符串緩沖區。 ```c if (wcscmp(L"rain", L"rainy")== STR_EQUAL) { wprintf(L"rain and rainy are equal strings\n"); } else { wprintf(L"rain and rainy are not equal strings\n"); } ``` `wcscmp()`比較兩個字符串。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\ANSICFunctions>ANSICFunctions.exe The length of the string is 18 characters Wuthering heights rain and rainy are not equal strings ``` 這是示例的輸出。 ## 安全性增強的 CRT 函數 安全性 CRT 函數為 CRT 函數增加了額外的安全性。 (它們不是標準函數,而是 MS 擴展。)這些函數驗證參數,獲取大小緩沖區,檢查字符串是否以 NULL 終止以及提供增強的錯誤報告。 安全 CRT 函數的后綴為`_s`。 `security_enhanced.c` ```c #include <windows.h> #include <wchar.h> #define BUF_LEN 25 int wmain(void) { wchar_t str1[] = L"There are 15 pines"; const int MAX_CHARS = 50; size_t count = wcsnlen_s(str1, MAX_CHARS); wprintf(L"The length of the string is %ld characters\n", count); wchar_t buf[BUF_LEN] = {0}; int r = wcscpy_s(buf, BUF_LEN, L"Wuthering"); if (r != 0) { wprintf(L"wcscpy_s() failed %ld", r); } r = wcscat_s(buf, BUF_LEN, L" heights\n"); if (r != 0) { wcscat_s(L"wcscat_s() failed %ld", r); } wprintf_s(buf); return 0; } ``` 在示例中,我們展示了四個函數:`wcsnlen_s()`,`wcscpy_s()`,`wcscat_s()`和`wprintf_s()`。 ```c const int MAX_CHARS = 50; size_t count = wcsnlen_s(str1, MAX_CHARS); ``` `wcsnlen_s()`計算寬字符串的長度。 該函數僅檢查前`MAX_CHARS`個字符。 ```c int r = wcscpy_s(buf, BUF_LEN, L"Wuthering"); ``` 使用`wcscpy_s()`函數,我們將`L"Wuthering"`字符串復制到緩沖區中。 該函數使用緩沖區中的最大字符數,如果失敗,則返回錯誤代碼。 該函數成功返回 0。 ```c r = wcscat_s(buf, BUF_LEN, L" heights\n"); ``` `wcscat_s()`是`wcscat()`函數的安全擴展。 ```c wprintf_s(buf); ``` 甚至還具有增強安全性的`wprintf()`函數; 它具有一些運行時約束。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\SecurityEnhanced>SecurityEnhanced.exe The length of the string is 18 characters Wuthering heights ``` 這是`SecurityEnhanced.exe`示例的輸出。 ## Windows API 內核和用戶字符串函數 這些函數特定于 Windows OS。 它們在`User32.lib`和`Kernel32.lib`中可用。 它們比 CRT 同類產品輕。 內核字符串函數起源于 Windows 內核的開發。 它們以`l`字母為前綴。 ### 字符串長度 最常見的要求之一是弄清楚字符串的長度。 `lstrlen()`函數以字符為單位返回指定字符串的長度。 它不計算終止的空字符。 ```c int WINAPI lstrlenA(LPCSTR lpString); int WINAPI lstrlenW(LPCWSTR lpString); ``` ANSI 和 UNICODE 函數將字符串作為參數,并返回字符串中的字符數。 `winapi_string_lenght.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { char *name = "Jane"; wchar_t *town = L"Bratislava"; wprintf(L"The length of the name string is %d\n", lstrlenA(name)); wprintf(L"The town string length is %d\n", lstrlenW(town)); return 0; } ``` 我們計算兩個字符串的長度。 `lstrlen()`函數實際上是`lstrlenA()`或`lstrlenW()`的宏。 第一個用于 ANSI 字符串,第二個用于寬字符串。 ```c wprintf(L"The town string length is %d\n", lstrlenW(town)); ``` 我們使用`lstrlenW()`函數打印`L"Bratislava"`字符串的長度。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\WinapiStringLength>WinapiStringLength.exe The length of the name string is 4 The town string length is 10 ``` 這是`WinapiStringLength.exe`程序的輸出。 ### 連接字符串 `lstrcatW()`函數將一個字符串附加到另一個字符串。 ```c LPWSTR WINAPI lstrcatW(LPWSTR lpString1, LPCWSTR lpString2); ``` 第一個參數是緩沖區,其中應包含兩個字符串。 它必須足夠大以包含兩個字符,包括`NULL`終止字符。 返回值是指向緩沖區的指針。 `winapi_string_concat.c` ```c #include <windows.h> #include <wchar.h> int main(void) { wchar_t *s1 = L"ZetCode, "; wchar_t *s2 = L"tutorials "; wchar_t *s3 = L"for "; wchar_t *s4 = L"programmers.\n"; int len = lstrlenW(s1) + lstrlenW(s2) + lstrlenW(s3) + lstrlenW(s4); wchar_t buf[len+1]; lstrcpyW(buf, s1); lstrcatW(buf, s2); lstrcatW(buf, s3); lstrcatW(buf, s4); wprintf(buf); return 0; } ``` 在示例中,我們連接了四個字符串。 ```c wchar_t *s1 = L"ZetCode, "; wchar_t *s2 = L"tutorials "; wchar_t *s3 = L"for "; wchar_t *s4 = L"programmers.\n"; ``` 這些是我們要連接的字符串。 ```c int len = lstrlenW(s1) + lstrlenW(s2) + lstrlenW(s3) + lstrlenW(s4); ``` 我們使用`lstrlenW()`函數計算四個字符串的長度。 ```c wchar_t buf[len+1]; ``` 我們創建一個緩沖區來保存最終的字符串。 請注意,我們加 1 來包含`NULL`字符。 ```c lstrcpyW(buf, s1); ``` 我們使用`lstrcpyW()`函數將第一個字符串復制到緩沖區。 ```c lstrcatW(buf, s2); lstrcatW(buf, s3); lstrcatW(buf, s4); ``` 我們用`lstrcatW()`函數附加其余的字符串。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\WinapiStringConcat>WinapiStringConcat.exe ZetCode, tutorials for programmers. ``` 這是`WinapiStringConcat.exe`程序的輸出。 ### 轉換字符 我們有兩種將字符轉換為大寫或小寫的方法。 `CharLowerW()`函數將字符串或單個字符轉換為小寫。 `CharUpperW()`函數將字符串或單個字符轉換為大寫。 如果操作數是字符串,則該函數將就地轉換字符。 換句話說,它們已被修改。 ```c LPWSTR WINAPI CharLowerW(LPWSTR lpsz); LPWSTR WINAPI CharUpperW(LPWSTR lpsz); ``` 這些函數在適當位置修改字符串,并返回指向修改后的字符串的指針。 `winapi_string_case.c` ```c #include <windows.h> #include <wchar.h> #pragma comment(lib, "User32.lib") int wmain(void) { wchar_t str[] = L"Europa"; CharLowerW(str); wprintf(L"%ls\n", str); CharUpperW(str); wprintf(L"%ls\n", str); return 0; } ``` 我們有一個字符串,可以將其轉換為小寫和大寫形式。 ```c CharLowerW(str); wprintf(L"%ls\n", str); ``` 我們使用`CharLowerW()`方法將`str`字符串轉換為小寫。 字符串被修改。 ```c C:\winapi\examples2\strings\UpperLower>UpperLower.exe europa EUROPA ``` 這是`UpperLower.exe`程序的輸出。 ### 比較字符串 `lstrcmpW()`函數比較兩個字符串。 如果字符串相等,則返回 0。 比較是區分大小寫的。 這意味著`Cup`和`cup`是兩個不同的字符串。 `lstrcmpiW()`產生不區分大小寫的字符串比較。 對于此函數,`Cup`和`cup`相等。 ```c int WINAPI lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2); int WINAPI lstrcmpiW(LPCWSTR lpString1, LPCWSTR lpString2); ``` 這些函數采用兩個字符串作為參數。 返回值指示字符串的相等性。 對于相等的字符串,返回 0 值。 `winapi_string_compare.c` ```c #include <windows.h> #include <wchar.h> #define STR_EQUAL 0 int wmain(void) { wchar_t *s1 = L"Strong"; wchar_t *s2 = L"strong"; if (lstrcmpW(s1, s2) == STR_EQUAL) { wprintf(L"%ls and %ls are equal\n", s1, s2); } else { wprintf(L"%ls and %ls are not equal\n", s1, s2); } wprintf(L"When applying case insensitive comparison:\n"); if (lstrcmpiW(s1, s2) == STR_EQUAL) { wprintf(L"%ls and %ls are equal\n", s1, s2); } else { wprintf(L"%ls and %ls are not equal\n", s1, s2); } return 0; } ``` 我們有兩個字符串。 我們使用區分大小寫和不區分大小寫的字符串比較來比較它們。 ```c if (lstrcmpW(s1, s2) == STR_EQUAL) { wprintf(L"%ls and %ls are equal\n", s1, s2); } else { wprintf(L"%ls and %ls are not equal\n", s1, s2); } ``` 如果`lstrcmpW()`函數返回定義為 0 的`STR_EQUAL`,則我們向控制臺顯示兩個字符串相等。 否則,我們打印出它們不相等。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\WinapiStringCompare>WinapiStringCompare.exe Strong and strong are not equal When applying case insensitive comparison: Strong and strong are equal ``` `WinapiStringCompare.exe`程序給出以上輸出。 ### 填充緩沖區 在 C 編程中,用格式化的數據填充緩沖區至關重要。 `wsprintfW()`函數將格式化的數據寫入指定的緩沖區。 ```c int __cdecl wsprintfW(LPWSTR lpOut, LPCWSTR lpFmt, ... ); ``` 該函數的第一個參數是要接收格式化輸出的緩沖區。 第二個是包含格式控制規范的字符串。 然后,我們有一個或多個可選參數,它們對應于格式控制規范。 `winapi_string_fillbuffer.c` ```c #include <windows.h> #include <wchar.h> #pragma comment(lib, "User32.lib") int wmain(void) { SYSTEMTIME st = {0}; wchar_t buf[128] = {0}; GetLocalTime(&st); wsprintfW(buf, L"Today is %lu.%lu.%lu\n", st.wDay, st.wMonth, st.wYear); wprintf(buf); return 0; } ``` 我們建立一個用當前日期填充的字符串。 ```c wchar_t buf[128] = {0}; ``` 在這種特殊情況下,我們可以安全地假設字符串不會超過 128 個字符。 ```c GetLocalTime(&st); ``` `GetLocalTime()`函數檢索當前的本地日期和時間。 ```c wsprintfW(buf, L"Today is %lu.%lu.%lu\n", st.wDay, st.wMonth, st.wYear); ``` `wsprintfW()`用寬字符串填充緩沖區。 根據格式說明符將參數復制到字符串。 ```c wprintf(buf); ``` 緩沖區的內容將打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\WinapiStringFillBuffer>WinapiStringFillBuffer.exe Today is 11.2.2016 ``` 這是`WinapiStringFillBuffer.exe`示例的輸出。 ### 字符類型 字符有多種類型。 它們可以是數字,空格,字母,標點符號或控制字符。 ```c BOOL WINAPI GetStringTypeW(DWORD dwInfoType, LPCWSTR lpSrcStr, int cchSrc, LPWORD lpCharType); ``` `GetStringTypeW()`函數檢索指定 Unicode 字符串中字符的字符類型信息。 第一個參數是指定信息類型的標志。 | 標志 | 含義 | | --- | --- | | `CT_CTYPE1` | 檢索字符類型信息。 | | `CT_CTYPE2` | 檢索雙向布局信息。 | | `CT_CTYPE3` | 檢索文本處理信息。 | Table: Character info types 第二個參數是要檢索其字符類型的 Unicode 字符串。 第三個參數是字符串的大小。 最后一個參數是指向 16 位值數組的指針。 該數組的長度必須足夠大,以便為源字符串中的每個字符接收一個 16 位值。 該數組將包含一個與源字符串中每個字符相對應的單詞。 `GetStringTypeW()`函數返回一個值,該值是類型的組合。 我們可以使用&運算符查詢特定類型。 | 值 | 含義 | | --- | --- | | `C1_DIGIT` | 小數位數 | | `C1_SPACE` | 空格字符 | | `C1_PUNCT` | 標點 | | `C1_CNTRL` | 控制字符 | | `C1_ALPHA` | 任何語言特征 | Table: Partial list of character types 如果失敗,該函數將返回 0。 `winapi_string_types.c` ```c #include <windows.h> #include <wchar.h> #include <stdbool.h> int wmain(void) { wchar_t str[] = L"7 white, 3 red roses.\n"; int alphas = 0; int digits = 0; int spaces = 0; int puncts = 0; int contrs = 0; int size = lstrlenW(str); WORD types[size]; ZeroMemory(types, size); bool rv = GetStringTypeW(CT_CTYPE1, str, size, types); if (!rv) { wprintf(L"Could not get character types (%ld)", GetLastError()); return EXIT_FAILURE; } for (int i=0; i<size; i++) { if (types[i] & C1_ALPHA) { alphas++; } if (types[i] & C1_DIGIT) { digits++; } if (types[i] & C1_SPACE) { spaces++; } if (types[i] & C1_PUNCT) { puncts++; } if (types[i] & C1_CNTRL) { contrs++; } } wprintf(L"There are %ld letter(s), %ld digit(s), " L"%ld space(s), %ld punctuation character(s), " L"and %ld control character(s)\n", alphas, digits, spaces, puncts, contrs); return 0; } ``` 我們有一句話。 `GetStringTypeW()`函數用于確定字符串的字符類型。 ```c wchar_t str[] = L"7 white, 3 red roses.\n"; ``` 這是由各種寬字符組成的簡短句子。 ```c int alphas = 0; int digits = 0; int spaces = 0; int puncts = 0; int contrs = 0; ``` 這些變量將用于計算字母,數字,空格,標點和控制字符。 ```c int size = lstrlenW(str); WORD types[size]; ZeroMemory(types, size); ``` 我們獲取字符串的大小,并創建值數組。 大小不包含`NULL`終止字符。 我們可以加 1 以包括它。 它將被算作控制字符。 ```c bool rv = GetStringTypeW(CT_CTYPE1, str, size, types); ``` 我們得到句子的字符類型。 `types`數組填充有字符類型值。 ```c if (types[i] & C1_DIGIT) { digits++; } ``` 如果該值包含`C1_DIGIT`標志,我們將增加數字計數器。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\WinapiStringTypes>WinapiStringTypes.exe There are 13 letter(s), 2 digit(s), 5 space(s), 2 punctuation character(s), and 1 control character(s) ``` 這是`WinapiStringTypes.exe`示例的輸出。 ## Windows API Shell 輕量級工具函數 這些函數特定于 Windows OS。 它們在`Shlwapi.lib`中可用。 ### 修剪字符串 `StrTrimW()`函數從字符串中刪除指定的開頭和結尾字符。 如果刪除了任何字符,則返回`true`;否則返回`false`。 否則為假。 ```c BOOL WINAPI StrTrimW(LPWSTR psz, LPCWSTR pszTrimChars); ``` 第一個參數是指向要修剪的字符串的指針。 當此函數成功返回時,`psz`接收到修剪后的字符串。 第二個參數是一個指向字符串的指針,該字符串包含要從`psz`中修剪的字符。 `winapi_shell_trim.c` ```c #include <windows.h> #include <wchar.h> #include <stdbool.h> #include "Shlwapi.h" #pragma comment(lib, "Shlwapi.lib") int wmain(void) { wchar_t buf[] = L"23tennis74"; wchar_t trim[] = L"0123456789"; wprintf(L"Original string: %ls\n", buf); bool r = StrTrimW(buf, trim); if (r == true) { wprintf(L"The StrTrim() trimmed some characters\n", buf); } else { wprintf(L"No characters were trimmed\n", buf); } wprintf(L"Trimmed string: %ls\n", buf); return 0; } ``` 在該示例中,我們從字符串中刪除所有數字。 ```c wchar_t buf[] = L"23tennis74"; ``` 我們將從該字符串中刪除所有數字。 ```c wchar_t trim[] = L"0123456789"; ``` 該字符串包含所有要刪除的字符。 ```c bool r = StrTrimW(buf, trim); ``` 使用`StrTrimW()`函數,我們可以修剪緩沖區中的數字。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\ShellTrimString>ShellTrimString.exe Original string: 23tennis74 The StrTrim() trimmed some characters Trimmed string: tennis ``` 這是`ShellTrimString.exe`示例的輸出。 ### 將字符串轉換為整數 `StrToIntExW()`將代表十進制或十六進制數字的字符串轉換為整數。 成功時該函數返回`true`。 ```c BOOL WINAPI StrToIntExW(LPCWSTR pszString, DWORD dwFlags, int *piRet); ``` 第一個參數是指向要轉換的字符串的指針。 第二個參數是標志之一,用于指定應如何解析`pszString`以將其轉換為整數。 第三個參數是指向接收轉換后的字符串的`int`的指針。 `winapi_shell_convert.c` ```c #include <windows.h> #include <wchar.h> #include <stdbool.h> #include "Shlwapi.h" #pragma comment(lib, "Shlwapi.lib") int wmain(void) { wchar_t str1[] = L"512"; wchar_t str2[] = L"0xAB12"; int n = 0; bool r = StrToIntExW(str1, STIF_DEFAULT, &n); if (r == true) { wprintf(L"The value is %d\n", n); } else { wprintf(L"The first conversion failed\n"); return 1; } r = StrToIntExW(str2, STIF_SUPPORT_HEX, &n); if (r == true) { wprintf(L"The value is %d\n", n); } else { wprintf(L"The second conversion failed\n"); return 1; } return 0; } ``` 在示例中,我們轉換了兩個字符串; 一個代表十進制值,一個代表十六進制值。 ```c wchar_t str1[] = L"512"; wchar_t str2[] = L"0xAB12"; ``` 第一個字符串代表十進制數;第二個字符串代表十進制數。 第二個字符串表示一個十六進制數。 ```c bool r = StrToIntExW(str1, STIF_DEFAULT, &n); ``` 使用`StrToIntExW()`函數,我們將第一個字符串轉換為整數。 `STIF_DEFAULT`標志告訴函數轉換十進制值。 ```c r = StrToIntExW(str2, STIF_SUPPORT_HEX, &n); ``` 通過`STIF_SUPPORT_HEX`標志,我們告訴函數轉換一個十六進制值。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\ShellConvertString>ShellConvertString.exe The value is 512 The value is 43794 ``` 這是`ShellConvertString.exe`程序的輸出。 ### 搜索字符串 `StrStrW()`函數查找字符串中子字符串的首次出現。 比較是區分大小寫的。 ```c LPWSTR WINAPI StrStrW(LPCWSTR pszFirst, LPCWSTR pszSrch); ``` 第一個參數是指向要搜索的字符串的指針。 第二個參數是指向要搜索的子字符串的指針。 如果成功,該函數將返回匹配子字符串的第一個匹配項的地址,否則返回`NULL`。 `winapi_shell_search.c` ```c #include <windows.h> #include <wchar.h> #include "Shlwapi.h" #pragma comment(lib, "Shlwapi.lib") int wmain(void) { wchar_t buf[] = L"Today is a rainy day."; wchar_t *search_word = L"rainy"; int len = wcslen(search_word); LPWSTR pr = StrStrW(buf, search_word); if (pr == NULL) { wprintf(L"No match\n", buf); } else { wprintf(L"%.*ls is found\n", len, pr); } return 0; } ``` 在代碼示例中,我們在句子中搜索單詞。 ```c wchar_t buf[] = L"Today is a rainy day."; ``` 我們從這句話中搜索一個單詞。 ```c wchar_t *search_word = L"rainy"; ``` 這是我們要搜索的詞。 ```c LPWSTR pr = StrStrW(buf, search_word); ``` `StrStrW()`函數在句子中搜索單詞。 如果成功,它將返回一個指向匹配子字符串的指針。 ```c C:\Users\Jano\Documents\Pelles C Projects\strings\ShellSearchString>ShellSearchString.exe rainy is found ``` 這是`ShellSearchString.exe`程序的輸出。 ## Windows API StrSafe 函數 為了提高應用的安全性,發布了 StrSafe 函數。 這些函數需要目標緩沖區的大小作為輸入。 保證緩沖區為空終止。 該函數返回錯誤代碼。 這樣可以進行正確的錯誤處理。 每個函數都有相應的字符計數`Cch`或字節計數`Cb`版本。 ### 字符串長度 `StringCchLengthW()`和`StringCbLengthW()`函數可以確定字符和字節的字符串長度。 ```c HRESULT StringCchLengthW(LPCWSTR psz, size_t cchMax, size_t *pcch); HRESULT StringCbLengthW(LPCWSTR psz, size_t cbMax, size_t *pcb); ``` 函數的第一個參數是要檢查其長度的字符串。 第二個參數是`psz`參數中允許的最大字符數(字節)。 該值不能超過`STRSAFE_MAX_CCH`。 第三個參數是`psz`中的字符數(字節),不包括終止的空字符。 函數成功返回`S_OK`,失敗返回`STRSAFE_E_INVALID_PARAMETER`。 如果`psz`中的值為`NULL`,`cchMax`大于`STRSAFE_MAX_CCH`或`psz`大于`cchMax`,則函數將失敗。 `SUCCEEDED`和`FAILED`宏可用于檢查函數的返回值。 `safe_length.c` ```c #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; } ``` 該代碼示例確定給定字符串(以字符和字節為單位)的長度。 ```c wchar_t str[] = L"ZetCode"; ``` 我們將確定此字符串的長度。 ```c size_t target_size = 0; ``` 函數返回時,`target_size`變量將填充計數值。 ```c size_t size = sizeof(str); ``` 使用`sizeof`運算符,我們獲得字符數組的大小(以字節為單位)。 該值用作`StringCbLengthW()`函數的字符串中允許的最大字符數。 ```c HRESULT r = StringCbLengthW(str, size, &target_size); ``` 使用`StringCbLengthW()`函數,我們可以確定字符串的長度(以字節為單位)。 長度存儲在`target_size`變量中。 ```c if (SUCCEEDED(r)) { wprintf(L"The string has %lld bytes\n", target_size); } else { wprintf(L"StringCbLengthW() failed\n"); return 1; } ``` 我們使用`SUCCEEDED`宏檢查返回的值。 成功后,我們將打印字符串中的字節數。 如果出現錯誤,我們將顯示一條錯誤消息。 ```c size = sizeof(str)/sizeof(wchar_t); ``` 在這里,我們確定字符串中允許的最大字符數。 `wchar_t`是一種寬字符類型; 它的大小取決于編譯器。 ```c r = StringCchLengthW(str, size, &target_size); ``` 使用`StringCchLengthW()`函數,我們可以得到字符串的大小(以字符為單位)。 ```c if (SUCCEEDED(r)) { wprintf(L"The string has %lld characters\n", target_size); } else { wprintf(L"StringCchLengthW() failed\n"); return 1; } ``` 成功后,我們將字符串中的字符數打印到控制臺。 出現錯誤時,我們會打印一條錯誤消息。 ```c C:\Users\Jano\Documents\Pelles C Projects\strsafe\SafeLength>SafeLength.exe The string has 14 bytes The string has 7 characters ``` 該字符串包含 14 個字節或 7 個字符。 ### 讀取標準輸入 `StringCchGetsW()`從標準輸入讀取一行,包括換行符。 ```c HRESULT StringCchGetsW(LPWSTR pszDest, size_t cchDest); ``` 第一個參數是目標緩沖區,它接收復制的字符。 第二個參數是目標緩沖區的大小(以字符為單位)。 `safe_gets.c` ```c #include <windows.h> #include <strsafe.h> #include <wchar.h> #define BUF_LEN 8191 int wmain(void) { wchar_t buf[BUF_LEN] = {0}; HRESULT r = StringCchGetsW(buf, ARRAYSIZE(buf)); if (SUCCEEDED(r)) { wprintf(L"You have entered: %ls\n", buf); } else { wprintf(L"StringCchGets() failed\n"); return 1; } return 0; } ``` 在示例中,我們從標準輸入中讀取了一行。 該行被打印回到控制臺。 ```c #define BUF_LEN 8191 ``` 根據 MSDN 文檔,命令提示符下的最大輸入不能超過 8191 個字符。 ```c wchar_t buf[BUF_LEN] = {0}; ``` 我們為輸入字符串創建一個緩沖區。 ```c HRESULT r = StringCchGetsW(buf, ARRAYSIZE(buf)); ``` `StringCchGetsW()`從`stdin`讀取一行。 ```c C:\Users\Jano\Documents\Pelles C Projects\strsafe\SafeGets>SafeGets.exe Today is a rainy day. You have entered: Today is a rainy day. ``` 這是`SafeGets.exe`程序的示例運行。 ### 復制字符串 `StringCchCopyW()`將一個字符串復制到另一個。 ```c HRESULT StringCchCopyW(LPTSTR pszDest, size_t cchDest, LPCWSTR pszSrc); ``` 第一個參數是目標緩沖區,它接收復制的字符串。 第二個參數是目標緩沖區的大小(以字符為單位)。 第三個參數是源字符串。 `safe_copy.c` ```c #include <windows.h> #include <strsafe.h> #include <wchar.h> int wmain(void) { wchar_t *sentence = L"Today is a rainy day."; size_t size = wcslen(sentence) + 1; wchar_t buf[size]; ZeroMemory(buf, size); HRESULT r = StringCchCopyW(buf, size, sentence); if (SUCCEEDED(r)) { wprintf(L"%ls\n", buf); } else { wprintf(L"StringCchCopyW() failed\n"); return 1; } return 0; } ``` 在代碼示例中,我們使用`StringCchCopyW()`函數復制一個字符串。 ```c wchar_t *sentence = L"Today is a rainy day."; ``` 這是要復制的字符串。 ```c size_t size = wcslen(sentence) + 1; ``` 我們用`wcslen()`函數確定它的長度; 換行符保留一個字符。 ```c wchar_t buf[size]; ZeroMemory(buf, size); ``` 我們使用`ZeroMemory()`函數創建一個緩沖區,并使用零。 ```c HRESULT r = StringCchCopyW(buf, size, sentence); ``` 使用`StringCchCopyW()`,我們將字符串復制到緩沖區中。 提供目標緩沖區的大小是為了確保它不會在該緩沖區的末尾寫入。 ```c C:\Users\Jano\Documents\Pelles C Projects\strsafe\SafeCopy>SafeCopy.exe Today is a rainy day. ``` 這是`SafeCopy.exe`程序的輸出。 ### 連接字符串 `StringCchCatW()`將一個字符串連接到另一個字符串。 ```c HRESULT StringCchCatW(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc); ``` 第一個參數是目標緩沖區。 第二個參數是目標緩沖區的大小(以字符為單位)。 第三個參數是要連接到目標緩沖區末尾的源字符串。 `safe_concat.c` ```c #include <windows.h> #include <strsafe.h> #include <wchar.h> #define BUF_LEN 256 int wmain(void) { wchar_t buf[BUF_LEN] = {0}; HRESULT r = StringCchCatW(buf, BUF_LEN, L"Hello "); if (FAILED(r)) { wprintf(L"StringCchCatW() failed\n"); return 1; } r = StringCchCatW(buf, BUF_LEN, L"there"); if (FAILED(r)) { wprintf(L"StringCchCatW() failed\n"); return 1; } wprintf(L"%ls\n", buf); return 0; } ``` 在代碼示例中,我們使用`StringCchCatW()`函數將兩個字符串連接在一起。 ```c HRESULT r = StringCchCatW(buf, BUF_LEN, L"Hello "); ``` `StringCchCatW()`函數將`L"Hello "`字符串添加到`buf`數組。 ```c r = StringCchCatW(buf, BUF_LEN, L"there"); ``` 稍后,第二個字符串將添加到緩沖區。 ```c C:\Users\Jano\Documents\Pelles C Projects\strsafe\SafeConcat>SafeConcat.exe Hello there ``` 這是`SafeConcat.exe`程序的輸出。 ### 格式化字符串 `StringCchPrintfW()`函數將格式化的數據寫入目標緩沖區。 ```c HRESULT StringCchPrintfW(LPWSTR pszDest, size_t cchDest, LPCWSTR pszFormat, ...); ``` 第一個參數是目標緩沖區,它接收從`pszFormat`及其參數創建的格式化字符串。 第二個參數是目標緩沖區,以字符為單位。 第三個參數是格式字符串。 將以下參數插入`pszFormat`字符串中。 `safe_format.c` ```c #include <windows.h> #include <strsafe.h> #include <wchar.h> #define BUF_LEN 256 int wmain(void) { wchar_t *word = L"table"; int count = 6; wchar_t buf[BUF_LEN] = {0}; wchar_t *line = L"There are %d %lss"; HRESULT r = StringCchPrintfW(buf, ARRAYSIZE(buf), line, count, word); if (SUCCEEDED(r)) { wprintf(L"%ls\n", buf); } else { wprintf(L"StringCchPrintfW() failed\n"); return 1; } return 0; } ``` 在代碼示例中,我們使用`StringCchPrintfW()`函數創建格式化的字符串。 ```c wchar_t *line = L"There are %d %lss"; ``` 這是格式字符串; 它具有兩個格式說明符:`%d`和`%ls`。 ```c HRESULT r = StringCchPrintfW(buf, ARRAYSIZE(buf), line, count, word); ``` 使用`StringCchPrintfW()`函數,我們將兩個值插入目標緩沖區。 ```c C:\Users\Jano\Documents\Pelles C Projects\strsafe\SafeFormat>SafeFormat.exe There are 6 tables ``` 這是`SafeFormat.exe`程序的輸出。 在 Windows API 教程的這一部分中,我們使用了字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看