title: How to use CompareString function so it works on both Windows XP and Windows 7
body: <p>I have an application built on Windows 7 SP1 Visual Studio 2010 SP1.</p>
  
  <p>It seems <code>CompareString</code> doesn't work the same way on Windows 7 and Windows XP. I am creating an  <code>EndsWith</code>/<code>StartsWith</code>-like (See C# <code>String.EndsWith</code>) methods, but where it works on Windows 7 as expected, but not on Windows XP.</p>
  
  <p>Here are my <code>StartsWith</code> and <code>EndsWith</code>:</p>
  
  <pre><code>bool String::StartsWith( const String&amp; value, bool bCaseSensitive ) const
  {
      if(this-&gt;strLen == 0 || value.strLen == 0)
          return false;
  
      DWORD flags;
      if(bCaseSensitive == false)
          flags = LINGUISTIC_IGNORECASE;
      else
          flags = NORM_LINGUISTIC_CASING;
  
      if( CSTR_EQUAL == CompareStringW(LOCALE_USER_DEFAULT, flags, this-&gt;_str, static_cast&lt;int&gt;(value.strLen), value._str, static_cast&lt;int&gt;(value.strLen)) )
          return true;
      else if(CSTR_EQUAL == CompareStringW(LOCALE_SYSTEM_DEFAULT, flags, this-&gt;_str, static_cast&lt;int&gt;(value.strLen), value._str, static_cast&lt;int&gt;(value.strLen)))
          return true;
      else if(CSTR_EQUAL == CompareStringW(GetThreadLocale(), flags, this-&gt;_str, static_cast&lt;int&gt;(value.strLen), value._str, static_cast&lt;int&gt;(value.strLen)))
          return true;
      else
          return false;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>bool String::EndsWith( const String&amp; value, bool bCaseSensitive ) const
  {
      if(this-&gt;strLen == 0 || value.strLen == 0)
          return false;
  
      DWORD flags;
      if(bCaseSensitive == false)
          flags = LINGUISTIC_IGNORECASE;
      else
          flags = NORM_LINGUISTIC_CASING;
  
      size_t maxLen;
      if(this-&gt;strLen &lt; value.strLen)
          maxLen = this-&gt;strLen;
      else
          maxLen = value.strLen;
  
      LPCWSTR szStartOffset;
  
      if(maxLen == this-&gt;strLen)
          szStartOffset = this-&gt;_str;
      else
          szStartOffset = (this-&gt;_str + (this-&gt;strLen - value.strLen));
  
      if( CSTR_EQUAL == CompareStringW(LOCALE_USER_DEFAULT, flags, szStartOffset, static_cast&lt;int&gt;(maxLen), value._str, static_cast&lt;int&gt;(maxLen)) )
          return true;
      else if(CSTR_EQUAL == CompareStringW(LOCALE_SYSTEM_DEFAULT, flags, szStartOffset, static_cast&lt;int&gt;(maxLen), value._str, static_cast&lt;int&gt;(maxLen)))
          return true;
      else if(CSTR_EQUAL == CompareStringW(GetThreadLocale(), flags, szStartOffset, static_cast&lt;int&gt;(maxLen), value._str, static_cast&lt;int&gt;(maxLen)))
          return true;
      else
          return false;
  }
  </code></pre>
  
  <p>If somebody could help me I'd be very grateful.</p>