(转)windows核心编程---CompareString函数

CompareString

    

       The CompareString function compares two character strings, using the specified locale.

[cpp]  view plain copy print ?
  1. int CompareString(  
  2.   LCID Locale,       // locale identifier  
  3.   DWORD dwCmpFlags,  // comparison-style options  
  4.   LPCTSTR lpString1, // first string  
  5.   int cchCount1,     // size of first string  
  6.   LPCTSTR lpString2, // second string  
  7.   int cchCount2      // size of second string  
  8. );  

      参数:

       此函数用来比较两字符串。第一个参数指定一个本地ID(LCID),它是32位的,确定一种特定语言。CompareString检查LCID所应用的特定语言的字符含义,来进行字符串比较。语言修正比较对最终用户产生更多的含义。但是这种比较方式比顺序比较慢。通过Windows的GetThreadLocale函数可以得到调用线程的本地ID:

[cpp]  view plain copy print ?
  1. LCID GetThreadLocale();    

       CompareString的第二个参数标记出函数比较两字符串所使用的方法。图2-4列出可能的标记:

Flag Meaning
NORM_IGNORECASE LINGUISTIC_IGNORECASE 忽略大小写。
NORM_IGNOREKANATYPE 不区分平假名和片假名。
NORM_IGNORENONSPACE LINGUISTIC_IGNOREDIACRITIC 忽略 nonspacing字符.
NORM_IGNORESYMBOLS 忽略符号。
NORM_IGNOREWIDTH 不区分相同字符的单字节和双字节字符。
SORT_STRINGSORT 将标点按符号处理。
       函数CompareString剩余的四个参数指定两个字符串及它们各自的字符长度(并非字节长度)。如果给参数cch1传了负值,函数会假定字符串pStirng1是零字符结尾,并计算字符串长度。同样,字符串pString2和参数cch2也是如此。

      返回值:

 
     函数CompareString的返回值,不像C运行期库的*cmp字符串比较函数所返回的值。CompareString返回0指示失败,CSTR_LESS_THAN(定义为1)指示pString1小于pString2CSTR_EQUAL(定义为2)指示pString1pString2相等,CSTR_GREATER_THAN(定义为3)指示pString1大于pString2。为了稍微方便些,如果函数成功,可以用返回值减去2,来使结果与C运行期库函数的返回值一致(-1,0和+1)。

     DEMO:

[cpp]  view plain copy print ?
  1. #include <Windows.h>  
  2. #include <tchar.h>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     TCHAR szBuffer[10] = {  
  9.         TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),  
  10.         TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),'\0'  
  11.     };  
  12.     LCID local = GetThreadLocale();  
  13.     int result = CompareString(local/*LOCALE_SYSTEM_DEFAULT*/,NORM_IGNORECASE,szBuffer,_countof(szBuffer),TEXT("AAAAAAAAA"),10);  
  14.     switch(result)  
  15.     {  
  16.     case 0:  
  17.         cout<<"Error"<<endl;  
  18.         break;  
  19.     case CSTR_LESS_THAN:  
  20.         cout<<"Str1 > Str2"<<endl;  
  21.         break;  
  22.     case CSTR_GREATER_THAN:  
  23.         cout<<"Str1 < Str2"<<endl;  
  24.         break;  
  25.     case CSTR_EQUAL:  
  26.         cout<<"Str1 = Str2"<<endl;  
  27.         break;  
  28.     default:  
  29.         cout<<"Don't goto there"<<endl;  
  30.         break;  
  31.     }  
  32.     cout<<result<<endl;  
  33.     return 0;  
  34. }  


附录:


Compares two Unicode strings to test binary equivalence.

Syntax

[cpp]  view plain copy print ?
  1. int CompareStringOrdinal(  
  2.   __in  LPCWSTR lpString1,  
  3.   __in  int cchCount1,  
  4.   __in  LPCWSTR lpString2,  
  5.   __in  int cchCount2,  
  6.   __in  BOOL bIgnoreCase  
  7. );  

Parameters

lpString1 [in]

Pointer to the first string to compare.

cchCount1 [in]

Length of the string indicated by lpString1. The application supplies -1 if the string is null-terminated. In this case, the function determines the length automatically.

lpString2 [in]

Pointer to the second string to compare.

cchCount2 [in]

Length of the string indicated by lpString2. The application supplies -1 if the string is null-terminated. In this case, the function determines the length automatically.

bIgnoreCase [in]

TRUE if the function is to perform a case-insensitive comparison, using the operating system uppercase table information. The application sets this parameter to FALSE if the function is to compare the strings exactly as they are passed in.

Return Value

Returns one of the following values if successful. To maintain the C runtime convention of comparing strings, the value 2 can be subtracted from a nonzero return value. Then, the meaning of <0, ==0, and >0 is consistent with the C runtime.

  • CSTR_LESS_THAN. The value indicated by lpString1 is less than the value indicated bylpString2.
  • CSTR_EQUAL. The value indicated by lpString1 equals the value indicated bylpString2.
  • CSTR_GREATER_THAN. The value indicated by lpString1 is greater than the value indicated bylpString2.

The function returns 0 if it does not succeed. To get extended error information, the application can callGetLastError, which can return one of the following error codes:

  • ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.




转自:http://blog.csdn.net/wangqiulin123456/article/details/8178099

你可能感兴趣的:((转)windows核心编程---CompareString函数)