Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / shell32 / shelllink.c
blobe304058488fc5df216e1f270f99c59da8baeb773
1 /*
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES
22 * Nearly complete information about the binary formats
23 * of .lnk files available at http://www.wotsit.org
25 * You can use winedump to examine the contents of a link file:
26 * winedump lnk sc.lnk
28 * MSI advertised shortcuts are totally undocumented. They provide an
29 * icon for a program that is not yet installed, and invoke MSI to
30 * install the program when the shortcut is clicked on. They are
31 * created by passing a special string to SetPath, and the information
32 * in that string is parsed an stored.
35 #define COBJMACROS
36 #define NONAMELESSUNION
38 #include "wine/debug.h"
39 #include "winerror.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winreg.h"
45 #include "winuser.h"
46 #include "wingdi.h"
47 #include "shlobj.h"
48 #include "undocshell.h"
50 #include "pidl.h"
51 #include "shell32_main.h"
52 #include "shlguid.h"
53 #include "shlwapi.h"
54 #include "msi.h"
55 #include "appmgmt.h"
57 #include "initguid.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(shell);
61 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
62 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
64 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
66 /* link file formats */
68 #include "pshpack1.h"
70 typedef struct _LINK_HEADER
72 DWORD dwSize; /* 0x00 size of the header - 0x4c */
73 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
74 DWORD dwFlags; /* 0x14 describes elements following */
75 DWORD dwFileAttr; /* 0x18 attributes of the target file */
76 FILETIME Time1; /* 0x1c */
77 FILETIME Time2; /* 0x24 */
78 FILETIME Time3; /* 0x2c */
79 DWORD dwFileLength; /* 0x34 File length */
80 DWORD nIcon; /* 0x38 icon number */
81 DWORD fStartup; /* 0x3c startup type */
82 DWORD wHotKey; /* 0x40 hotkey */
83 DWORD Unknown5; /* 0x44 */
84 DWORD Unknown6; /* 0x48 */
85 } LINK_HEADER, * PLINK_HEADER;
87 #define SHLINK_LOCAL 0
88 #define SHLINK_REMOTE 1
90 typedef struct _LOCATION_INFO
92 DWORD dwTotalSize;
93 DWORD dwHeaderSize;
94 DWORD dwFlags;
95 DWORD dwVolTableOfs;
96 DWORD dwLocalPathOfs;
97 DWORD dwNetworkVolTableOfs;
98 DWORD dwFinalPathOfs;
99 } LOCATION_INFO;
101 typedef struct _LOCAL_VOLUME_INFO
103 DWORD dwSize;
104 DWORD dwType;
105 DWORD dwVolSerial;
106 DWORD dwVolLabelOfs;
107 } LOCAL_VOLUME_INFO;
109 typedef struct volume_info_t
111 DWORD type;
112 DWORD serial;
113 WCHAR label[12]; /* assume 8.3 */
114 } volume_info;
116 #include "poppack.h"
118 static const IShellLinkAVtbl slvt;
119 static const IShellLinkWVtbl slvtw;
120 static const IPersistFileVtbl pfvt;
121 static const IPersistStreamVtbl psvt;
122 static const IShellLinkDataListVtbl dlvt;
123 static const IShellExtInitVtbl eivt;
124 static const IContextMenuVtbl cmvt;
125 static const IObjectWithSiteVtbl owsvt;
127 /* IShellLink Implementation */
129 typedef struct
131 const IShellLinkAVtbl *lpVtbl;
132 const IShellLinkWVtbl *lpvtblw;
133 const IPersistFileVtbl *lpvtblPersistFile;
134 const IPersistStreamVtbl *lpvtblPersistStream;
135 const IShellLinkDataListVtbl *lpvtblShellLinkDataList;
136 const IShellExtInitVtbl *lpvtblShellExtInit;
137 const IContextMenuVtbl *lpvtblContextMenu;
138 const IObjectWithSiteVtbl *lpvtblObjectWithSite;
140 LONG ref;
142 /* data structures according to the information in the link */
143 LPITEMIDLIST pPidl;
144 WORD wHotKey;
145 SYSTEMTIME time1;
146 SYSTEMTIME time2;
147 SYSTEMTIME time3;
149 DWORD iShowCmd;
150 LPWSTR sIcoPath;
151 INT iIcoNdx;
152 LPWSTR sPath;
153 LPWSTR sArgs;
154 LPWSTR sWorkDir;
155 LPWSTR sDescription;
156 LPWSTR sPathRel;
157 LPWSTR sProduct;
158 LPWSTR sComponent;
159 volume_info volume;
161 BOOL bDirty;
162 INT iIdOpen; /* id of the "Open" entry in the context menu */
163 IUnknown *site;
164 } IShellLinkImpl;
166 static inline IShellLinkImpl *impl_from_IShellLinkW( IShellLinkW *iface )
168 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
171 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
173 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
176 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
178 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
181 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
183 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
186 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
188 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
191 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
193 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
196 static inline IShellLinkImpl *impl_from_IObjectWithSite( IObjectWithSite *iface )
198 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblObjectWithSite));
201 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
203 /* strdup on the process heap */
204 static inline LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
206 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
207 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
208 if( !p )
209 return p;
210 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
211 return p;
214 static inline LPWSTR strdupW( LPCWSTR src )
216 LPWSTR dest;
217 if (!src) return NULL;
218 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
219 if (dest)
220 lstrcpyW(dest, src);
221 return dest;
224 /**************************************************************************
225 * ShellLink::QueryInterface implementation
227 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
229 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
231 *ppvObj = NULL;
233 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
235 *ppvObj = This;
237 else if(IsEqualIID(riid, &IID_IShellLinkW))
239 *ppvObj = &(This->lpvtblw);
241 else if(IsEqualIID(riid, &IID_IPersistFile))
243 *ppvObj = &(This->lpvtblPersistFile);
245 else if(IsEqualIID(riid, &IID_IPersistStream))
247 *ppvObj = &(This->lpvtblPersistStream);
249 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
251 *ppvObj = &(This->lpvtblShellLinkDataList);
253 else if(IsEqualIID(riid, &IID_IShellExtInit))
255 *ppvObj = &(This->lpvtblShellExtInit);
257 else if(IsEqualIID(riid, &IID_IContextMenu))
259 *ppvObj = &(This->lpvtblContextMenu);
261 else if(IsEqualIID(riid, &IID_IObjectWithSite))
263 *ppvObj = &(This->lpvtblObjectWithSite);
266 if(*ppvObj)
268 IUnknown_AddRef((IUnknown*)(*ppvObj));
269 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
270 return S_OK;
272 ERR("-- Interface: E_NOINTERFACE\n");
273 return E_NOINTERFACE;
276 /**************************************************************************
277 * ShellLink::AddRef implementation
279 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
281 ULONG refCount = InterlockedIncrement(&This->ref);
283 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
285 return refCount;
288 /**************************************************************************
289 * ShellLink::Release implementation
291 static ULONG ShellLink_Release( IShellLinkImpl *This )
293 ULONG refCount = InterlockedDecrement(&This->ref);
295 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
297 if (refCount)
298 return refCount;
300 TRACE("-- destroying IShellLink(%p)\n",This);
302 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
303 HeapFree(GetProcessHeap(), 0, This->sArgs);
304 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
305 HeapFree(GetProcessHeap(), 0, This->sDescription);
306 HeapFree(GetProcessHeap(), 0, This->sPath);
307 HeapFree(GetProcessHeap(), 0, This->sPathRel);
308 HeapFree(GetProcessHeap(), 0, This->sProduct);
309 HeapFree(GetProcessHeap(), 0, This->sComponent);
311 if (This->site)
312 IUnknown_Release( This->site );
314 if (This->pPidl)
315 ILFree(This->pPidl);
317 LocalFree(This);
319 return 0;
322 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
324 TRACE("%p %p\n", This, pclsid);
326 *pclsid = CLSID_ShellLink;
327 return S_OK;
330 /**************************************************************************
331 * IPersistFile_QueryInterface
333 static HRESULT WINAPI IPersistFile_fnQueryInterface(
334 IPersistFile* iface,
335 REFIID riid,
336 LPVOID *ppvObj)
338 IShellLinkImpl *This = impl_from_IPersistFile(iface);
339 return ShellLink_QueryInterface( This, riid, ppvObj );
342 /******************************************************************************
343 * IPersistFile_AddRef
345 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
347 IShellLinkImpl *This = impl_from_IPersistFile(iface);
348 return ShellLink_AddRef( This );
351 /******************************************************************************
352 * IPersistFile_Release
354 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
356 IShellLinkImpl *This = impl_from_IPersistFile(iface);
357 return IShellLinkA_Release((IShellLinkA*)This);
360 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
362 IShellLinkImpl *This = impl_from_IPersistFile(iface);
363 return ShellLink_GetClassID( This, pClassID );
366 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
368 IShellLinkImpl *This = impl_from_IPersistFile(iface);
370 TRACE("(%p)\n",This);
372 if (This->bDirty)
373 return S_OK;
375 return S_FALSE;
378 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
380 IShellLinkImpl *This = impl_from_IPersistFile(iface);
381 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
382 HRESULT r;
383 IStream *stm;
385 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
387 if( dwMode == 0 )
388 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
389 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
390 if( SUCCEEDED( r ) )
392 r = IPersistStream_Load(StreamThis, stm);
393 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
394 IStream_Release( stm );
395 This->bDirty = FALSE;
397 TRACE("-- returning hr %08x\n", r);
398 return r;
401 BOOL run_winemenubuilder( const WCHAR *args )
403 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
404 LONG len;
405 LPWSTR buffer;
406 STARTUPINFOW si;
407 PROCESS_INFORMATION pi;
408 BOOL ret;
409 WCHAR app[MAX_PATH];
411 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
412 strcatW( app, menubuilder );
414 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
415 buffer = HeapAlloc( GetProcessHeap(), 0, len );
416 if( !buffer )
417 return FALSE;
419 strcpyW( buffer, app );
420 strcatW( buffer, args );
422 TRACE("starting %s\n",debugstr_w(buffer));
424 memset(&si, 0, sizeof(si));
425 si.cb = sizeof(si);
427 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
429 HeapFree( GetProcessHeap(), 0, buffer );
431 if (ret)
433 CloseHandle( pi.hProcess );
434 CloseHandle( pi.hThread );
437 return ret;
440 static BOOL StartLinkProcessor( LPCOLESTR szLink )
442 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
443 LONG len;
444 LPWSTR buffer;
445 BOOL ret;
447 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
448 buffer = HeapAlloc( GetProcessHeap(), 0, len );
449 if( !buffer )
450 return FALSE;
452 wsprintfW( buffer, szFormat, szLink );
453 ret = run_winemenubuilder( buffer );
454 HeapFree( GetProcessHeap(), 0, buffer );
455 return ret;
458 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
460 IShellLinkImpl *This = impl_from_IPersistFile(iface);
461 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
462 HRESULT r;
463 IStream *stm;
465 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
467 if (!pszFileName)
468 return E_FAIL;
470 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
471 if( SUCCEEDED( r ) )
473 r = IPersistStream_Save(StreamThis, stm, FALSE);
474 IStream_Release( stm );
476 if( SUCCEEDED( r ) )
478 StartLinkProcessor( pszFileName );
480 This->bDirty = FALSE;
482 else
484 DeleteFileW( pszFileName );
485 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
489 return r;
492 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
494 IShellLinkImpl *This = impl_from_IPersistFile(iface);
495 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
496 return NOERROR;
499 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
501 IShellLinkImpl *This = impl_from_IPersistFile(iface);
502 FIXME("(%p)\n",This);
503 return NOERROR;
506 static const IPersistFileVtbl pfvt =
508 IPersistFile_fnQueryInterface,
509 IPersistFile_fnAddRef,
510 IPersistFile_fnRelease,
511 IPersistFile_fnGetClassID,
512 IPersistFile_fnIsDirty,
513 IPersistFile_fnLoad,
514 IPersistFile_fnSave,
515 IPersistFile_fnSaveCompleted,
516 IPersistFile_fnGetCurFile
519 /************************************************************************
520 * IPersistStream_QueryInterface
522 static HRESULT WINAPI IPersistStream_fnQueryInterface(
523 IPersistStream* iface,
524 REFIID riid,
525 VOID** ppvObj)
527 IShellLinkImpl *This = impl_from_IPersistStream(iface);
528 return ShellLink_QueryInterface( This, riid, ppvObj );
531 /************************************************************************
532 * IPersistStream_Release
534 static ULONG WINAPI IPersistStream_fnRelease(
535 IPersistStream* iface)
537 IShellLinkImpl *This = impl_from_IPersistStream(iface);
538 return IShellLinkA_Release((IShellLinkA*)This);
541 /************************************************************************
542 * IPersistStream_AddRef
544 static ULONG WINAPI IPersistStream_fnAddRef(
545 IPersistStream* iface)
547 IShellLinkImpl *This = impl_from_IPersistStream(iface);
548 return ShellLink_AddRef( This );
551 /************************************************************************
552 * IPersistStream_GetClassID
555 static HRESULT WINAPI IPersistStream_fnGetClassID(
556 IPersistStream* iface,
557 CLSID* pClassID)
559 IShellLinkImpl *This = impl_from_IPersistStream(iface);
560 return ShellLink_GetClassID( This, pClassID );
563 /************************************************************************
564 * IPersistStream_IsDirty (IPersistStream)
566 static HRESULT WINAPI IPersistStream_fnIsDirty(
567 IPersistStream* iface)
569 IShellLinkImpl *This = impl_from_IPersistStream(iface);
571 TRACE("(%p)\n", This);
573 return S_OK;
577 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
579 DWORD count;
580 USHORT len;
581 LPVOID temp;
582 LPWSTR str;
583 HRESULT r;
585 TRACE("%p\n", stm);
587 count = 0;
588 r = IStream_Read(stm, &len, sizeof(len), &count);
589 if ( FAILED (r) || ( count != sizeof(len) ) )
590 return E_FAIL;
592 if( unicode )
593 len *= sizeof (WCHAR);
595 TRACE("reading %d\n", len);
596 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
597 if( !temp )
598 return E_OUTOFMEMORY;
599 count = 0;
600 r = IStream_Read(stm, temp, len, &count);
601 if( FAILED (r) || ( count != len ) )
603 HeapFree( GetProcessHeap(), 0, temp );
604 return E_FAIL;
607 TRACE("read %s\n", debugstr_an(temp,len));
609 /* convert to unicode if necessary */
610 if( !unicode )
612 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
613 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
614 if( !str )
616 HeapFree( GetProcessHeap(), 0, temp );
617 return E_OUTOFMEMORY;
619 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
620 HeapFree( GetProcessHeap(), 0, temp );
622 else
624 count /= 2;
625 str = temp;
627 str[count] = 0;
629 *pstr = str;
631 return S_OK;
634 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
636 DWORD size;
637 ULONG count;
638 HRESULT r;
639 struct sized_chunk {
640 DWORD size;
641 unsigned char data[1];
642 } *chunk;
644 TRACE("%p\n",stm);
646 r = IStream_Read( stm, &size, sizeof(size), &count );
647 if( FAILED( r ) || count != sizeof(size) )
648 return E_FAIL;
650 chunk = HeapAlloc( GetProcessHeap(), 0, size );
651 if( !chunk )
652 return E_OUTOFMEMORY;
654 chunk->size = size;
655 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
656 if( FAILED( r ) || count != (size - sizeof(size)) )
658 HeapFree( GetProcessHeap(), 0, chunk );
659 return E_FAIL;
662 TRACE("Read %d bytes\n",chunk->size);
664 *data = chunk;
666 return S_OK;
669 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
671 const int label_sz = sizeof volume->label/sizeof volume->label[0];
672 LPSTR label;
673 int len;
675 volume->serial = vol->dwVolSerial;
676 volume->type = vol->dwType;
678 if( !vol->dwVolLabelOfs )
679 return FALSE;
680 if( vol->dwSize <= vol->dwVolLabelOfs )
681 return FALSE;
682 len = vol->dwSize - vol->dwVolLabelOfs;
684 label = (LPSTR) vol;
685 label += vol->dwVolLabelOfs;
686 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
688 return TRUE;
691 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
693 int len = 0, wlen;
694 LPWSTR path;
696 while( p[len] && (len < maxlen) )
697 len++;
699 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
700 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
701 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
702 path[wlen] = 0;
704 return path;
707 static HRESULT Stream_LoadLocation( IStream *stm,
708 volume_info *volume, LPWSTR *path )
710 char *p = NULL;
711 LOCATION_INFO *loc;
712 HRESULT r;
713 DWORD n;
715 r = Stream_ReadChunk( stm, (LPVOID*) &p );
716 if( FAILED(r) )
717 return r;
719 loc = (LOCATION_INFO*) p;
720 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
722 HeapFree( GetProcessHeap(), 0, p );
723 return E_FAIL;
726 /* if there's valid local volume information, load it */
727 if( loc->dwVolTableOfs &&
728 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
730 LOCAL_VOLUME_INFO *volume_info;
732 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
733 Stream_LoadVolume( volume_info, volume );
736 /* if there's a local path, load it */
737 n = loc->dwLocalPathOfs;
738 if( n && (n < loc->dwTotalSize) )
739 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
741 TRACE("type %d serial %08x name %s path %s\n", volume->type,
742 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
744 HeapFree( GetProcessHeap(), 0, p );
745 return S_OK;
749 * The format of the advertised shortcut info seems to be:
751 * Offset Description
752 * ------ -----------
754 * 0 Length of the block (4 bytes, usually 0x314)
755 * 4 tag (dword)
756 * 8 string data in ASCII
757 * 8+0x104 string data in UNICODE
759 * In the original Win32 implementation the buffers are not initialized
760 * to zero, so data trailing the string is random garbage.
762 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
764 DWORD size;
765 ULONG count;
766 HRESULT r;
767 EXP_DARWIN_LINK buffer;
769 TRACE("%p\n",stm);
771 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
772 if( FAILED( r ) )
773 return r;
775 /* make sure that we read the size of the structure even on error */
776 size = sizeof buffer - sizeof (DWORD);
777 if( buffer.dbh.cbSize != sizeof buffer )
779 ERR("Ooops. This structure is not as expected...\n");
780 return E_FAIL;
783 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
784 if( FAILED( r ) )
785 return r;
787 if( count != size )
788 return E_FAIL;
790 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
792 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
794 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
795 return E_FAIL;
798 *str = HeapAlloc( GetProcessHeap(), 0,
799 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
800 lstrcpyW( *str, buffer.szwDarwinID );
802 return S_OK;
805 /************************************************************************
806 * IPersistStream_Load (IPersistStream)
808 static HRESULT WINAPI IPersistStream_fnLoad(
809 IPersistStream* iface,
810 IStream* stm)
812 LINK_HEADER hdr;
813 ULONG dwBytesRead;
814 BOOL unicode;
815 HRESULT r;
816 DWORD zero;
818 IShellLinkImpl *This = impl_from_IPersistStream(iface);
820 TRACE("%p %p\n", This, stm);
822 if( !stm )
823 return STG_E_INVALIDPOINTER;
825 dwBytesRead = 0;
826 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
827 if( FAILED( r ) )
828 return r;
830 if( dwBytesRead != sizeof(hdr))
831 return E_FAIL;
832 if( hdr.dwSize != sizeof(hdr))
833 return E_FAIL;
834 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
835 return E_FAIL;
837 /* free all the old stuff */
838 ILFree(This->pPidl);
839 This->pPidl = NULL;
840 memset( &This->volume, 0, sizeof This->volume );
841 HeapFree(GetProcessHeap(), 0, This->sPath);
842 This->sPath = NULL;
843 HeapFree(GetProcessHeap(), 0, This->sDescription);
844 This->sDescription = NULL;
845 HeapFree(GetProcessHeap(), 0, This->sPathRel);
846 This->sPathRel = NULL;
847 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
848 This->sWorkDir = NULL;
849 HeapFree(GetProcessHeap(), 0, This->sArgs);
850 This->sArgs = NULL;
851 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
852 This->sIcoPath = NULL;
853 HeapFree(GetProcessHeap(), 0, This->sProduct);
854 This->sProduct = NULL;
855 HeapFree(GetProcessHeap(), 0, This->sComponent);
856 This->sComponent = NULL;
858 This->wHotKey = (WORD)hdr.wHotKey;
859 This->iIcoNdx = hdr.nIcon;
860 FileTimeToSystemTime (&hdr.Time1, &This->time1);
861 FileTimeToSystemTime (&hdr.Time2, &This->time2);
862 FileTimeToSystemTime (&hdr.Time3, &This->time3);
863 if (TRACE_ON(shell))
865 WCHAR sTemp[MAX_PATH];
866 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
867 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
868 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
869 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
870 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
871 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
872 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
873 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
874 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
877 /* load all the new stuff */
878 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
880 r = ILLoadFromStream( stm, &This->pPidl );
881 if( FAILED( r ) )
882 return r;
884 pdump(This->pPidl);
886 /* load the location information */
887 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
888 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
889 if( FAILED( r ) )
890 goto end;
892 unicode = hdr.dwFlags & SLDF_UNICODE;
893 if( hdr.dwFlags & SLDF_HAS_NAME )
895 r = Stream_LoadString( stm, unicode, &This->sDescription );
896 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
898 if( FAILED( r ) )
899 goto end;
901 if( hdr.dwFlags & SLDF_HAS_RELPATH )
903 r = Stream_LoadString( stm, unicode, &This->sPathRel );
904 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
906 if( FAILED( r ) )
907 goto end;
909 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
911 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
912 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
914 if( FAILED( r ) )
915 goto end;
917 if( hdr.dwFlags & SLDF_HAS_ARGS )
919 r = Stream_LoadString( stm, unicode, &This->sArgs );
920 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
922 if( FAILED( r ) )
923 goto end;
925 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
927 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
928 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
930 if( FAILED( r ) )
931 goto end;
933 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
935 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
936 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
938 if( FAILED( r ) )
939 goto end;
941 if( hdr.dwFlags & SLDF_HAS_DARWINID )
943 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
944 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
946 if( FAILED( r ) )
947 goto end;
949 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
950 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
952 /* Some lnk files have extra data blocks starting with a
953 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
954 * one with a 0xa0000003 signature. However these don't seem to matter
955 * too much.
957 WARN("Last word was not zero\n");
960 TRACE("OK\n");
962 pdump (This->pPidl);
964 return S_OK;
965 end:
966 return r;
969 /************************************************************************
970 * Stream_WriteString
972 * Helper function for IPersistStream_Save. Writes a unicode string
973 * with terminating nul byte to a stream, preceded by the its length.
975 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
977 USHORT len = lstrlenW( str ) + 1;
978 DWORD count;
979 HRESULT r;
981 r = IStream_Write( stm, &len, sizeof(len), &count );
982 if( FAILED( r ) )
983 return r;
985 len *= sizeof(WCHAR);
987 r = IStream_Write( stm, str, len, &count );
988 if( FAILED( r ) )
989 return r;
991 return S_OK;
994 /************************************************************************
995 * Stream_WriteLocationInfo
997 * Writes the location info to a stream
999 * FIXME: One day we might want to write the network volume information
1000 * and the final path.
1001 * Figure out how Windows deals with unicode paths here.
1003 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
1004 volume_info *volume )
1006 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
1007 LOCAL_VOLUME_INFO *vol;
1008 LOCATION_INFO *loc;
1009 LPSTR szLabel, szPath, szFinalPath;
1010 ULONG count = 0;
1011 HRESULT hr;
1013 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
1015 /* figure out the size of everything */
1016 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1017 NULL, 0, NULL, NULL );
1018 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
1019 NULL, 0, NULL, NULL );
1020 volume_info_size = sizeof *vol + label_size;
1021 final_path_size = 1;
1022 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
1024 /* create pointers to everything */
1025 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
1026 vol = (LOCAL_VOLUME_INFO*) &loc[1];
1027 szLabel = (LPSTR) &vol[1];
1028 szPath = &szLabel[label_size];
1029 szFinalPath = &szPath[path_size];
1031 /* fill in the location information header */
1032 loc->dwTotalSize = total_size;
1033 loc->dwHeaderSize = sizeof (*loc);
1034 loc->dwFlags = 1;
1035 loc->dwVolTableOfs = sizeof (*loc);
1036 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1037 loc->dwNetworkVolTableOfs = 0;
1038 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1040 /* fill in the volume information */
1041 vol->dwSize = volume_info_size;
1042 vol->dwType = volume->type;
1043 vol->dwVolSerial = volume->serial;
1044 vol->dwVolLabelOfs = sizeof (*vol);
1046 /* copy in the strings */
1047 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1048 szLabel, label_size, NULL, NULL );
1049 WideCharToMultiByte( CP_ACP, 0, path, -1,
1050 szPath, path_size, NULL, NULL );
1051 szFinalPath[0] = 0;
1053 hr = IStream_Write( stm, loc, total_size, &count );
1054 HeapFree(GetProcessHeap(), 0, loc);
1056 return hr;
1059 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1061 EXP_DARWIN_LINK *buffer;
1063 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1064 buffer->dbh.cbSize = sizeof *buffer;
1065 buffer->dbh.dwSignature = magic;
1066 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1067 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1069 return buffer;
1072 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1074 EXP_DARWIN_LINK *buffer;
1075 ULONG count;
1077 TRACE("%p\n",stm);
1079 buffer = shelllink_build_darwinid( string, magic );
1081 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1084 /************************************************************************
1085 * IPersistStream_Save (IPersistStream)
1087 * FIXME: makes assumptions about byte order
1089 static HRESULT WINAPI IPersistStream_fnSave(
1090 IPersistStream* iface,
1091 IStream* stm,
1092 BOOL fClearDirty)
1094 LINK_HEADER header;
1095 ULONG count;
1096 DWORD zero;
1097 HRESULT r;
1099 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1101 TRACE("%p %p %x\n", This, stm, fClearDirty);
1103 memset(&header, 0, sizeof(header));
1104 header.dwSize = sizeof(header);
1105 header.fStartup = This->iShowCmd;
1106 header.MagicGuid = CLSID_ShellLink;
1108 header.wHotKey = This->wHotKey;
1109 header.nIcon = This->iIcoNdx;
1110 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1111 if( This->pPidl )
1112 header.dwFlags |= SLDF_HAS_ID_LIST;
1113 if( This->sPath )
1114 header.dwFlags |= SLDF_HAS_LINK_INFO;
1115 if( This->sDescription )
1116 header.dwFlags |= SLDF_HAS_NAME;
1117 if( This->sWorkDir )
1118 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1119 if( This->sArgs )
1120 header.dwFlags |= SLDF_HAS_ARGS;
1121 if( This->sIcoPath )
1122 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1123 if( This->sProduct )
1124 header.dwFlags |= SLDF_HAS_LOGO3ID;
1125 if( This->sComponent )
1126 header.dwFlags |= SLDF_HAS_DARWINID;
1128 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1129 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1130 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1132 /* write the Shortcut header */
1133 r = IStream_Write( stm, &header, sizeof(header), &count );
1134 if( FAILED( r ) )
1136 ERR("Write failed at %d\n",__LINE__);
1137 return r;
1140 TRACE("Writing pidl\n");
1142 /* write the PIDL to the shortcut */
1143 if( This->pPidl )
1145 r = ILSaveToStream( stm, This->pPidl );
1146 if( FAILED( r ) )
1148 ERR("Failed to write PIDL at %d\n",__LINE__);
1149 return r;
1153 if( This->sPath )
1154 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1156 if( This->sDescription )
1157 r = Stream_WriteString( stm, This->sDescription );
1159 if( This->sPathRel )
1160 r = Stream_WriteString( stm, This->sPathRel );
1162 if( This->sWorkDir )
1163 r = Stream_WriteString( stm, This->sWorkDir );
1165 if( This->sArgs )
1166 r = Stream_WriteString( stm, This->sArgs );
1168 if( This->sIcoPath )
1169 r = Stream_WriteString( stm, This->sIcoPath );
1171 if( This->sProduct )
1172 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1174 if( This->sComponent )
1175 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1177 /* the last field is a single zero dword */
1178 zero = 0;
1179 r = IStream_Write( stm, &zero, sizeof zero, &count );
1181 return S_OK;
1184 /************************************************************************
1185 * IPersistStream_GetSizeMax (IPersistStream)
1187 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1188 IPersistStream* iface,
1189 ULARGE_INTEGER* pcbSize)
1191 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1193 TRACE("(%p)\n", This);
1195 return E_NOTIMPL;
1198 static const IPersistStreamVtbl psvt =
1200 IPersistStream_fnQueryInterface,
1201 IPersistStream_fnAddRef,
1202 IPersistStream_fnRelease,
1203 IPersistStream_fnGetClassID,
1204 IPersistStream_fnIsDirty,
1205 IPersistStream_fnLoad,
1206 IPersistStream_fnSave,
1207 IPersistStream_fnGetSizeMax
1210 /**************************************************************************
1211 * IShellLink_Constructor
1213 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1214 REFIID riid, LPVOID *ppv )
1216 IShellLinkImpl * sl;
1217 HRESULT r;
1219 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1221 *ppv = NULL;
1223 if (pUnkOuter)
1224 return CLASS_E_NOAGGREGATION;
1225 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1226 if (!sl)
1227 return E_OUTOFMEMORY;
1229 sl->ref = 1;
1230 sl->lpVtbl = &slvt;
1231 sl->lpvtblw = &slvtw;
1232 sl->lpvtblPersistFile = &pfvt;
1233 sl->lpvtblPersistStream = &psvt;
1234 sl->lpvtblShellLinkDataList = &dlvt;
1235 sl->lpvtblShellExtInit = &eivt;
1236 sl->lpvtblContextMenu = &cmvt;
1237 sl->lpvtblObjectWithSite = &owsvt;
1238 sl->iShowCmd = SW_SHOWNORMAL;
1239 sl->bDirty = FALSE;
1240 sl->iIdOpen = -1;
1241 sl->site = NULL;
1243 TRACE("(%p)->()\n",sl);
1245 r = ShellLink_QueryInterface( sl, riid, ppv );
1246 ShellLink_Release( sl );
1247 return r;
1251 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1253 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1254 return FALSE;
1255 return TRUE;
1258 /**************************************************************************
1259 * ShellLink_UpdatePath
1260 * update absolute path in sPath using relative path in sPathRel
1262 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1264 if (!path || !psPath)
1265 return E_INVALIDARG;
1267 if (!*psPath && sPathRel) {
1268 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1269 LPWSTR final = NULL;
1271 /* first try if [directory of link file] + [relative path] finds an existing file */
1273 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1274 if( !final )
1275 final = buffer;
1276 lstrcpyW(final, sPathRel);
1278 *abs_path = '\0';
1280 if (SHELL_ExistsFileW(buffer)) {
1281 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1282 lstrcpyW(abs_path, buffer);
1283 } else {
1284 /* try if [working directory] + [relative path] finds an existing file */
1285 if (sWorkDir) {
1286 lstrcpyW(buffer, sWorkDir);
1287 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1289 if (SHELL_ExistsFileW(buffer))
1290 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1291 lstrcpyW(abs_path, buffer);
1295 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1296 if (!*abs_path)
1297 lstrcpyW(abs_path, sPathRel);
1299 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1300 if (!*psPath)
1301 return E_OUTOFMEMORY;
1303 lstrcpyW(*psPath, abs_path);
1306 return S_OK;
1309 /**************************************************************************
1310 * IShellLink_ConstructFromFile
1312 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1313 LPCITEMIDLIST pidl, LPVOID* ppv)
1315 IShellLinkW* psl;
1317 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1319 if (SUCCEEDED(hr)) {
1320 IPersistFile* ppf;
1322 *ppv = NULL;
1324 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1326 if (SUCCEEDED(hr)) {
1327 WCHAR path[MAX_PATH];
1329 if (SHGetPathFromIDListW(pidl, path))
1330 hr = IPersistFile_Load(ppf, path, 0);
1331 else
1332 hr = E_FAIL;
1334 if (SUCCEEDED(hr))
1335 *ppv = psl;
1337 IPersistFile_Release(ppf);
1340 if (!*ppv)
1341 IShellLinkW_Release(psl);
1344 return hr;
1347 /**************************************************************************
1348 * IShellLinkA_QueryInterface
1350 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1352 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1353 return ShellLink_QueryInterface( This, riid, ppvObj );
1356 /******************************************************************************
1357 * IShellLinkA_AddRef
1359 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1361 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1362 return ShellLink_AddRef( This );
1365 /******************************************************************************
1366 * IShellLinkA_Release
1368 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1370 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1371 return ShellLink_Release( This );
1374 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1375 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1377 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1379 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1380 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1382 if (This->sComponent || This->sProduct)
1383 return S_FALSE;
1385 if (cchMaxPath)
1386 pszFile[0] = 0;
1387 if (This->sPath)
1388 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1389 pszFile, cchMaxPath, NULL, NULL);
1391 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1393 return S_OK;
1396 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1398 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1400 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1402 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1405 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1407 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1409 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1411 if (This->pPidl)
1412 ILFree(This->pPidl);
1413 This->pPidl = ILClone (pidl);
1414 This->bDirty = TRUE;
1416 return S_OK;
1419 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1421 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1423 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1425 if( cchMaxName )
1426 pszName[0] = 0;
1427 if( This->sDescription )
1428 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1429 pszName, cchMaxName, NULL, NULL);
1431 return S_OK;
1434 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1436 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1438 TRACE("(%p)->(pName=%s)\n", This, pszName);
1440 HeapFree(GetProcessHeap(), 0, This->sDescription);
1441 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1442 if ( !This->sDescription )
1443 return E_OUTOFMEMORY;
1445 This->bDirty = TRUE;
1447 return S_OK;
1450 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1452 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1454 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1456 if( cchMaxPath )
1457 pszDir[0] = 0;
1458 if( This->sWorkDir )
1459 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1460 pszDir, cchMaxPath, NULL, NULL);
1462 return S_OK;
1465 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1467 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1469 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1471 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1472 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1473 if ( !This->sWorkDir )
1474 return E_OUTOFMEMORY;
1476 This->bDirty = TRUE;
1478 return S_OK;
1481 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1483 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1485 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1487 if( cchMaxPath )
1488 pszArgs[0] = 0;
1489 if( This->sArgs )
1490 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1491 pszArgs, cchMaxPath, NULL, NULL);
1493 return S_OK;
1496 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1498 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1500 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1502 HeapFree(GetProcessHeap(), 0, This->sArgs);
1503 if (pszArgs)
1505 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1506 if( !This->sArgs )
1507 return E_OUTOFMEMORY;
1509 else This->sArgs = NULL;
1511 This->bDirty = TRUE;
1513 return S_OK;
1516 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1518 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1520 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1522 *pwHotkey = This->wHotKey;
1524 return S_OK;
1527 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1529 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1531 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1533 This->wHotKey = wHotkey;
1534 This->bDirty = TRUE;
1536 return S_OK;
1539 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1541 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1543 TRACE("(%p)->(%p)\n",This, piShowCmd);
1544 *piShowCmd = This->iShowCmd;
1545 return S_OK;
1548 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1550 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1552 TRACE("(%p) %d\n",This, iShowCmd);
1554 This->iShowCmd = iShowCmd;
1555 This->bDirty = TRUE;
1557 return NOERROR;
1560 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPCITEMIDLIST pidl,
1561 LPSTR pszIconPath, int cchIconPath, int* piIcon)
1563 LPCITEMIDLIST pidlLast;
1565 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1567 if (SUCCEEDED(hr)) {
1568 IExtractIconA* pei;
1570 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1572 if (SUCCEEDED(hr)) {
1573 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1575 IExtractIconA_Release(pei);
1578 IShellFolder_Release(psf);
1581 return hr;
1584 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1586 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1588 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1590 pszIconPath[0] = 0;
1591 *piIcon = This->iIcoNdx;
1593 if (This->sIcoPath)
1595 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1596 return S_OK;
1599 if (This->pPidl || This->sPath)
1601 IShellFolder* pdsk;
1603 HRESULT hr = SHGetDesktopFolder(&pdsk);
1605 if (SUCCEEDED(hr))
1607 /* first look for an icon using the PIDL (if present) */
1608 if (This->pPidl)
1609 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1610 else
1611 hr = E_FAIL;
1613 /* if we couldn't find an icon yet, look for it using the file system path */
1614 if (FAILED(hr) && This->sPath)
1616 LPITEMIDLIST pidl;
1618 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1620 if (SUCCEEDED(hr)) {
1621 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1623 SHFree(pidl);
1627 IShellFolder_Release(pdsk);
1630 return hr;
1632 return S_OK;
1635 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1637 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1639 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1641 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1642 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1643 if ( !This->sIcoPath )
1644 return E_OUTOFMEMORY;
1646 This->iIcoNdx = iIcon;
1647 This->bDirty = TRUE;
1649 return S_OK;
1652 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1654 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1656 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1658 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1659 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1660 This->bDirty = TRUE;
1662 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1665 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1667 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1669 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1671 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1674 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1676 HRESULT r;
1677 LPWSTR str;
1678 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1680 TRACE("(%p)->(path=%s)\n",This, pszFile);
1682 if (!pszFile) return E_INVALIDARG;
1684 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1685 if( !str )
1686 return E_OUTOFMEMORY;
1688 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1689 HeapFree( GetProcessHeap(), 0, str );
1691 return r;
1694 /**************************************************************************
1695 * IShellLink Implementation
1698 static const IShellLinkAVtbl slvt =
1700 IShellLinkA_fnQueryInterface,
1701 IShellLinkA_fnAddRef,
1702 IShellLinkA_fnRelease,
1703 IShellLinkA_fnGetPath,
1704 IShellLinkA_fnGetIDList,
1705 IShellLinkA_fnSetIDList,
1706 IShellLinkA_fnGetDescription,
1707 IShellLinkA_fnSetDescription,
1708 IShellLinkA_fnGetWorkingDirectory,
1709 IShellLinkA_fnSetWorkingDirectory,
1710 IShellLinkA_fnGetArguments,
1711 IShellLinkA_fnSetArguments,
1712 IShellLinkA_fnGetHotkey,
1713 IShellLinkA_fnSetHotkey,
1714 IShellLinkA_fnGetShowCmd,
1715 IShellLinkA_fnSetShowCmd,
1716 IShellLinkA_fnGetIconLocation,
1717 IShellLinkA_fnSetIconLocation,
1718 IShellLinkA_fnSetRelativePath,
1719 IShellLinkA_fnResolve,
1720 IShellLinkA_fnSetPath
1724 /**************************************************************************
1725 * IShellLinkW_fnQueryInterface
1727 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1728 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1730 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1731 return ShellLink_QueryInterface( This, riid, ppvObj );
1734 /******************************************************************************
1735 * IShellLinkW_fnAddRef
1737 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1739 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1740 return ShellLink_AddRef( This );
1743 /******************************************************************************
1744 * IShellLinkW_fnRelease
1746 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1748 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1749 return ShellLink_Release( This );
1752 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1754 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1756 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1757 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1759 if (This->sComponent || This->sProduct)
1760 return S_FALSE;
1762 if (cchMaxPath)
1763 pszFile[0] = 0;
1764 if (This->sPath)
1765 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1767 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1769 return S_OK;
1772 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1774 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1776 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1778 if (!This->pPidl)
1780 *ppidl = NULL;
1781 return S_FALSE;
1783 *ppidl = ILClone(This->pPidl);
1784 return S_OK;
1787 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1789 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1791 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1793 if( This->pPidl )
1794 ILFree( This->pPidl );
1795 This->pPidl = ILClone( pidl );
1796 if( !This->pPidl )
1797 return E_FAIL;
1799 This->bDirty = TRUE;
1801 return S_OK;
1804 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1806 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1808 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1810 pszName[0] = 0;
1811 if( This->sDescription )
1812 lstrcpynW( pszName, This->sDescription, cchMaxName );
1814 return S_OK;
1817 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1819 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1821 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1823 HeapFree(GetProcessHeap(), 0, This->sDescription);
1824 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1825 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1826 if ( !This->sDescription )
1827 return E_OUTOFMEMORY;
1829 lstrcpyW( This->sDescription, pszName );
1830 This->bDirty = TRUE;
1832 return S_OK;
1835 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1837 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1839 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1841 if( cchMaxPath )
1842 pszDir[0] = 0;
1843 if( This->sWorkDir )
1844 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1846 return S_OK;
1849 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1851 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1853 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1855 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1856 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1857 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1858 if ( !This->sWorkDir )
1859 return E_OUTOFMEMORY;
1860 lstrcpyW( This->sWorkDir, pszDir );
1861 This->bDirty = TRUE;
1863 return S_OK;
1866 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1868 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1870 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1872 if( cchMaxPath )
1873 pszArgs[0] = 0;
1874 if( This->sArgs )
1875 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1877 return NOERROR;
1880 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1882 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1884 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1886 HeapFree(GetProcessHeap(), 0, This->sArgs);
1887 if (pszArgs)
1889 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1890 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1891 if ( !This->sArgs )
1892 return E_OUTOFMEMORY;
1893 lstrcpyW( This->sArgs, pszArgs );
1895 else This->sArgs = NULL;
1897 This->bDirty = TRUE;
1899 return S_OK;
1902 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1904 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1906 TRACE("(%p)->(%p)\n",This, pwHotkey);
1908 *pwHotkey=This->wHotKey;
1910 return S_OK;
1913 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1915 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1917 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1919 This->wHotKey = wHotkey;
1920 This->bDirty = TRUE;
1922 return S_OK;
1925 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1927 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1929 TRACE("(%p)->(%p)\n",This, piShowCmd);
1931 *piShowCmd = This->iShowCmd;
1933 return S_OK;
1936 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1938 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1940 This->iShowCmd = iShowCmd;
1941 This->bDirty = TRUE;
1943 return S_OK;
1946 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1947 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1949 LPCITEMIDLIST pidlLast;
1951 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1953 if (SUCCEEDED(hr)) {
1954 IExtractIconW* pei;
1956 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1958 if (SUCCEEDED(hr)) {
1959 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1961 IExtractIconW_Release(pei);
1964 IShellFolder_Release(psf);
1967 return hr;
1970 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1972 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1974 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1976 pszIconPath[0] = 0;
1977 *piIcon = This->iIcoNdx;
1979 if (This->sIcoPath)
1981 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1982 return S_OK;
1985 if (This->pPidl || This->sPath)
1987 IShellFolder* pdsk;
1989 HRESULT hr = SHGetDesktopFolder(&pdsk);
1991 if (SUCCEEDED(hr))
1993 /* first look for an icon using the PIDL (if present) */
1994 if (This->pPidl)
1995 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1996 else
1997 hr = E_FAIL;
1999 /* if we couldn't find an icon yet, look for it using the file system path */
2000 if (FAILED(hr) && This->sPath)
2002 LPITEMIDLIST pidl;
2004 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
2006 if (SUCCEEDED(hr))
2008 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
2010 SHFree(pidl);
2014 IShellFolder_Release(pdsk);
2016 return hr;
2018 return S_OK;
2021 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
2023 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2025 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
2027 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
2028 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
2029 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
2030 if ( !This->sIcoPath )
2031 return E_OUTOFMEMORY;
2032 lstrcpyW( This->sIcoPath, pszIconPath );
2034 This->iIcoNdx = iIcon;
2035 This->bDirty = TRUE;
2037 return S_OK;
2040 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2042 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2044 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2046 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2047 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2048 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2049 if ( !This->sPathRel )
2050 return E_OUTOFMEMORY;
2051 lstrcpyW( This->sPathRel, pszPathRel );
2052 This->bDirty = TRUE;
2054 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2057 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2059 HRESULT hr = S_OK;
2060 BOOL bSuccess;
2062 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2064 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2066 /*FIXME: use IResolveShellLink interface */
2068 if (!This->sPath && This->pPidl) {
2069 WCHAR buffer[MAX_PATH];
2071 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2073 if (bSuccess && *buffer) {
2074 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2075 if (!This->sPath)
2076 return E_OUTOFMEMORY;
2078 lstrcpyW(This->sPath, buffer);
2080 This->bDirty = TRUE;
2081 } else
2082 hr = S_OK; /* don't report an error occurred while just caching information */
2085 if (!This->sIcoPath && This->sPath) {
2086 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2087 if (!This->sIcoPath)
2088 return E_OUTOFMEMORY;
2090 lstrcpyW(This->sIcoPath, This->sPath);
2091 This->iIcoNdx = 0;
2093 This->bDirty = TRUE;
2096 return hr;
2099 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2101 LPWSTR ret;
2102 LPCWSTR p;
2103 DWORD len;
2105 if( !str )
2106 return NULL;
2108 p = strchrW( str, ':' );
2109 if( !p )
2110 return NULL;
2111 len = p - str;
2112 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2113 if( !ret )
2114 return ret;
2115 memcpy( ret, str, sizeof(WCHAR)*len );
2116 ret[len] = 0;
2117 return ret;
2120 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2122 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2123 WCHAR szGuid[39];
2124 HRESULT r;
2125 GUID guid;
2126 int len;
2128 while( str[0] )
2130 /* each segment must start with two colons */
2131 if( str[0] != ':' || str[1] != ':' )
2132 return E_FAIL;
2134 /* the last segment is just two colons */
2135 if( !str[2] )
2136 break;
2137 str += 2;
2139 /* there must be a colon straight after a guid */
2140 p = strchrW( str, ':' );
2141 if( !p )
2142 return E_FAIL;
2143 len = p - str;
2144 if( len != 38 )
2145 return E_FAIL;
2147 /* get the guid, and check it's validly formatted */
2148 memcpy( szGuid, str, sizeof(WCHAR)*len );
2149 szGuid[len] = 0;
2150 r = CLSIDFromString( szGuid, &guid );
2151 if( r != S_OK )
2152 return r;
2153 str = p + 1;
2155 /* match it up to a guid that we care about */
2156 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2157 szComponent = str;
2158 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2159 szProduct = str;
2160 else
2161 return E_FAIL;
2163 /* skip to the next field */
2164 str = strchrW( str, ':' );
2165 if( !str )
2166 return E_FAIL;
2169 /* we have to have a component for an advertised shortcut */
2170 if( !szComponent )
2171 return E_FAIL;
2173 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2174 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2176 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2177 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2179 return S_OK;
2182 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2184 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2185 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2186 BOOL r;
2188 volume->type = GetDriveTypeW(drive);
2189 r = GetVolumeInformationW(drive, volume->label, label_sz,
2190 &volume->serial, NULL, NULL, NULL, 0);
2191 TRACE("r = %d type %d serial %08x name %s\n", r,
2192 volume->type, volume->serial, debugstr_w(volume->label));
2193 return r;
2196 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2198 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2199 WCHAR buffer[MAX_PATH];
2200 LPWSTR fname, unquoted = NULL;
2201 HRESULT hr = S_OK;
2202 UINT len;
2204 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2206 if (!pszFile) return E_INVALIDARG;
2208 /* quotes at the ends of the string are stripped */
2209 len = lstrlenW(pszFile);
2210 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2212 unquoted = strdupW(pszFile);
2213 PathUnquoteSpacesW(unquoted);
2214 pszFile = unquoted;
2217 /* any other quote marks are invalid */
2218 if (strchrW(pszFile, '"'))
2220 HeapFree(GetProcessHeap(), 0, unquoted);
2221 return S_FALSE;
2224 HeapFree(GetProcessHeap(), 0, This->sPath);
2225 This->sPath = NULL;
2227 HeapFree(GetProcessHeap(), 0, This->sComponent);
2228 This->sComponent = NULL;
2230 if (This->pPidl)
2231 ILFree(This->pPidl);
2232 This->pPidl = NULL;
2234 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2236 if (*pszFile == '\0')
2237 *buffer = '\0';
2238 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2239 return E_FAIL;
2240 else if(!PathFileExistsW(buffer) &&
2241 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2242 hr = S_FALSE;
2244 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2245 ShellLink_GetVolumeInfo(buffer, &This->volume);
2247 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2248 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2249 if (!This->sPath)
2251 HeapFree(GetProcessHeap(), 0, unquoted);
2252 return E_OUTOFMEMORY;
2255 lstrcpyW(This->sPath, buffer);
2257 This->bDirty = TRUE;
2258 HeapFree(GetProcessHeap(), 0, unquoted);
2260 return hr;
2263 /**************************************************************************
2264 * IShellLinkW Implementation
2267 static const IShellLinkWVtbl slvtw =
2269 IShellLinkW_fnQueryInterface,
2270 IShellLinkW_fnAddRef,
2271 IShellLinkW_fnRelease,
2272 IShellLinkW_fnGetPath,
2273 IShellLinkW_fnGetIDList,
2274 IShellLinkW_fnSetIDList,
2275 IShellLinkW_fnGetDescription,
2276 IShellLinkW_fnSetDescription,
2277 IShellLinkW_fnGetWorkingDirectory,
2278 IShellLinkW_fnSetWorkingDirectory,
2279 IShellLinkW_fnGetArguments,
2280 IShellLinkW_fnSetArguments,
2281 IShellLinkW_fnGetHotkey,
2282 IShellLinkW_fnSetHotkey,
2283 IShellLinkW_fnGetShowCmd,
2284 IShellLinkW_fnSetShowCmd,
2285 IShellLinkW_fnGetIconLocation,
2286 IShellLinkW_fnSetIconLocation,
2287 IShellLinkW_fnSetRelativePath,
2288 IShellLinkW_fnResolve,
2289 IShellLinkW_fnSetPath
2292 static HRESULT WINAPI
2293 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2295 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2296 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2299 static ULONG WINAPI
2300 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2302 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2303 return IShellLinkA_AddRef((IShellLinkA*)This);
2306 static ULONG WINAPI
2307 ShellLink_DataList_Release( IShellLinkDataList* iface )
2309 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2310 return ShellLink_Release( This );
2313 static HRESULT WINAPI
2314 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2316 FIXME("\n");
2317 return E_NOTIMPL;
2320 static HRESULT WINAPI
2321 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2323 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2324 LPVOID block = NULL;
2325 HRESULT r = E_FAIL;
2327 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2329 switch (dwSig)
2331 case EXP_DARWIN_ID_SIG:
2332 if (!This->sComponent)
2333 break;
2334 block = shelllink_build_darwinid( This->sComponent, dwSig );
2335 r = S_OK;
2336 break;
2337 case EXP_SZ_LINK_SIG:
2338 case NT_CONSOLE_PROPS_SIG:
2339 case NT_FE_CONSOLE_PROPS_SIG:
2340 case EXP_SPECIAL_FOLDER_SIG:
2341 case EXP_SZ_ICON_SIG:
2342 FIXME("valid but unhandled datablock %08x\n", dwSig);
2343 break;
2344 default:
2345 ERR("unknown datablock %08x\n", dwSig);
2347 *ppDataBlock = block;
2348 return r;
2351 static HRESULT WINAPI
2352 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2354 FIXME("\n");
2355 return E_NOTIMPL;
2358 static HRESULT WINAPI
2359 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2361 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2362 DWORD flags = 0;
2364 FIXME("%p %p\n", This, pdwFlags );
2366 /* FIXME: add more */
2367 if (This->sArgs)
2368 flags |= SLDF_HAS_ARGS;
2369 if (This->sComponent)
2370 flags |= SLDF_HAS_DARWINID;
2371 if (This->sIcoPath)
2372 flags |= SLDF_HAS_ICONLOCATION;
2373 if (This->sProduct)
2374 flags |= SLDF_HAS_LOGO3ID;
2375 if (This->pPidl)
2376 flags |= SLDF_HAS_ID_LIST;
2378 *pdwFlags = flags;
2380 return S_OK;
2383 static HRESULT WINAPI
2384 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2386 FIXME("\n");
2387 return E_NOTIMPL;
2390 static const IShellLinkDataListVtbl dlvt =
2392 ShellLink_DataList_QueryInterface,
2393 ShellLink_DataList_AddRef,
2394 ShellLink_DataList_Release,
2395 ShellLink_AddDataBlock,
2396 ShellLink_CopyDataBlock,
2397 ShellLink_RemoveDataBlock,
2398 ShellLink_GetFlags,
2399 ShellLink_SetFlags
2402 static HRESULT WINAPI
2403 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2405 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2406 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2409 static ULONG WINAPI
2410 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2412 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2413 return IShellLinkA_AddRef((IShellLinkA*)This);
2416 static ULONG WINAPI
2417 ShellLink_ExtInit_Release( IShellExtInit* iface )
2419 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2420 return ShellLink_Release( This );
2423 /**************************************************************************
2424 * ShellLink implementation of IShellExtInit::Initialize()
2426 * Loads the shelllink from the dataobject the shell is pointing to.
2428 static HRESULT WINAPI
2429 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2430 IDataObject *pdtobj, HKEY hkeyProgID )
2432 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2433 FORMATETC format;
2434 STGMEDIUM stgm;
2435 UINT count;
2436 HRESULT r = E_FAIL;
2438 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2440 if( !pdtobj )
2441 return r;
2443 format.cfFormat = CF_HDROP;
2444 format.ptd = NULL;
2445 format.dwAspect = DVASPECT_CONTENT;
2446 format.lindex = -1;
2447 format.tymed = TYMED_HGLOBAL;
2449 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2450 return r;
2452 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2453 if( count == 1 )
2455 LPWSTR path;
2457 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2458 count++;
2459 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2460 if( path )
2462 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2464 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2465 r = IPersistFile_Load( pf, path, 0 );
2466 HeapFree( GetProcessHeap(), 0, path );
2469 ReleaseStgMedium( &stgm );
2471 return r;
2474 static const IShellExtInitVtbl eivt =
2476 ShellLink_ExtInit_QueryInterface,
2477 ShellLink_ExtInit_AddRef,
2478 ShellLink_ExtInit_Release,
2479 ShellLink_ExtInit_Initialize
2482 static HRESULT WINAPI
2483 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2485 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2486 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2489 static ULONG WINAPI
2490 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2492 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2493 return IShellLinkA_AddRef((IShellLinkA*)This);
2496 static ULONG WINAPI
2497 ShellLink_ContextMenu_Release( IContextMenu* iface )
2499 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2500 return ShellLink_Release( This );
2503 static HRESULT WINAPI
2504 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2505 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2507 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2508 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2509 MENUITEMINFOW mii;
2510 int id = 1;
2512 TRACE("%p %p %u %u %u %u\n", This,
2513 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2515 if ( !hmenu )
2516 return E_INVALIDARG;
2518 memset( &mii, 0, sizeof mii );
2519 mii.cbSize = sizeof mii;
2520 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2521 mii.dwTypeData = szOpen;
2522 mii.cch = strlenW( mii.dwTypeData );
2523 mii.wID = idCmdFirst + id++;
2524 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2525 mii.fType = MFT_STRING;
2526 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2527 return E_FAIL;
2528 This->iIdOpen = 0;
2530 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2533 static LPWSTR
2534 shelllink_get_msi_component_path( LPWSTR component )
2536 LPWSTR path;
2537 DWORD r, sz = 0;
2539 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2540 if (r != ERROR_SUCCESS)
2541 return NULL;
2543 sz++;
2544 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2545 r = CommandLineFromMsiDescriptor( component, path, &sz );
2546 if (r != ERROR_SUCCESS)
2548 HeapFree( GetProcessHeap(), 0, path );
2549 path = NULL;
2552 TRACE("returning %s\n", debugstr_w( path ) );
2554 return path;
2557 static HRESULT WINAPI
2558 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2560 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2561 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2562 SHELLEXECUTEINFOW sei;
2563 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2564 LPWSTR args = NULL;
2565 LPWSTR path = NULL;
2566 HRESULT r;
2568 TRACE("%p %p\n", This, lpici );
2570 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2571 return E_INVALIDARG;
2573 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2575 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2576 return E_INVALIDARG;
2579 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2580 if ( FAILED( r ) )
2581 return r;
2583 if ( This->sComponent )
2585 path = shelllink_get_msi_component_path( This->sComponent );
2586 if (!path)
2587 return E_FAIL;
2589 else
2590 path = strdupW( This->sPath );
2592 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2593 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2595 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2596 DWORD len = 2;
2598 if ( This->sArgs )
2599 len += lstrlenW( This->sArgs );
2600 if ( iciex->lpParametersW )
2601 len += lstrlenW( iciex->lpParametersW );
2603 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2604 args[0] = 0;
2605 if ( This->sArgs )
2606 lstrcatW( args, This->sArgs );
2607 if ( iciex->lpParametersW )
2609 static const WCHAR space[] = { ' ', 0 };
2610 lstrcatW( args, space );
2611 lstrcatW( args, iciex->lpParametersW );
2615 memset( &sei, 0, sizeof sei );
2616 sei.cbSize = sizeof sei;
2617 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2618 sei.lpFile = path;
2619 sei.nShow = This->iShowCmd;
2620 sei.lpIDList = This->pPidl;
2621 sei.lpDirectory = This->sWorkDir;
2622 sei.lpParameters = args;
2623 sei.lpVerb = szOpen;
2625 if( ShellExecuteExW( &sei ) )
2626 r = S_OK;
2627 else
2628 r = E_FAIL;
2630 HeapFree( GetProcessHeap(), 0, args );
2631 HeapFree( GetProcessHeap(), 0, path );
2633 return r;
2636 static HRESULT WINAPI
2637 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2638 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2640 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2642 FIXME("%p %lu %u %p %p %u\n", This,
2643 idCmd, uType, pwReserved, pszName, cchMax );
2645 return E_NOTIMPL;
2648 static const IContextMenuVtbl cmvt =
2650 ShellLink_ContextMenu_QueryInterface,
2651 ShellLink_ContextMenu_AddRef,
2652 ShellLink_ContextMenu_Release,
2653 ShellLink_QueryContextMenu,
2654 ShellLink_InvokeCommand,
2655 ShellLink_GetCommandString
2658 static HRESULT WINAPI
2659 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2661 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2662 return ShellLink_QueryInterface( This, riid, ppvObject );
2665 static ULONG WINAPI
2666 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2668 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2669 return ShellLink_AddRef( This );
2672 static ULONG WINAPI
2673 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2675 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2676 return ShellLink_Release( This );
2679 static HRESULT WINAPI
2680 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2682 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2684 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2686 if ( !This->site )
2687 return E_FAIL;
2688 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2691 static HRESULT WINAPI
2692 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2694 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2696 TRACE("%p %p\n", iface, punk);
2698 if ( punk )
2699 IUnknown_AddRef( punk );
2700 This->site = punk;
2702 return S_OK;
2705 static const IObjectWithSiteVtbl owsvt =
2707 ShellLink_ObjectWithSite_QueryInterface,
2708 ShellLink_ObjectWithSite_AddRef,
2709 ShellLink_ObjectWithSite_Release,
2710 ShellLink_SetSite,
2711 ShellLink_GetSite,