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

📄 string_access.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/string_access.hpp (formerly winstl_string_access.h)
 *
 * Purpose:     Contains classes and functions for dealing with Win32 strings.
 *
 * Created:     24th May 2002
 * Updated:     10th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2002-2006, Matthew Wilson and Synesis Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
 *   any contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ////////////////////////////////////////////////////////////////////// */


/// \file winstl/string_access.hpp
///
/// Contains classes and functions for dealing with Win32 strings.

#ifndef WINSTL_INCL_WINSTL_HPP_STRING_ACCESS
#define WINSTL_INCL_WINSTL_HPP_STRING_ACCESS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_HPP_STRING_ACCESS_MAJOR      3
# define WINSTL_VER_WINSTL_HPP_STRING_ACCESS_MINOR      3
# define WINSTL_VER_WINSTL_HPP_STRING_ACCESS_REVISION   3
# define WINSTL_VER_WINSTL_HPP_STRING_ACCESS_EDIT       97
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */
#ifndef WINSTL_INCL_WINSTL_H_WINDOW_FUNCTIONS
# include <winstl/window_functions.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINDOW_FUNCTIONS */
#ifndef WINSTL_INCL_WINSTL_HPP_WINDOWS_IDENT
# include <winstl/windows_ident.hpp>
#endif /* !WINSTL_INCL_WINSTL_HPP_WINDOWS_IDENT */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
# include <stlsoft/string/cstring_maker.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Functions
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

inline ws_size_t GetWindowTextLength__(HWND hwnd)
{
    WindowIdent ident   =   GetWindowIdent(hwnd);
    int         sel;

    switch(ident)
    {
        case    ListBox:
            if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
            {
                sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));

                if(LB_ERR != sel)
                {
                    return static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXTLEN, static_cast<WPARAM>(sel), 0L));
                }
                else
                {
                    return 0;
                }
            }
            break;
#if 0
        case    ListBox:
            if(1 == SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0L))
            {
                sel =
            }
            break;
#endif /* 0 */
        case    Generic:
        case    ComboBox:
        case    ListView:
        default:
            break;
    }

    return static_cast<ws_size_t>(::GetWindowTextLength(hwnd));
}

inline ws_size_t GetWindowTextA__(HWND hwnd, ws_char_a_t *buffer, ws_size_t cchBuffer)
{
    WindowIdent ident   =   GetWindowIdent(hwnd);
    int         sel;
    ws_size_t      cch;

    switch(ident)
    {
        case    ListBox:
            if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
            {
                sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));

                if(LB_ERR != sel)
                {
                    cch =   static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXT, static_cast<WPARAM>(sel), reinterpret_cast<LPARAM>(buffer)));

                    // Some programs using list-boxes do not null-terminate - Visual
                    // SourceSafe Explorer, anyone? - so we must do so here.
                    buffer[cch] = '\0';
                }
                else
                {
                    buffer[0] = '\0';

                    cch = 0;
                }

                WINSTL_MESSAGE_ASSERT("Buffer overwrite", !(cchBuffer < cch));

                return cch;
            }
            break;
        case    Generic:
        case    ComboBox:
        case    ListView:
        default:
            break;
    }

    return static_cast<ws_size_t>(::GetWindowTextA(hwnd, buffer, static_cast<int>(cchBuffer)));
}

inline ws_size_t GetWindowTextW__(HWND hwnd, ws_char_w_t *buffer, ws_size_t cchBuffer)
{
    WindowIdent ident   =   GetWindowIdent(hwnd);
    int         sel;

    switch(ident)
    {
        case    ListBox:
            if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
            {
                ws_size_t  cch;

                sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));

                if(LB_ERR != sel)
                {
                    cch =   static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXT, static_cast<WPARAM>(sel), reinterpret_cast<LPARAM>(buffer)));
                }
                else
                {
                    buffer[0] = '\0';

                    cch = 0;
                }

                WINSTL_MESSAGE_ASSERT("Buffer overwrite", !(cchBuffer < cch));

                return cch;
            }
            break;
        case    Generic:
        case    ComboBox:
        case    ListView:
        default:
            break;
    }

    return static_cast<ws_size_t>(::GetWindowTextW(hwnd, buffer, static_cast<int>(cchBuffer)));
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */


/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

/* HWND */
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the window text of a given window may be
/// accessed as a null-terminated string.
template <ss_typename_param_k C>
class c_str_ptr_null_HWND_proxy
{
    typedef cstring_maker<C>                            string_maker_type;
public:
    typedef C                                           char_type;
    typedef c_str_ptr_null_HWND_proxy<C>                class_type;

// Construction
public:
    /// Constructs an instance of the proxy from the given HWND
    ///
    /// \param h The HWND from which the text will be retrieved
    ss_explicit_k c_str_ptr_null_HWND_proxy(HWND h)
    {
        ws_size_t length  =   GetWindowTextLength__(h);

        if(length == 0)
        {
            m_buffer = NULL;
        }
        else
        {
            m_buffer = string_maker_type::alloc(length);

            if(NULL != m_buffer)
            {
                get_window_text(h, m_buffer, length + 1);
            }
        }
    }

#ifdef STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT
    /// Move constructor
    ///
    /// This <a href = "http://synesis.com.au/resources/articles/cpp/movectors.pdf">move constructor</a>
    /// is for circumstances when the compiler does not, or cannot, apply the
    /// return value optimisation. It causes the contents of \c rhs to be
    /// transferred into the constructing instance. This is completely safe
    /// because the \c rhs instance will never be accessed in its own right, so
    /// does not need to maintain ownership of its contents.
    c_str_ptr_null_HWND_proxy(class_type &rhs)
        : m_buffer(rhs.m_buffer)
    {
        move_lhs_from_rhs(rhs).m_buffer = NULL;
    }
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
    // Copy constructor
    c_str_ptr_null_HWND_proxy(class_type const &rhs)
        : m_buffer(string_maker_type::dup_null(rhs.m_buffer))
    {}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */

