strret.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //+-------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. //  File:       strret.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. ///////////////////////////////////////////////////////////////////////////////
  11. /*  File: strret.cpp
  12.     Description: Class to handle STRRET objects used in returning strings
  13.         from the Windows Shell.  Main function is to properly clean up any
  14.         dynamic storage.
  15.     Revision History:
  16.     Date        Description                                          Programmer
  17.     --------    ---------------------------------------------------  ----------
  18.     07/01/97    Initial creation.                                    BrianAu
  19. */
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #include "pch.h"
  22. #pragma hdrstop
  23. #include "strret.h"
  24. StrRet::StrRet(
  25.     LPITEMIDLIST pidl,
  26.     UINT type
  27.     ) : m_pMalloc(NULL)
  28. {
  29.     uType = type;
  30.     pOleStr = NULL;
  31.     SHGetMalloc(&m_pMalloc);
  32.     m_pidl = pidl;
  33. }
  34. StrRet::~StrRet(
  35.     VOID
  36.     )
  37. {
  38.     FreeOleStr();
  39.     if (NULL != m_pMalloc)
  40.     {
  41.         m_pMalloc->Release();
  42.     }
  43. }
  44. StrRet::StrRet(const StrRet& rhs)
  45. {
  46.     *this = rhs;
  47. }
  48.     
  49. StrRet& StrRet::operator = (const StrRet& rhs)
  50. {
  51.     if (this != &rhs)
  52.     {
  53.         if (NULL != m_pMalloc)
  54.             m_pMalloc->Release();
  55.         m_pMalloc = rhs.m_pMalloc;
  56.         if (NULL != m_pMalloc)
  57.             m_pMalloc->AddRef();
  58.         
  59.         m_pidl = rhs.m_pidl;
  60.         switch(rhs.uType)
  61.         {
  62.             case STRRET_WSTR:
  63.                 CopyOleStr(rhs.pOleStr);
  64.                 break;
  65.             case STRRET_CSTR:
  66.                 lstrcpynA(cStr, rhs.cStr, ARRAYSIZE(cStr));
  67.                 break;
  68.             case STRRET_OFFSET:
  69.                 uOffset = rhs.uOffset;
  70.                 break;
  71.         }
  72.     }
  73.     return *this;
  74. }
  75. VOID
  76. StrRet::FreeOleStr(
  77.     VOID
  78.     )
  79. {
  80.     if (NULL != m_pMalloc &&
  81.         STRRET_WSTR == uType && 
  82.         NULL != pOleStr)
  83.     {
  84.         m_pMalloc->Free(pOleStr);
  85.         pOleStr = NULL;
  86.     }
  87. }
  88. VOID
  89. StrRet::CopyOleStr(
  90.     LPCWSTR pszOle
  91.     )
  92. {
  93.     FreeOleStr();
  94.     pOleStr = new WCHAR[lstrlenW(pszOle) + 1];
  95.     if (NULL != pOleStr)
  96.     {
  97.         lstrcpyW(pOleStr, pszOle);
  98.     }
  99. }
  100. LPTSTR 
  101. StrRet::GetString(
  102.     LPTSTR pszStr, 
  103.     INT cchStr
  104.     ) const
  105. {
  106.     switch(uType)
  107.     {
  108.         case STRRET_WSTR:
  109. #ifndef UNICODE
  110.             WideCharToMultiByte(CP_ACP, 0, pOleStr, -1, pszStr, cchStr, NULL, NULL);
  111. #else
  112.             lstrcpyn(pszStr, pOleStr, cchStr);
  113. #endif                                    
  114.             break;
  115.         case STRRET_CSTR:
  116. #ifndef UNICODE
  117.             lstrcpyn(pszStr, cStr, cchStr);
  118. #else
  119.             MultiByteToWideChar(CP_ACP, 0, cStr, -1, pszStr, cchStr);
  120. #endif
  121.             break;
  122.         case STRRET_OFFSET:
  123.             lstrcpyn(pszStr, (LPCTSTR)((LPBYTE)m_pidl + uOffset), cchStr);
  124.             break;
  125.     }
  126.     return pszStr;
  127. }