⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilities.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "utilities.h"
#include "Cred.h"
#include "mlang.h"
#include "auto_xxx.hxx"
#include "Settings.h"
#include <msxml2.h>

/*------------------------------------------------------------------------------
    EncodeString
    
    Encode non A-Za-z0-9 chars into http unicode chars.
    
    Parameters:
       wszToEncode        : String containing a combination of alpha/nums and need-to-be encoded chars
       pbstrEncodedResult : [OUT] buffer that will contain the result
       fIsURL             : TRUE if the encoding is for a URL, FALSE if its for the request body

    Returns: HRESULT indicating whether the string was completely encoded
------------------------------------------------------------------------------*/
HRESULT EncodeString(
    const WCHAR *wszToEncode, 
    BSTR        *pbstrEncodedResult,
    BOOL        fIsURL /*= TRUE*/
    )
{
    //trusted function
    PREFAST_ASSERT(wszToEncode && pbstrEncodedResult);
    if(wszToEncode == NULL || pbstrEncodedResult == NULL)
    {
        return E_INVALIDARG;
    }
    
    //variable to walk the string
    WCHAR       *pwchEncode     = (WCHAR*)wszToEncode;    
    CHAR        szEncodeChar[10] = ""; //a UTF-8 representation of a single character (narrow character buffer)
    ce::wstring wstrBuffer;
    WCHAR       wszFormatEncodedChar[10];    
         
    /*
        When transmitting data to the exchange server using XMLHttp the encoding used is UTF-8,
        meaning that each character must be converted into one or more UTF-8 bytes.

        0-9, a-z, and A-Z can be left as is (they are already encoded as utf-8) 
        - all other characters must be converted to utf-8 bytes and escaped using the percentage sign.

        For example: the string "abcd$" should be converted to "abcd%24" and the character
        0xFF76 (the Halfwidth Katakana Letter 'Ka') should be converted as %ef%bd%b6
    */
    while (*pwchEncode != L'\0')
    {
        //get the next character
        WCHAR wc = *pwchEncode; 
        
        //if its an english alpha or digit, keep it - otherwise encode it 
        if ((wc >= L'0' && wc <= L'9') || (wc >= L'a' && wc <= L'z') || (wc >= L'A' && wc <= L'Z'))
        {
            if(!wstrBuffer.append(wc))
            {
                return E_OUTOFMEMORY;
            }
        }
        else
        {
            //clear out the multi-byte encoding buffer
            ZeroMemory(szEncodeChar, sizeof(szEncodeChar));
            if (WideCharToMultiByte(
                CP_UTF8,        //code page
                0,              //flags
                pwchEncode,     //string to encode
                1,              //number of characters to encode
                szEncodeChar,   //out buffer
                sizeof(szEncodeChar),
                NULL, 
                NULL
                ) == 0)
            {
                ASSERT(FALSE);
                return E_FAIL;
            }
            NULL_TERMINATE_ARRAY(szEncodeChar);
            
            CHAR *pchMultiByte = (CHAR*)szEncodeChar;
            //escape each returned character with a percentage sign
            while (*pchMultiByte)
            {
                //convert the char to an unsigned char, so it is not sign extended when passed into sprintf
                unsigned char encodedChar = (unsigned char)*pchMultiByte;                
                StringCchPrintf(
                    wszFormatEncodedChar, 
                    ARRAYSIZE(wszFormatEncodedChar),
                    (fIsURL) ? L"%%%02x" : L"%02x", 
                    encodedChar
                    );
                if(!wstrBuffer.append(wszFormatEncodedChar))
                {
                    return E_OUTOFMEMORY;
                }
        
                pchMultiByte++;
            }
        }

        pwchEncode++;
    }

    *pbstrEncodedResult = SysAllocString (wstrBuffer);
    if (pbstrEncodedResult == NULL)
    {
        ASSERT(FALSE);
        return E_OUTOFMEMORY;
    }

    //return true if we encoded all the characters
    ASSERT(!*pwchEncode);
    return S_OK;
}

/*------------------------------------------------------------------------------
    AddLocaleHeader
    
    Adds a 'Accept-Language=<RFC 1766 Identifier> to the request
------------------------------------------------------------------------------*/
HRESULT AddLocaleHeader(
    IXMLHTTPRequest * piRequest
    )
{
    if (piRequest == NULL)
    {
        ASSERT(FALSE);
        return E_POINTER;
    }

    LCID    lcidUser       = GetUserDefaultLCID();
    WCHAR   wszRfc1766[25] = L"";
    HRESULT hr             = S_OK;
    ce::auto_bstr bstrName, bstrValue;

    hr = LcidToRfc1766(
        lcidUser, 
        wszRfc1766, 
        ARRAYSIZE(wszRfc1766)
        );
    if (FAILED(hr))
    {
        StringCchCopy(
            wszRfc1766,
            _countof(wszRfc1766),
            c_SettingLocaleDefaultValue
            );
        hr = S_FALSE;
    }

    bstrName  = SysAllocString(c_SettingLocaleName);
    bstrValue = SysAllocString(wszRfc1766);

    if (! bstrName || !bstrValue)
    {
        return E_OUTOFMEMORY;
    }

    return piRequest->setRequestHeader(bstrName, bstrValue);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -