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

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * intshcut.cpp - IUnknown implementation for InternetShortcut class.
  3.  */
  4. /* Headers
  5.  **********/
  6. #include "project.hpp"
  7. #pragma hdrstop
  8. #include "assoc.h"
  9. #include "clsfact.h"
  10. /* Global Constants
  11.  *******************/
  12. #pragma data_seg(DATA_SEG_READ_ONLY)
  13. PUBLIC_DATA const int g_nDefaultShowCmd  = SW_NORMAL;
  14. #pragma data_seg()
  15. /****************************** Public Functions *****************************/
  16. PUBLIC_CODE HRESULT IsProtocolRegistered(PCSTR pcszProtocol)
  17. {
  18.    HRESULT hr;
  19.    PSTR pszKey;
  20.    ASSERT(IS_VALID_STRING_PTR(pcszProtocol, CSTR));
  21.    hr = GetProtocolKey(pcszProtocol, EMPTY_STRING, &pszKey);
  22.    if (hr == S_OK)
  23.    {
  24.       hr = (GetRegKeyValue(g_hkeyURLProtocols, pszKey, g_cszURLProtocol, NULL,
  25.                            NULL, NULL) == ERROR_SUCCESS)
  26.            ? S_OK
  27.            : URL_E_UNREGISTERED_PROTOCOL;
  28.       delete pszKey;
  29.       pszKey = NULL;
  30.    }
  31.    if (hr != S_OK) {
  32.       TRACE_OUT(("IsProtocolRegistered(): Protocol "%s" is not registered.",
  33.                  pcszProtocol));
  34.    }
  35.    return(hr);
  36. }
  37. PUBLIC_CODE HRESULT ValidateURL(PCSTR pcszURL)
  38. {
  39.    HRESULT hr;
  40.    PSTR pszProtocol;
  41.    ASSERT(IS_VALID_STRING_PTR(pcszURL, CSTR));
  42.    hr = CopyURLProtocol(pcszURL, &pszProtocol);
  43.    if (hr == S_OK)
  44.    {
  45.       hr = IsProtocolRegistered(pszProtocol);
  46.       delete pszProtocol;
  47.       pszProtocol = NULL;
  48.    }
  49.    return(hr);
  50. }
  51. PUBLIC_CODE HRESULT ValidateWorkingDirectory(PCSTR pcszWorkingDirectory)
  52. {
  53.    ASSERT(IS_VALID_STRING_PTR(pcszWorkingDirectory, CSTR));
  54.    return(IsPathDirectory(pcszWorkingDirectory) ? S_OK : E_PATH_NOT_FOUND);
  55. }
  56. #ifdef DEBUG
  57. PUBLIC_CODE BOOL IsValidPCInternetShortcut(PCInternetShortcut pcintshcut)
  58. {
  59.    return(IS_VALID_READ_PTR(pcintshcut, CInternetShortcut) &&
  60.           FLAGS_ARE_VALID(pcintshcut->m_dwFlags, ALL_INTSHCUT_FLAGS) &&
  61.           (! pcintshcut->m_pszFile ||
  62.            IS_VALID_STRING_PTR(pcintshcut->m_pszFile, STR)) &&
  63.           (! pcintshcut->m_pszURL ||
  64.            IS_VALID_STRING_PTR(pcintshcut->m_pszURL, STR)) &&
  65.           ((! pcintshcut->m_pszIconFile &&
  66.             ! pcintshcut->m_niIcon) ||
  67.            EVAL(IsValidIconIndex(S_OK, pcintshcut->m_pszIconFile, MAX_PATH_LEN, pcintshcut->m_niIcon))) &&
  68.           (! pcintshcut->m_pszWorkingDirectory ||
  69.            EVAL(IsFullPath(pcintshcut->m_pszWorkingDirectory))) &&
  70.           EVAL(IsValidShowCmd(pcintshcut->m_nShowCmd)) &&
  71.           EVAL(! pcintshcut->m_pszFolder ||
  72.                IsValidPath(pcintshcut->m_pszFolder)) &&
  73.           EVAL(! pcintshcut->m_wHotkey ||
  74.                IsValidHotkey(pcintshcut->m_wHotkey)) &&
  75.           IS_VALID_STRUCT_PTR((PCRefCount)pcintshcut, CRefCount) &&
  76.           IS_VALID_INTERFACE_PTR((PCIDataObject)pcintshcut, IDataObject) &&
  77.           IS_VALID_INTERFACE_PTR((PCIExtractIcon)pcintshcut, IExtractIcon) &&
  78.           IS_VALID_INTERFACE_PTR((PCINewShortcutHook)pcintshcut, INewShortcutHook) &&
  79.           IS_VALID_INTERFACE_PTR((PCIPersistFile)pcintshcut, IPersistFile) &&
  80.           IS_VALID_INTERFACE_PTR((PCIPersistStream)pcintshcut, IPersistStream) &&
  81.           IS_VALID_INTERFACE_PTR((PCIShellExecuteHook)pcintshcut, IShellExecuteHook) &&
  82.           IS_VALID_INTERFACE_PTR((PCIShellExtInit)pcintshcut, IShellExtInit) &&
  83.           IS_VALID_INTERFACE_PTR((PCIShellLink)pcintshcut, IShellLink) &&
  84.           IS_VALID_INTERFACE_PTR((PCIShellPropSheetExt)pcintshcut, IShellPropSheetExt) &&
  85.           IS_VALID_INTERFACE_PTR((PCIUniformResourceLocator)pcintshcut, IUniformResourceLocator));
  86. }
  87. #endif
  88. /********************************** Methods **********************************/
  89. #pragma warning(disable:4705) /* "statement has no effect" warning - cl bug, see KB Q98989 */
  90. InternetShortcut::InternetShortcut()
  91. {
  92.    DebugEntry(InternetShortcut::InternetShortcut);
  93.    // Don't validate this until after construction.
  94.    m_dwFlags = 0;
  95.    m_pszFile = NULL;
  96.    m_pszURL = NULL;
  97.    m_pszIconFile = NULL;
  98.    m_niIcon = 0;
  99.    m_pszWorkingDirectory = NULL;
  100.    m_nShowCmd = g_nDefaultShowCmd;
  101.    m_pszFolder = NULL;
  102.    m_wHotkey = 0;
  103.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  104.    DebugExitVOID(InternetShortcut::InternetShortcut);
  105.    return;
  106. }
  107. #pragma warning(default:4705) /* "statement has no effect" warning - cl bug, see KB Q98989 */
  108. InternetShortcut::~InternetShortcut(void)
  109. {
  110.    DebugEntry(InternetShortcut::~InternetShortcut);
  111.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  112.    if (m_pszFile)
  113.    {
  114.       delete m_pszFile;
  115.       m_pszFile = NULL;
  116.    }
  117.    if (m_pszURL)
  118.    {
  119.       delete m_pszURL;
  120.       m_pszURL = NULL;
  121.    }
  122.    if (m_pszIconFile)
  123.    {
  124.       delete m_pszIconFile;
  125.       m_pszIconFile = NULL;
  126.       m_niIcon = 0;
  127.    }
  128.    if (m_pszWorkingDirectory)
  129.    {
  130.       delete m_pszWorkingDirectory;
  131.       m_pszWorkingDirectory = NULL;
  132.    }
  133.    if (m_pszFolder)
  134.    {
  135.       delete m_pszFolder;
  136.       m_pszFolder = NULL;
  137.    }
  138.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  139.    DebugExitVOID(InternetShortcut::~InternetShortcut);
  140.    return;
  141. }
  142. ULONG STDMETHODCALLTYPE InternetShortcut::AddRef(void)
  143. {
  144.    ULONG ulcRef;
  145.    DebugEntry(InternetShortcut::AddRef);
  146.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  147.    ulcRef = RefCount::AddRef();
  148.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  149.    DebugExitULONG(InternetShortcut::AddRef, ulcRef);
  150.    return(ulcRef);
  151. }
  152. ULONG STDMETHODCALLTYPE InternetShortcut::Release(void)
  153. {
  154.    ULONG ulcRef;
  155.    DebugEntry(InternetShortcut::Release);
  156.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  157.    ulcRef = RefCount::Release();
  158.    DebugExitULONG(InternetShortcut::Release, ulcRef);
  159.    return(ulcRef);
  160. }
  161. HRESULT STDMETHODCALLTYPE InternetShortcut::QueryInterface(REFIID riid,
  162.                                                            PVOID *ppvObject)
  163. {
  164.    HRESULT hr = S_OK;
  165.    DebugEntry(InternetShortcut::QueryInterface);
  166.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  167.    ASSERT(IsValidREFIID(riid));
  168.    ASSERT(IS_VALID_WRITE_PTR(ppvObject, PVOID));
  169.    if (riid == IID_IDataObject)
  170.    {
  171.       *ppvObject = (PIDataObject)this;
  172.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IDataObject."));
  173.    }
  174.    else if (riid == IID_IExtractIcon)
  175.    {
  176.       *ppvObject = (PIExtractIcon)this;
  177.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IExtractIcon."));
  178.    }
  179.    else if (riid == IID_INewShortcutHook)
  180.    {
  181.       *ppvObject = (PINewShortcutHook)this;
  182.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning INewShortcutHook."));
  183.    }
  184.    else if (riid == IID_IPersist)
  185.    {
  186.       *ppvObject = (PIPersist)(PIPersistStream)this;
  187.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IPersist."));
  188.    }
  189.    else if (riid == IID_IPersistFile)
  190.    {
  191.       *ppvObject = (PIPersistFile)this;
  192.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IPersistFile."));
  193.    }
  194.    else if (riid == IID_IPersistStream)
  195.    {
  196.       *ppvObject = (PIPersistStream)this;
  197.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IPersistStream."));
  198.    }
  199.    else if (riid == IID_IShellExecuteHook)
  200.    {
  201.       *ppvObject = (PIShellExecuteHook)this;
  202.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IShellExecuteHook."));
  203.    }
  204.    else if (riid == IID_IShellExtInit)
  205.    {
  206.       *ppvObject = (PIShellExtInit)this;
  207.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IShellExtInit."));
  208.    }
  209.    else if (riid == IID_IShellLink)
  210.    {
  211.       *ppvObject = (PIShellLink)this;
  212.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IShellLink."));
  213.    }
  214.    else if (riid == IID_IShellPropSheetExt)
  215.    {
  216.       *ppvObject = (PIShellPropSheetExt)this;
  217.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IShellPropSheetExt."));
  218.    }
  219.    else if (riid == IID_IUniformResourceLocator)
  220.    {
  221.       *ppvObject = (PIUniformResourceLocator)this;
  222.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IUniformResourceLocator."));
  223.    }
  224.    else if (riid == IID_IUnknown)
  225.    {
  226.       *ppvObject = (PIUnknown)(PIUniformResourceLocator)this;
  227.       TRACE_OUT(("InternetShortcut::QueryInterface(): Returning IUnknown."));
  228.    }
  229.    else
  230.    {
  231.       TRACE_OUT(("InternetShortcut::QueryInterface(): Called on unknown interface."));
  232.       *ppvObject = NULL;
  233.       hr = E_NOINTERFACE;
  234.    }
  235.    if (hr == S_OK)
  236.       AddRef();
  237.    ASSERT(IS_VALID_STRUCT_PTR(this, CInternetShortcut));
  238.    DebugExitHRESULT(InternetShortcut::QueryInterface, hr);
  239.    return(hr);
  240. }
  241. extern "C"
  242. STDAPI CreateInstance_Intshcut(IUnknown *punkOuter, REFIID riid, void **ppvOut)
  243. {
  244.     HRESULT hres;
  245.     *ppvOut = NULL;
  246.     if (punkOuter)
  247.         return CLASS_E_NOAGGREGATION;
  248.     InternetShortcut *pintshcut = new(InternetShortcut);
  249.     if (pintshcut) 
  250.     {
  251.         hres = pintshcut->QueryInterface(riid, ppvOut);
  252.         pintshcut->Release();
  253.     }
  254.     else
  255.         hres = E_OUTOFMEMORY;
  256.     return hres;
  257. }