    /// Releases any storage aquired by the proxy
    ~c_str_ptr_null_HWND_proxy() stlsoft_throw_0()
    {
        string_maker_type::free(m_buffer);
    }

// Accessors
public:
    /// Returns a null-terminated string representing the window contents, or
    /// the empty string "" if the window contains no text.
    operator char_type const *() const
    {
        return m_buffer;
    }

// Implementation
private:
    ws_size_t get_window_text(HWND h, char_type *buffer, ws_size_t cchBuffer);

// Members
private:
    char_type   *m_buffer;

// Not to be implemented
private:
    void operator =(class_type const &rhs);
};

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

STLSOFT_TEMPLATE_SPECIALISATION
inline ws_size_t c_str_ptr_null_HWND_proxy<ws_char_a_t>::get_window_text(HWND h, ws_char_a_t *buffer, ws_size_t cchBuffer)
{
    return GetWindowTextA__(h, buffer, cchBuffer);
}

STLSOFT_TEMPLATE_SPECIALISATION
inline ws_size_t c_str_ptr_null_HWND_proxy<ws_char_w_t>::get_window_text(HWND h, ws_char_w_t *buffer, ws_size_t cchBuffer)
{
    return GetWindowTextW__(h, buffer, cchBuffer);
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/// This class provides an intermediary object that may be returned by the
/// c_str_ptr() function, such that the window text of a given window may be
/// accessed as a null-terminated string.
template <ss_typename_param_k C>
class c_str_ptr_HWND_proxy
{
    typedef cstring_maker<C>                            string_maker_type;
public:
    typedef C                                           char_type;
    typedef c_str_ptr_HWND_proxy<C>                     class_type;

// Construction
public:
    /// Constructs an instance of the proxy from the given HWND
    ///
    /// \param h The HWND from which the text will be retrieved
    ss_explicit_k c_str_ptr_HWND_proxy(HWND h)
    {
        ws_size_t length  =   GetWindowTextLength__(h);

        m_buffer = string_maker_type::alloc(length);

        if(NULL != m_buffer)
        {
            get_window_text(h, m_buffer, length + 1);
        }
    }

#ifdef STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT
    /// Move constructor
    ///
    /// This <a href = "http://synesis.com.au/resources/articles/cpp/movectors.pdf">move constructor</a>
    /// is for circumstances when the compiler does not, or cannot, apply the
    /// return value optimisation. It causes the contents of \c rhs to be
    /// transferred into the constructing instance. This is completely safe
    /// because the \c rhs instance will never be accessed in its own right, so
    /// does not need to maintain ownership of its contents.
    c_str_ptr_HWND_proxy(class_type &rhs)
        : m_buffer(rhs.m_buffer)
    {
        move_lhs_from_rhs(rhs).m_buffer = NULL;
    }
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
    // Copy constructor
    c_str_ptr_HWND_proxy(class_type const &rhs)
        : m_buffer(string_maker_type::dup_null(rhs.m_buffer))
    {}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */

    /// Releases any storage aquired by the proxy
    ~c_str_ptr_HWND_proxy() stlsoft_throw_0()
    {
        string_maker_type::free(m_buffer);
    }

// Accessors
public:
    /// Returns a null-terminated string representing the window contents, or
    /// the empty string "" if the window contains no text.
    operator char_type const *() const
    {
        static char_type    s_ch[1] = { '\0' };

        return (NULL == m_buffer) ? s_ch : m_buffer;
    }

// Implementation
private:
    ws_size_t get_window_text(HWND h, char_type *buffer, ws_size_t cchBuffer);

// Members
private:
    char_type   *m_buffer;

// Not to be implemented
private:
    void operator =(class_type const &rhs);
};

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

STLSOFT_TEMPLATE_SPECIALISATION
inline ws_size_t c_str_ptr_HWND_proxy<ws_char_a_t>::get_window_text(HWND h, ws_char_a_t *buffer, ws_size_t cchBuffer)
{
    return GetWindowTextA__(h, buffer, cchBuffer);
}

STLSOFT_TEMPLATE_SPECIALISATION
inline ws_size_t c_str_ptr_HWND_proxy<ws_char_w_t>::get_window_text(HWND h, ws_char_w_t *buffer, ws_size_t cchBuffer)
{
    return GetWindowTextW__(h, buffer, cchBuffer);
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */


/* Windows Policy Management LSA_UNICODE_STRING string */
#ifdef _NTSECAPI_
/// This class provides an intermediary object that may be returned by the
/// c_str_ptr_null() function, such that the text of a given LSA_UNICODE_STRING
/// string may be accessed as a null-terminated string.
class c_str_ptr_LSA_UNICODE_STRING_proxy

⌨️ 快捷键说明

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