Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.7 KiB

  1. // this file implements shell command files.
  2. // .scf scffile
  3. // when executed, they run a shell internal command.
  4. // they can have stream storage, or whatnot in them
  5. //
  6. // file format is *PURPOSELY* text so that folks can create and modify by hand
  7. #include "shellprv.h"
  8. #include <desktopp.h>
  9. #include <trayp.h>
  10. #include <strsafe.h>
  11. extern HWND g_hwndTray; // desktop.cpp
  12. void SFC_IECommand(LPCTSTR pszFile)
  13. {
  14. TCHAR szCommand[40];
  15. if (GetPrivateProfileString(TEXT("IE"), TEXT("Command"), TEXT(""), szCommand, ARRAYSIZE(szCommand), pszFile))
  16. {
  17. if (!lstrcmpi(szCommand, TEXT("Channels"))
  18. && !SHRestricted2W(REST_NoChannelUI, NULL, 0))
  19. {
  20. Channel_QuickLaunch();
  21. }
  22. }
  23. }
  24. void SFC_TrayCommand(LPCTSTR pszFile)
  25. {
  26. HWND hwnd = g_hwndTray;
  27. if (hwnd && IsWindowInProcess(hwnd))
  28. {
  29. TCHAR szCommand[40];
  30. if (GetPrivateProfileString(TEXT("Taskbar"), TEXT("Command"), TEXT(""), szCommand, ARRAYSIZE(szCommand), pszFile))
  31. {
  32. char szAnsiCommand[40];
  33. SHTCharToAnsi(szCommand, szAnsiCommand, ARRAYSIZE(szAnsiCommand));
  34. LPSTR psz = StrDupA(szAnsiCommand);
  35. if (psz)
  36. {
  37. if (!PostMessage(hwnd, TM_PRIVATECOMMAND, 0, (LPARAM)psz))
  38. LocalFree(psz);
  39. }
  40. }
  41. }
  42. }
  43. const struct
  44. {
  45. UINT id;
  46. void (*pfn)(LPCTSTR pszBuf);
  47. }
  48. c_sCmdInfo[] =
  49. {
  50. { 2, SFC_TrayCommand},
  51. { 3, SFC_IECommand},
  52. };
  53. STDAPI_(void) ShellExecCommandFile(LPCITEMIDLIST pidl)
  54. {
  55. TCHAR szFile[MAX_PATH];
  56. if (SHGetPathFromIDList(pidl, szFile))
  57. {
  58. UINT uID = GetPrivateProfileInt(TEXT("Shell"), TEXT("Command"), 0, szFile);
  59. if (uID)
  60. {
  61. for (int i = 0; i < ARRAYSIZE(c_sCmdInfo); i++)
  62. {
  63. if (uID == c_sCmdInfo[i].id)
  64. {
  65. c_sCmdInfo[i].pfn(szFile);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. class CShellCmdFileIcon : public IExtractIconA, public IExtractIconW, public IPersistFile
  73. {
  74. public:
  75. // IUnknown
  76. STDMETHODIMP QueryInterface(REFIID riid, void** ppv);
  77. STDMETHODIMP_(ULONG) AddRef();
  78. STDMETHODIMP_(ULONG) Release();
  79. // IExtractIconA
  80. STDMETHODIMP GetIconLocation(UINT uFlags, LPSTR pszIconFile, UINT cchMax, int* piIndex, UINT* pwFlags);
  81. STDMETHODIMP Extract(LPCSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize) {return S_FALSE;};
  82. // IExtractIconW
  83. STDMETHODIMP GetIconLocation(UINT uFlags, LPWSTR pszIconFile, UINT cchMax, int* piIndex, UINT* pwFlags);
  84. STDMETHODIMP Extract(LPCWSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize) {return S_FALSE;};
  85. // IPersist
  86. STDMETHODIMP GetClassID(CLSID *pclsid) { *pclsid = CLSID_CmdFileIcon; return S_OK;};
  87. // IPersistFile
  88. STDMETHODIMP IsDirty(void) {return S_FALSE;};
  89. STDMETHODIMP Save(LPCOLESTR pcwszFileName, BOOL bRemember) {return S_OK;};
  90. STDMETHODIMP SaveCompleted(LPCOLESTR pcwszFileName){return S_OK;};
  91. STDMETHODIMP Load(LPCOLESTR pcwszFileName, DWORD dwMode);
  92. STDMETHODIMP GetCurFile(LPOLESTR *ppwszFileName);
  93. CShellCmdFileIcon() { _cRef = 1; DllAddRef(); };
  94. private:
  95. ~CShellCmdFileIcon() { DllRelease(); };
  96. LONG _cRef;
  97. TCHAR _szFile[MAX_PATH];
  98. };
  99. ULONG CShellCmdFileIcon::AddRef()
  100. {
  101. return InterlockedIncrement(&_cRef);
  102. }
  103. ULONG CShellCmdFileIcon::Release()
  104. {
  105. ASSERT( 0 != _cRef );
  106. ULONG cRef = InterlockedDecrement(&_cRef);
  107. if ( 0 == cRef )
  108. {
  109. delete this;
  110. }
  111. return cRef;
  112. }
  113. HRESULT CShellCmdFileIcon::GetIconLocation(UINT uFlags, LPTSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
  114. {
  115. TCHAR szData[MAX_PATH + 80];
  116. if (_szFile[0])
  117. {
  118. *pwFlags = 0;
  119. *piIndex = 0;
  120. szIconFile[0] = 0;
  121. GetPrivateProfileString(TEXT("Shell"), TEXT("IconFile"), TEXT(""), szData, ARRAYSIZE(szData), _szFile);
  122. *piIndex = PathParseIconLocation(szData);
  123. HRESULT hr = StringCchCopy(szIconFile, cchMax, szData);
  124. return hr;
  125. }
  126. return E_FAIL;
  127. }
  128. HRESULT CShellCmdFileIcon::GetIconLocation(UINT uFlags, LPSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
  129. {
  130. WCHAR szAnsiIconPath[MAX_PATH];
  131. HRESULT hr = GetIconLocation(uFlags, szAnsiIconPath, MAX_PATH, piIndex, pwFlags);
  132. if (SUCCEEDED(hr))
  133. {
  134. SHUnicodeToAnsi(szAnsiIconPath, szIconFile, cchMax);
  135. }
  136. return hr;
  137. }
  138. // IPersistFile::Load
  139. STDMETHODIMP CShellCmdFileIcon::Load(LPCOLESTR pwszFile, DWORD dwMode)
  140. {
  141. SHUnicodeToTChar(pwszFile, _szFile, ARRAYSIZE(_szFile));
  142. return S_OK;
  143. }
  144. STDMETHODIMP CShellCmdFileIcon::GetCurFile(LPOLESTR *ppwszFileName)
  145. {
  146. SHTCharToUnicode(_szFile, *ppwszFileName, ARRAYSIZE(_szFile));
  147. return S_OK;
  148. }
  149. HRESULT CShellCmdFileIcon::QueryInterface(REFIID riid, void **ppv)
  150. {
  151. static const QITAB qit[] =
  152. {
  153. QITABENT(CShellCmdFileIcon, IExtractIconA),
  154. QITABENT(CShellCmdFileIcon, IExtractIconW),
  155. QITABENT(CShellCmdFileIcon, IPersistFile),
  156. QITABENTMULTI(CShellCmdFileIcon, IPersist, IPersistFile),
  157. { 0 },
  158. };
  159. return QISearch(this, qit, riid, ppv);
  160. }
  161. STDAPI CShellCmdFileIcon_CreateInstance(IUnknown* pUnkOuter, REFIID riid, void **ppv)
  162. {
  163. HRESULT hr;
  164. CShellCmdFileIcon *pObj = new CShellCmdFileIcon();
  165. if (pObj)
  166. {
  167. hr = pObj->QueryInterface(riid, ppv);
  168. pObj->Release();
  169. }
  170. else
  171. {
  172. *ppv = NULL;
  173. hr = E_OUTOFMEMORY;
  174. }
  175. return hr;
  176. }