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.

472 lines
17 KiB

  1. #ifndef __SHLINK_H__
  2. #define __SHLINK_H__
  3. #include <trkwks.h>
  4. #include <rpcasync.h>
  5. #include <filter.h>
  6. #include "cowsite.h"
  7. class CDarwinContextMenuCB;
  8. class CTracker;
  9. class CShellLink : public IShellLinkA,
  10. public IShellLinkW,
  11. public IPersistStream,
  12. public IPersistFile,
  13. public IShellExtInit,
  14. public IContextMenu3,
  15. public IDropTarget,
  16. public IQueryInfo,
  17. public IShellLinkDataList,
  18. public IExtractIconA,
  19. public IExtractIconW,
  20. public IExtractImage2,
  21. public IPersistPropertyBag,
  22. public IServiceProvider,
  23. public IFilter,
  24. public CObjectWithSite,
  25. public ICustomizeInfoTip
  26. {
  27. friend CTracker;
  28. friend CDarwinContextMenuCB;
  29. public:
  30. CShellLink();
  31. // IUnknown
  32. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  33. STDMETHODIMP_(ULONG) AddRef();
  34. STDMETHODIMP_(ULONG) Release();
  35. // IShellLinkA methods
  36. STDMETHOD(GetPath)(LPSTR pszFile, int cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD flags);
  37. STDMETHOD(SetPath)(LPCSTR pszFile);
  38. //STDMETHOD(GetIDList)(LPITEMIDLIST *ppidl);
  39. //STDMETHOD(SetIDList)(LPCITEMIDLIST pidl);
  40. STDMETHOD(GetDescription)(LPSTR pszName, int cchMaxName);
  41. STDMETHOD(SetDescription)(LPCSTR pszName);
  42. STDMETHOD(GetWorkingDirectory)(LPSTR pszDir, int cchMaxPath);
  43. STDMETHOD(SetWorkingDirectory)(LPCSTR pszDir);
  44. STDMETHOD(GetArguments)(LPSTR pszArgs, int cchMaxPath);
  45. STDMETHOD(SetArguments)(LPCSTR pszArgs);
  46. //STDMETHOD(GetHotkey)(WORD *pwHotkey);
  47. //STDMETHOD(SetHotkey)(WORD wHotkey);
  48. //STDMETHOD(GetShowCmd)(int *piShowCmd);
  49. //STDMETHOD(SetShowCmd)(int iShowCmd);
  50. STDMETHOD(GetIconLocation)(LPSTR pszIconPath, int cchIconPath, int *piIcon);
  51. STDMETHOD(SetIconLocation)(LPCSTR pszIconPath, int iIcon);
  52. //STDMETHOD(Resolve)(HWND hwnd, DWORD dwResolveFlags);
  53. STDMETHOD(SetRelativePath)(LPCSTR pszPathRel, DWORD dwReserved);
  54. // IShellLinkW
  55. STDMETHOD(GetPath)(LPWSTR pszFile, int cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags);
  56. STDMETHOD(GetIDList)(LPITEMIDLIST *ppidl);
  57. STDMETHOD(SetIDList)(LPCITEMIDLIST pidl);
  58. STDMETHOD(GetDescription)(LPWSTR pszName, int cchMaxName);
  59. STDMETHOD(SetDescription)(LPCWSTR pszName);
  60. STDMETHOD(GetWorkingDirectory)(LPWSTR pszDir, int cchMaxPath);
  61. STDMETHOD(SetWorkingDirectory)(LPCWSTR pszDir);
  62. STDMETHOD(GetArguments)(LPWSTR pszArgs, int cchMaxPath);
  63. STDMETHOD(SetArguments)(LPCWSTR pszArgs);
  64. STDMETHOD(GetHotkey)(WORD *pwHotKey);
  65. STDMETHOD(SetHotkey)(WORD wHotkey);
  66. STDMETHOD(GetShowCmd)(int *piShowCmd);
  67. STDMETHOD(SetShowCmd)(int iShowCmd);
  68. STDMETHOD(GetIconLocation)(LPWSTR pszIconPath, int cchIconPath, int *piIcon);
  69. STDMETHOD(SetIconLocation)(LPCWSTR pszIconPath, int iIcon);
  70. STDMETHOD(SetRelativePath)(LPCWSTR pszPathRel, DWORD dwReserved);
  71. STDMETHOD(Resolve)(HWND hwnd, DWORD dwResolveFlags);
  72. STDMETHOD(SetPath)(LPCWSTR pszFile);
  73. // IPersist
  74. STDMETHOD(GetClassID)(CLSID *pClassID);
  75. STDMETHOD(IsDirty)();
  76. // IPersistStream
  77. STDMETHOD(Load)(IStream *pstm);
  78. STDMETHOD(Save)(IStream *pstm, BOOL fClearDirty);
  79. STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize);
  80. // IPersistFile
  81. STDMETHOD(Load)(LPCOLESTR pwszFile, DWORD grfMode);
  82. STDMETHOD(Save)(LPCOLESTR pwszFile, BOOL fRemember);
  83. STDMETHOD(SaveCompleted)(LPCOLESTR pwszFile);
  84. STDMETHOD(GetCurFile)(LPOLESTR *lplpszFileName);
  85. // IPersistPropertyBag
  86. STDMETHOD(Save)(IPropertyBag* pPropBag, BOOL fClearDirty, BOOL fSaveAllProperties);
  87. STDMETHOD(Load)(IPropertyBag* pPropBag, IErrorLog* pErrorLog);
  88. STDMETHOD(InitNew)(void);
  89. // IShellExtInit
  90. STDMETHOD(Initialize)(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID);
  91. // IContextMenu3
  92. STDMETHOD(QueryContextMenu)(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
  93. STDMETHOD(InvokeCommand)(LPCMINVOKECOMMANDINFO piciIn);
  94. STDMETHOD(GetCommandString)(UINT_PTR idCmd, UINT wFlags, UINT *pmf, LPSTR pszName, UINT cchMax);
  95. STDMETHOD(HandleMenuMsg)(UINT uMsg, WPARAM wParam, LPARAM lParam);
  96. STDMETHOD(HandleMenuMsg2)(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *lResult);
  97. // IDropTarget
  98. STDMETHOD(DragEnter)(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
  99. STDMETHOD(DragOver)(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
  100. STDMETHOD(DragLeave)();
  101. STDMETHOD(Drop)(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
  102. // IQueryInfo
  103. STDMETHOD(GetInfoTip)(DWORD dwFlags, WCHAR **ppwszTip);
  104. STDMETHOD(GetInfoFlags)(LPDWORD pdwFlags);
  105. // IShellLinkDataList
  106. STDMETHOD(AddDataBlock)(void *pdb);
  107. STDMETHOD(CopyDataBlock)(DWORD dwSig, void **ppdb);
  108. STDMETHOD(RemoveDataBlock)(DWORD dwSig);
  109. STDMETHOD(GetFlags)(LPDWORD pdwFlags);
  110. STDMETHOD(SetFlags)(DWORD dwFlags);
  111. // IExtractIconA
  112. STDMETHOD(GetIconLocation)(UINT uFlags,LPSTR szIconFile,UINT cchMax,int *piIndex,UINT * pwFlags);
  113. STDMETHOD(Extract)(LPCSTR pszFile,UINT nIconIndex,HICON *phiconLarge,HICON *phiconSmall,UINT nIcons);
  114. // IExtractIconW
  115. STDMETHOD(GetIconLocation)(UINT uFlags, LPWSTR pszIconFile, UINT cchMax, int *piIndex, UINT *pwFlags);
  116. STDMETHOD(Extract)(LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize);
  117. // IExtractImage
  118. STDMETHOD (GetLocation)(LPWSTR pszPathBuffer, DWORD cch, DWORD * pdwPriority, const SIZE * prgSize,
  119. DWORD dwRecClrDepth, DWORD *pdwFlags);
  120. STDMETHOD (Extract)(HBITMAP *phBmpThumbnail);
  121. // IExtractImage2
  122. STDMETHOD (GetDateStamp)(FILETIME *pftDateStamp);
  123. // IServiceProvider
  124. STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void **ppv);
  125. // IFilter
  126. STDMETHOD(Init)(ULONG grfFlags, ULONG cAttributes, const FULLPROPSPEC *aAttributes, ULONG *pFlags);
  127. STDMETHOD(GetChunk)(STAT_CHUNK *pStat);
  128. STDMETHOD(GetText)(ULONG *pcwcBuffer, WCHAR *awcBuffer);
  129. STDMETHOD(GetValue)(PROPVARIANT **ppPropValue);
  130. STDMETHOD(BindRegion)(FILTERREGION origPos, REFIID riid, void **ppunk);
  131. //*** IObjectWithSite ***
  132. //STDMETHOD(SetSite)(IUnknown *punkSite);
  133. //STDMETHOD(GetSite)(REFIID riid, void **ppvSite);
  134. // ICustomizeInfoTip
  135. STDMETHODIMP SetPrefixText(LPCWSTR pszPrefix);
  136. STDMETHODIMP SetExtraProperties(const SHCOLUMNID *pscid, UINT cscid);
  137. // public non interface members
  138. void _AddExtraDataSection(DATABLOCK_HEADER *pdbh);
  139. void _RemoveExtraDataSection(DWORD dwSig);
  140. protected:
  141. HRESULT _Resolve(HWND hwnd, DWORD dwResolveFlags, DWORD dwTracker);
  142. private:
  143. ~CShellLink();
  144. static DWORD CALLBACK _InvokeThreadProc(void *pv);
  145. static DWORD CALLBACK _VerifyPathThreadProc(void *pv);
  146. void _ResetPersistData();
  147. BOOL _GetRelativePath(LPTSTR pszPath);
  148. HRESULT _SetPIDLPath(LPCITEMIDLIST pidl, LPCTSTR pszPath, BOOL bUpdateTrackingData);
  149. HRESULT _SetSimplePIDL(LPCTSTR pszPath);
  150. void _UpdateWorkingDir(LPCTSTR pszPath);
  151. PLINKINFO _GetLinkInfo(LPCTSTR pszPath);
  152. void _FreeLinkInfo();
  153. HRESULT _GetFindDataAndTracker(LPCTSTR pszPath);
  154. void _ClearTrackerData();
  155. BOOL _SetFindData(const WIN32_FIND_DATA *pfd);
  156. void _GetFindData(WIN32_FIND_DATA *pfd);
  157. BOOL _IsEqualFindData(const WIN32_FIND_DATA *pfd);
  158. HRESULT _ResolveIDList(HWND hwnd, DWORD dwResolveFlags);
  159. HRESULT _ResolveLinkInfo(HWND hwnd, DWORD dwResolveFlags, LPTSTR pszPath, DWORD *pfifFlags);
  160. HRESULT _ResolveRemovable(HWND hwnd, LPCTSTR pszPath);
  161. BOOL _ShouldTryRemovable(HRESULT hr, LPCTSTR pszPath);
  162. void _SetIDListFromEnvVars();
  163. BOOL _ResolveDarwin(HWND hwnd, DWORD dwResolveFlags, HRESULT *phr);
  164. HRESULT _ResolveLogo3Link(HWND hwnd, DWORD dwResolveFlags);
  165. HRESULT _CheckForLinkBlessing(LPCTSTR *ppszPathIn);
  166. HRESULT BlessLink(LPCTSTR *ppszPath, DWORD dwSignature);
  167. BOOL _EncodeSpecialFolder();
  168. void _DecodeSpecialFolder();
  169. HRESULT _SetRelativePath(LPCTSTR pszRelSource);
  170. HRESULT _UpdateTracker();
  171. HRESULT _LoadFromFile(LPCTSTR pszPath);
  172. HRESULT _LoadFromPIF(LPCTSTR szPath);
  173. HRESULT _SaveToFile(LPTSTR pszPathSave, BOOL fRemember);
  174. HRESULT _SaveAsLink(LPCTSTR szPath);
  175. HRESULT _SaveAsPIF(LPCTSTR pszPath, BOOL fPath);
  176. BOOL _GetWorkingDir(LPTSTR pszDir);
  177. HRESULT _GetUIObject(HWND hwnd, REFIID riid, void **ppvOut);
  178. HRESULT _ShortNetTimeout();
  179. HRESULT _CreateDarwinContextMenu(HWND hwnd,IContextMenu **pcmOut);
  180. HRESULT _CreateDarwinContextMenuForPidl(HWND hwnd, LPCITEMIDLIST pidlTarget, IContextMenu **pcmOut);
  181. HRESULT _InvokeCommandAsync(LPCMINVOKECOMMANDINFO pici);
  182. HRESULT _InitDropTarget();
  183. HRESULT _GetExtractIcon(REFIID riid, void **ppvOut);
  184. HRESULT _InitExtractIcon();
  185. HRESULT _InitExtractImage();
  186. BOOL _GetExpandedPath(LPTSTR psz, DWORD cch);
  187. HRESULT _SetField(LPTSTR *ppszField, LPCWSTR pszValueW);
  188. HRESULT _SetField(LPTSTR *ppszField, LPCSTR pszValueA);
  189. HRESULT _GetField(LPCTSTR pszField, LPWSTR pszValueW, int cchValue);
  190. HRESULT _GetField(LPCTSTR pszField, LPSTR pszValueA, int cchValue);
  191. int _IsOldDarwin(LPCTSTR pszPath);
  192. HRESULT _SetPathOldDarwin(LPCTSTR pszPath);
  193. HRESULT _CreateProcessWithShimLayer(HANDLE hData, BOOL fAllowAsync);
  194. HRESULT _MaybeAddShim(IBindCtx **ppbcRelease);
  195. HRESULT _UpdateIconFromExpIconSz();
  196. //
  197. // Inner class to manage the context menu of the shortcut target.
  198. // We do this to ensure that the target's context menu gets a proper
  199. // SetSite call so it can contact the containing shortcut.
  200. //
  201. class TargetContextMenu {
  202. public:
  203. operator IUnknown*() { return _pcmTarget; }
  204. // WARNING! Use only as an output pointer
  205. IContextMenu **GetOutputPtr() { return &_pcmTarget; }
  206. HRESULT QueryContextMenu(IShellLink *outer, HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
  207. {
  208. IUnknown_SetSite(_pcmTarget, outer);
  209. HRESULT hr = _pcmTarget->QueryContextMenu(hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
  210. IUnknown_SetSite(_pcmTarget, NULL);
  211. return hr;
  212. }
  213. HRESULT InvokeCommand(IShellLink *outer, LPCMINVOKECOMMANDINFO pici)
  214. {
  215. IUnknown_SetSite(_pcmTarget, outer);
  216. HRESULT hr = _pcmTarget->InvokeCommand(pici);
  217. IUnknown_SetSite(_pcmTarget, NULL);
  218. return hr;
  219. }
  220. HRESULT HandleMenuMsg2(IShellLink *outer, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *plResult);
  221. // This doesn't need to be wrapped in a SetSite (yet)
  222. UINT GetCommandString(IShellLink *outer, UINT_PTR idCmd, UINT wFlags, UINT *pmf, LPSTR pszName, UINT cchMax)
  223. {
  224. HRESULT hr = _pcmTarget->GetCommandString(idCmd, wFlags, pmf, pszName, cchMax);
  225. return hr;
  226. }
  227. // This doesn't need to be wrapped in a SetSite (yet)
  228. UINT GetMenuIndexForCanonicalVerb(IShellLink *outer, HMENU hMenu, UINT idCmdFirst, LPCWSTR pwszVerb)
  229. {
  230. UINT ui = ::GetMenuIndexForCanonicalVerb(hMenu, _pcmTarget, idCmdFirst, pwszVerb);
  231. return ui;
  232. }
  233. void AtomicRelease()
  234. {
  235. ATOMICRELEASE(_pcmTarget);
  236. }
  237. ~TargetContextMenu()
  238. {
  239. IUnknown_SetSite(_pcmTarget, NULL);
  240. AtomicRelease();
  241. }
  242. private:
  243. IContextMenu *_pcmTarget; // stuff for IContextMenu
  244. };
  245. // Data Members
  246. LONG _cRef; // Ref Count
  247. BOOL _bDirty; // something has changed
  248. LPTSTR _pszCurFile; // current file from IPersistFile
  249. LPTSTR _pszRelSource; // overrides pszCurFile in relative tracking
  250. TargetContextMenu _cmTarget; // stuff for IContextMenu
  251. CDarwinContextMenuCB *_pcbDarwin;
  252. UINT _indexMenuSave;
  253. UINT _idCmdFirstSave;
  254. UINT _idCmdLastSave;
  255. UINT _uFlagsSave;
  256. // IDropTarget specific
  257. IDropTarget* _pdtSrc; // IDropTarget of link source (unresolved)
  258. DWORD _grfKeyStateLast;
  259. IExtractIconW *_pxi; // for IExtractIcon support
  260. IExtractIconA *_pxiA;
  261. IExtractImage *_pxthumb;
  262. UINT _gilFlags; // ::GetIconLocation() flags
  263. // persistant data
  264. LPITEMIDLIST _pidl; // may be NULL
  265. PLINKINFO _pli; // may be NULL
  266. LPTSTR _pszName; // title on short volumes
  267. LPTSTR _pszRelPath;
  268. LPTSTR _pszWorkingDir;
  269. LPTSTR _pszArgs;
  270. LPTSTR _pszIconLocation;
  271. LPDBLIST _pExtraData; // extra data to preserve for future compatibility
  272. CTracker *_ptracker;
  273. WORD _wOldHotkey; // to broadcast hotkey changes
  274. WORD _wAllign;
  275. SHELL_LINK_DATA _sld;
  276. BOOL _bExpandedIcon; // have we already tried to update the icon from the env variable for this instance?
  277. // IFilter stuff
  278. UINT _iChunkIndex;
  279. UINT _iValueIndex;
  280. LPWSTR _pszPrefix;
  281. };
  282. DECLARE_INTERFACE_(ISLTracker, IUnknown)
  283. {
  284. STDMETHOD(QueryInterface)(REFIID riid, void **ppv);
  285. STDMETHOD_(ULONG, AddRef)();
  286. STDMETHOD_(ULONG, Release)();
  287. STDMETHOD(Resolve)(HWND hwnd, DWORD fFlags, DWORD TrackerRestrictions) PURE;
  288. STDMETHOD(GetIDs)(CDomainRelativeObjId *pdroidBirth, CDomainRelativeObjId *pdroidLast, CMachineId *pmcid) PURE;
  289. STDMETHOD(CancelSearch)() PURE;
  290. };
  291. // This class implements the object ID-based link tracking (new to NT5).
  292. class CTracker : public ISLTracker
  293. {
  294. public:
  295. // IUnknown
  296. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  297. STDMETHODIMP_(ULONG) AddRef();
  298. STDMETHODIMP_(ULONG) Release();
  299. // ISLTracker
  300. STDMETHODIMP Resolve(HWND hwnd, DWORD fFlags, DWORD TrackerRestrictions);
  301. STDMETHODIMP GetIDs(CDomainRelativeObjId *pdroidBirth, CDomainRelativeObjId *pdroidLast, CMachineId *pmcid);
  302. CTracker(CShellLink *psl) : _psl(psl)
  303. {
  304. _fLoadedAtLeastOnce = _fLoaded = _fDirty = FALSE;
  305. _fCritsecInitialized = _fMendInProgress = _fUserCancelled = FALSE;
  306. _hEvent = NULL;
  307. _pRpcAsyncState = NULL;
  308. };
  309. ~CTracker()
  310. {
  311. if (_fCritsecInitialized)
  312. {
  313. DeleteCriticalSection(&_cs);
  314. _fCritsecInitialized = FALSE;
  315. }
  316. if (NULL != _pRpcAsyncState)
  317. {
  318. delete _pRpcAsyncState;
  319. _pRpcAsyncState = NULL;
  320. }
  321. if (NULL != _hEvent)
  322. {
  323. CloseHandle(_hEvent);
  324. _hEvent = NULL;
  325. }
  326. }
  327. // Initialization.
  328. HRESULT InitFromHandle(const HANDLE hFile, const TCHAR* ptszFile);
  329. HRESULT InitNew();
  330. void UnInit();
  331. // Load and Save
  332. HRESULT Load(BYTE *pb, ULONG cb);
  333. ULONG GetSize()
  334. {
  335. return sizeof(DWORD) // To save the length
  336. + sizeof(DWORD) // To save flags
  337. + sizeof(_mcidLast) + sizeof(_droidLast) + sizeof(_droidBirth);
  338. }
  339. void Save(BYTE *pb, ULONG cb);
  340. // Search for a file
  341. HRESULT Search(const DWORD dwTickCountDeadline,
  342. const WIN32_FIND_DATA *pfdIn,
  343. WIN32_FIND_DATA *pfdOut,
  344. UINT uShlinkFlags,
  345. DWORD TrackerRestrictions);
  346. STDMETHODIMP CancelSearch(); // Also in ISLTracker
  347. BOOL IsDirty()
  348. {
  349. return _fDirty;
  350. }
  351. BOOL IsLoaded()
  352. {
  353. return _fLoaded;
  354. }
  355. BOOL WasLoadedAtLeastOnce()
  356. {
  357. return _fLoadedAtLeastOnce;
  358. }
  359. private:
  360. // Call this from either InitNew or Load
  361. HRESULT InitRPC();
  362. BOOL _fDirty:1;
  363. // TRUE => InitNew has be called, but neither InitFromHandle nor Load
  364. // has been called since.
  365. BOOL _fLoaded:1;
  366. // TRUE => _cs has been intialized and must be deleted on destruction.
  367. BOOL _fCritsecInitialized:1;
  368. // TRUE => An async call to LnkMendLink is active
  369. BOOL _fMendInProgress:1;
  370. BOOL _fUserCancelled:1;
  371. // Event used for the async RPC call LnkMendLink, and a critsec to
  372. // coordinate the search thread and UI thread.
  373. HANDLE _hEvent;
  374. CRITICAL_SECTION _cs;
  375. // Either InitFromHandle or Load has been called at least once, though
  376. // InitNew may have been called since.
  377. BOOL _fLoadedAtLeastOnce:1;
  378. CShellLink *_psl;
  379. PRPC_ASYNC_STATE _pRpcAsyncState;
  380. CMachineId _mcidLast;
  381. CDomainRelativeObjId _droidLast;
  382. CDomainRelativeObjId _droidBirth;
  383. };
  384. #endif //__SHLINK_H__