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
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:
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.
36 #define NONAMELESSUNION
38 #include "wine/debug.h"
48 #include "undocshell.h"
51 #include "shell32_main.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 */
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
97 DWORD dwNetworkVolTableOfs;
101 typedef struct _LOCAL_VOLUME_INFO
109 typedef struct volume_info_t
113 WCHAR label[12]; /* assume 8.3 */
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 */
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;
142 /* data structures according to the information in the link */
162 INT iIdOpen; /* id of the "Open" entry in the context menu */
165 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
168 static inline IShellLinkImpl *impl_from_IShellLinkW( IShellLinkW *iface )
170 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
173 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
175 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
178 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
180 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
183 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
185 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
188 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
190 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
193 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
195 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
198 static inline IShellLinkImpl *impl_from_IObjectWithSite( IObjectWithSite *iface )
200 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblObjectWithSite));
203 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
205 /* strdup on the process heap */
206 static inline LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
208 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
209 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
212 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
216 static inline LPWSTR strdupW( LPCWSTR src )
219 if (!src) return NULL;
220 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
226 /**************************************************************************
227 * ShellLink::QueryInterface implementation
229 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
231 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
235 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
239 else if(IsEqualIID(riid, &IID_IShellLinkW))
241 *ppvObj = &(This->lpvtblw);
243 else if(IsEqualIID(riid, &IID_IPersistFile))
245 *ppvObj = &(This->lpvtblPersistFile);
247 else if(IsEqualIID(riid, &IID_IPersistStream))
249 *ppvObj = &(This->lpvtblPersistStream);
251 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
253 *ppvObj = &(This->lpvtblShellLinkDataList);
255 else if(IsEqualIID(riid, &IID_IShellExtInit))
257 *ppvObj = &(This->lpvtblShellExtInit);
259 else if(IsEqualIID(riid, &IID_IContextMenu))
261 *ppvObj = &(This->lpvtblContextMenu);
263 else if(IsEqualIID(riid, &IID_IObjectWithSite))
265 *ppvObj = &(This->lpvtblObjectWithSite);
270 IUnknown_AddRef((IUnknown*)(*ppvObj));
271 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
274 ERR("-- Interface: E_NOINTERFACE\n");
275 return E_NOINTERFACE;
278 /**************************************************************************
279 * ShellLink::AddRef implementation
281 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
283 ULONG refCount = InterlockedIncrement(&This->ref);
285 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
290 /**************************************************************************
291 * ShellLink::Release implementation
293 static ULONG ShellLink_Release( IShellLinkImpl *This )
295 ULONG refCount = InterlockedDecrement(&This->ref);
297 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
302 TRACE("-- destroying IShellLink(%p)\n",This);
304 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
305 HeapFree(GetProcessHeap(), 0, This->sArgs);
306 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
307 HeapFree(GetProcessHeap(), 0, This->sDescription);
308 HeapFree(GetProcessHeap(), 0, This->sPath);
309 HeapFree(GetProcessHeap(), 0, This->sPathRel);
310 HeapFree(GetProcessHeap(), 0, This->sProduct);
311 HeapFree(GetProcessHeap(), 0, This->sComponent);
312 HeapFree(GetProcessHeap(), 0, This->filepath);
315 IUnknown_Release( This->site );
325 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
327 TRACE("%p %p\n", This, pclsid);
329 *pclsid = CLSID_ShellLink;
333 /**************************************************************************
334 * IPersistFile_QueryInterface
336 static HRESULT WINAPI IPersistFile_fnQueryInterface(
341 IShellLinkImpl *This = impl_from_IPersistFile(iface);
342 return ShellLink_QueryInterface( This, riid, ppvObj );
345 /******************************************************************************
346 * IPersistFile_AddRef
348 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
350 IShellLinkImpl *This = impl_from_IPersistFile(iface);
351 return ShellLink_AddRef( This );
354 /******************************************************************************
355 * IPersistFile_Release
357 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
359 IShellLinkImpl *This = impl_from_IPersistFile(iface);
360 return IShellLinkA_Release((IShellLinkA*)This);
363 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
365 IShellLinkImpl *This = impl_from_IPersistFile(iface);
366 return ShellLink_GetClassID( This, pClassID );
369 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
371 IShellLinkImpl *This = impl_from_IPersistFile(iface);
373 TRACE("(%p)\n",This);
381 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
383 IShellLinkImpl *This = impl_from_IPersistFile(iface);
384 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
388 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
391 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
392 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
395 r = IPersistStream_Load(StreamThis, stm);
396 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
397 IStream_Release( stm );
399 /* update file path */
400 HeapFree(GetProcessHeap(), 0, This->filepath);
401 This->filepath = strdupW(pszFileName);
403 This->bDirty = FALSE;
405 TRACE("-- returning hr %08x\n", r);
409 BOOL run_winemenubuilder( const WCHAR *args )
411 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
415 PROCESS_INFORMATION pi;
420 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
421 strcatW( app, menubuilder );
423 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
424 buffer = HeapAlloc( GetProcessHeap(), 0, len );
428 strcpyW( buffer, app );
429 strcatW( buffer, args );
431 TRACE("starting %s\n",debugstr_w(buffer));
433 memset(&si, 0, sizeof(si));
436 Wow64DisableWow64FsRedirection( &redir );
437 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
438 Wow64RevertWow64FsRedirection( redir );
440 HeapFree( GetProcessHeap(), 0, buffer );
444 CloseHandle( pi.hProcess );
445 CloseHandle( pi.hThread );
451 static BOOL StartLinkProcessor( LPCOLESTR szLink )
453 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
458 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
459 buffer = HeapAlloc( GetProcessHeap(), 0, len );
463 wsprintfW( buffer, szFormat, szLink );
464 ret = run_winemenubuilder( buffer );
465 HeapFree( GetProcessHeap(), 0, buffer );
469 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
471 IShellLinkImpl *This = impl_from_IPersistFile(iface);
472 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
476 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
481 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
484 r = IPersistStream_Save(StreamThis, stm, FALSE);
485 IStream_Release( stm );
489 StartLinkProcessor( pszFileName );
491 /* update file path */
492 HeapFree(GetProcessHeap(), 0, This->filepath);
493 This->filepath = strdupW(pszFileName);
495 This->bDirty = FALSE;
499 DeleteFileW( pszFileName );
500 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
507 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
509 IShellLinkImpl *This = impl_from_IPersistFile(iface);
510 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
514 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
516 IShellLinkImpl *This = impl_from_IPersistFile(iface);
519 TRACE("(%p)->(%p)\n", This, filename);
527 SHGetMalloc(&pMalloc);
528 *filename = IMalloc_Alloc(pMalloc, (strlenW(This->filepath)+1)*sizeof(WCHAR));
529 if (!*filename) return E_OUTOFMEMORY;
531 strcpyW(*filename, This->filepath);
536 static const IPersistFileVtbl pfvt =
538 IPersistFile_fnQueryInterface,
539 IPersistFile_fnAddRef,
540 IPersistFile_fnRelease,
541 IPersistFile_fnGetClassID,
542 IPersistFile_fnIsDirty,
545 IPersistFile_fnSaveCompleted,
546 IPersistFile_fnGetCurFile
549 /************************************************************************
550 * IPersistStream_QueryInterface
552 static HRESULT WINAPI IPersistStream_fnQueryInterface(
553 IPersistStream* iface,
557 IShellLinkImpl *This = impl_from_IPersistStream(iface);
558 return ShellLink_QueryInterface( This, riid, ppvObj );
561 /************************************************************************
562 * IPersistStream_Release
564 static ULONG WINAPI IPersistStream_fnRelease(
565 IPersistStream* iface)
567 IShellLinkImpl *This = impl_from_IPersistStream(iface);
568 return IShellLinkA_Release((IShellLinkA*)This);
571 /************************************************************************
572 * IPersistStream_AddRef
574 static ULONG WINAPI IPersistStream_fnAddRef(
575 IPersistStream* iface)
577 IShellLinkImpl *This = impl_from_IPersistStream(iface);
578 return ShellLink_AddRef( This );
581 /************************************************************************
582 * IPersistStream_GetClassID
585 static HRESULT WINAPI IPersistStream_fnGetClassID(
586 IPersistStream* iface,
589 IShellLinkImpl *This = impl_from_IPersistStream(iface);
590 return ShellLink_GetClassID( This, pClassID );
593 /************************************************************************
594 * IPersistStream_IsDirty (IPersistStream)
596 static HRESULT WINAPI IPersistStream_fnIsDirty(
597 IPersistStream* iface)
599 IShellLinkImpl *This = impl_from_IPersistStream(iface);
601 TRACE("(%p)\n", This);
607 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
618 r = IStream_Read(stm, &len, sizeof(len), &count);
619 if ( FAILED (r) || ( count != sizeof(len) ) )
623 len *= sizeof (WCHAR);
625 TRACE("reading %d\n", len);
626 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
628 return E_OUTOFMEMORY;
630 r = IStream_Read(stm, temp, len, &count);
631 if( FAILED (r) || ( count != len ) )
633 HeapFree( GetProcessHeap(), 0, temp );
637 TRACE("read %s\n", debugstr_an(temp,len));
639 /* convert to unicode if necessary */
642 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
643 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
646 HeapFree( GetProcessHeap(), 0, temp );
647 return E_OUTOFMEMORY;
649 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
650 HeapFree( GetProcessHeap(), 0, temp );
664 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
671 unsigned char data[1];
676 r = IStream_Read( stm, &size, sizeof(size), &count );
677 if( FAILED( r ) || count != sizeof(size) )
680 chunk = HeapAlloc( GetProcessHeap(), 0, size );
682 return E_OUTOFMEMORY;
685 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
686 if( FAILED( r ) || count != (size - sizeof(size)) )
688 HeapFree( GetProcessHeap(), 0, chunk );
692 TRACE("Read %d bytes\n",chunk->size);
699 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
701 const int label_sz = sizeof volume->label/sizeof volume->label[0];
705 volume->serial = vol->dwVolSerial;
706 volume->type = vol->dwType;
708 if( !vol->dwVolLabelOfs )
710 if( vol->dwSize <= vol->dwVolLabelOfs )
712 len = vol->dwSize - vol->dwVolLabelOfs;
715 label += vol->dwVolLabelOfs;
716 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
721 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
726 while( p[len] && (len < maxlen) )
729 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
730 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
731 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
737 static HRESULT Stream_LoadLocation( IStream *stm,
738 volume_info *volume, LPWSTR *path )
745 r = Stream_ReadChunk( stm, (LPVOID*) &p );
749 loc = (LOCATION_INFO*) p;
750 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
752 HeapFree( GetProcessHeap(), 0, p );
756 /* if there's valid local volume information, load it */
757 if( loc->dwVolTableOfs &&
758 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
760 LOCAL_VOLUME_INFO *volume_info;
762 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
763 Stream_LoadVolume( volume_info, volume );
766 /* if there's a local path, load it */
767 n = loc->dwLocalPathOfs;
768 if( n && (n < loc->dwTotalSize) )
769 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
771 TRACE("type %d serial %08x name %s path %s\n", volume->type,
772 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
774 HeapFree( GetProcessHeap(), 0, p );
779 * The format of the advertised shortcut info seems to be:
784 * 0 Length of the block (4 bytes, usually 0x314)
786 * 8 string data in ASCII
787 * 8+0x104 string data in UNICODE
789 * In the original Win32 implementation the buffers are not initialized
790 * to zero, so data trailing the string is random garbage.
792 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
797 EXP_DARWIN_LINK buffer;
801 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
805 /* make sure that we read the size of the structure even on error */
806 size = sizeof buffer - sizeof (DWORD);
807 if( buffer.dbh.cbSize != sizeof buffer )
809 ERR("Ooops. This structure is not as expected...\n");
813 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
820 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
822 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
824 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
828 *str = HeapAlloc( GetProcessHeap(), 0,
829 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
830 lstrcpyW( *str, buffer.szwDarwinID );
835 /************************************************************************
836 * IPersistStream_Load (IPersistStream)
838 static HRESULT WINAPI IPersistStream_fnLoad(
839 IPersistStream* iface,
848 IShellLinkImpl *This = impl_from_IPersistStream(iface);
850 TRACE("%p %p\n", This, stm);
853 return STG_E_INVALIDPOINTER;
856 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
860 if( dwBytesRead != sizeof(hdr))
862 if( hdr.dwSize != sizeof(hdr))
864 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
867 /* free all the old stuff */
870 memset( &This->volume, 0, sizeof This->volume );
871 HeapFree(GetProcessHeap(), 0, This->sPath);
873 HeapFree(GetProcessHeap(), 0, This->sDescription);
874 This->sDescription = NULL;
875 HeapFree(GetProcessHeap(), 0, This->sPathRel);
876 This->sPathRel = NULL;
877 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
878 This->sWorkDir = NULL;
879 HeapFree(GetProcessHeap(), 0, This->sArgs);
881 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
882 This->sIcoPath = NULL;
883 HeapFree(GetProcessHeap(), 0, This->sProduct);
884 This->sProduct = NULL;
885 HeapFree(GetProcessHeap(), 0, This->sComponent);
886 This->sComponent = NULL;
888 This->wHotKey = (WORD)hdr.wHotKey;
889 This->iIcoNdx = hdr.nIcon;
890 FileTimeToSystemTime (&hdr.Time1, &This->time1);
891 FileTimeToSystemTime (&hdr.Time2, &This->time2);
892 FileTimeToSystemTime (&hdr.Time3, &This->time3);
895 WCHAR sTemp[MAX_PATH];
896 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
897 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
898 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
899 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
900 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
901 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
902 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
903 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
904 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
907 /* load all the new stuff */
908 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
910 r = ILLoadFromStream( stm, &This->pPidl );
916 /* load the location information */
917 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
918 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
922 unicode = hdr.dwFlags & SLDF_UNICODE;
923 if( hdr.dwFlags & SLDF_HAS_NAME )
925 r = Stream_LoadString( stm, unicode, &This->sDescription );
926 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
931 if( hdr.dwFlags & SLDF_HAS_RELPATH )
933 r = Stream_LoadString( stm, unicode, &This->sPathRel );
934 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
939 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
941 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
942 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
947 if( hdr.dwFlags & SLDF_HAS_ARGS )
949 r = Stream_LoadString( stm, unicode, &This->sArgs );
950 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
955 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
957 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
958 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
963 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
965 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
966 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
971 if( hdr.dwFlags & SLDF_HAS_DARWINID )
973 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
974 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
979 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
980 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
982 /* Some lnk files have extra data blocks starting with a
983 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
984 * one with a 0xa0000003 signature. However these don't seem to matter
987 WARN("Last word was not zero\n");
999 /************************************************************************
1000 * Stream_WriteString
1002 * Helper function for IPersistStream_Save. Writes a unicode string
1003 * with terminating nul byte to a stream, preceded by the its length.
1005 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
1007 USHORT len = lstrlenW( str ) + 1;
1011 r = IStream_Write( stm, &len, sizeof(len), &count );
1015 len *= sizeof(WCHAR);
1017 r = IStream_Write( stm, str, len, &count );
1024 /************************************************************************
1025 * Stream_WriteLocationInfo
1027 * Writes the location info to a stream
1029 * FIXME: One day we might want to write the network volume information
1030 * and the final path.
1031 * Figure out how Windows deals with unicode paths here.
1033 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
1034 volume_info *volume )
1036 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
1037 LOCAL_VOLUME_INFO *vol;
1039 LPSTR szLabel, szPath, szFinalPath;
1043 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
1045 /* figure out the size of everything */
1046 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1047 NULL, 0, NULL, NULL );
1048 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
1049 NULL, 0, NULL, NULL );
1050 volume_info_size = sizeof *vol + label_size;
1051 final_path_size = 1;
1052 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
1054 /* create pointers to everything */
1055 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
1056 vol = (LOCAL_VOLUME_INFO*) &loc[1];
1057 szLabel = (LPSTR) &vol[1];
1058 szPath = &szLabel[label_size];
1059 szFinalPath = &szPath[path_size];
1061 /* fill in the location information header */
1062 loc->dwTotalSize = total_size;
1063 loc->dwHeaderSize = sizeof (*loc);
1065 loc->dwVolTableOfs = sizeof (*loc);
1066 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1067 loc->dwNetworkVolTableOfs = 0;
1068 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1070 /* fill in the volume information */
1071 vol->dwSize = volume_info_size;
1072 vol->dwType = volume->type;
1073 vol->dwVolSerial = volume->serial;
1074 vol->dwVolLabelOfs = sizeof (*vol);
1076 /* copy in the strings */
1077 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1078 szLabel, label_size, NULL, NULL );
1079 WideCharToMultiByte( CP_ACP, 0, path, -1,
1080 szPath, path_size, NULL, NULL );
1083 hr = IStream_Write( stm, loc, total_size, &count );
1084 HeapFree(GetProcessHeap(), 0, loc);
1089 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1091 EXP_DARWIN_LINK *buffer;
1093 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1094 buffer->dbh.cbSize = sizeof *buffer;
1095 buffer->dbh.dwSignature = magic;
1096 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1097 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1102 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1104 EXP_DARWIN_LINK *buffer;
1109 buffer = shelllink_build_darwinid( string, magic );
1111 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1114 /************************************************************************
1115 * IPersistStream_Save (IPersistStream)
1117 * FIXME: makes assumptions about byte order
1119 static HRESULT WINAPI IPersistStream_fnSave(
1120 IPersistStream* iface,
1129 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1131 TRACE("%p %p %x\n", This, stm, fClearDirty);
1133 memset(&header, 0, sizeof(header));
1134 header.dwSize = sizeof(header);
1135 header.fStartup = This->iShowCmd;
1136 header.MagicGuid = CLSID_ShellLink;
1138 header.wHotKey = This->wHotKey;
1139 header.nIcon = This->iIcoNdx;
1140 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1142 header.dwFlags |= SLDF_HAS_ID_LIST;
1144 header.dwFlags |= SLDF_HAS_LINK_INFO;
1145 if( This->sDescription )
1146 header.dwFlags |= SLDF_HAS_NAME;
1147 if( This->sWorkDir )
1148 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1150 header.dwFlags |= SLDF_HAS_ARGS;
1151 if( This->sIcoPath )
1152 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1153 if( This->sProduct )
1154 header.dwFlags |= SLDF_HAS_LOGO3ID;
1155 if( This->sComponent )
1156 header.dwFlags |= SLDF_HAS_DARWINID;
1158 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1159 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1160 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1162 /* write the Shortcut header */
1163 r = IStream_Write( stm, &header, sizeof(header), &count );
1166 ERR("Write failed at %d\n",__LINE__);
1170 TRACE("Writing pidl\n");
1172 /* write the PIDL to the shortcut */
1175 r = ILSaveToStream( stm, This->pPidl );
1178 ERR("Failed to write PIDL at %d\n",__LINE__);
1184 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1186 if( This->sDescription )
1187 r = Stream_WriteString( stm, This->sDescription );
1189 if( This->sPathRel )
1190 r = Stream_WriteString( stm, This->sPathRel );
1192 if( This->sWorkDir )
1193 r = Stream_WriteString( stm, This->sWorkDir );
1196 r = Stream_WriteString( stm, This->sArgs );
1198 if( This->sIcoPath )
1199 r = Stream_WriteString( stm, This->sIcoPath );
1201 if( This->sProduct )
1202 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1204 if( This->sComponent )
1205 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1207 /* the last field is a single zero dword */
1209 r = IStream_Write( stm, &zero, sizeof zero, &count );
1214 /************************************************************************
1215 * IPersistStream_GetSizeMax (IPersistStream)
1217 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1218 IPersistStream* iface,
1219 ULARGE_INTEGER* pcbSize)
1221 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1223 TRACE("(%p)\n", This);
1228 static const IPersistStreamVtbl psvt =
1230 IPersistStream_fnQueryInterface,
1231 IPersistStream_fnAddRef,
1232 IPersistStream_fnRelease,
1233 IPersistStream_fnGetClassID,
1234 IPersistStream_fnIsDirty,
1235 IPersistStream_fnLoad,
1236 IPersistStream_fnSave,
1237 IPersistStream_fnGetSizeMax
1240 /**************************************************************************
1241 * IShellLink_Constructor
1243 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1244 REFIID riid, LPVOID *ppv )
1246 IShellLinkImpl * sl;
1249 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1254 return CLASS_E_NOAGGREGATION;
1255 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1257 return E_OUTOFMEMORY;
1261 sl->lpvtblw = &slvtw;
1262 sl->lpvtblPersistFile = &pfvt;
1263 sl->lpvtblPersistStream = &psvt;
1264 sl->lpvtblShellLinkDataList = &dlvt;
1265 sl->lpvtblShellExtInit = &eivt;
1266 sl->lpvtblContextMenu = &cmvt;
1267 sl->lpvtblObjectWithSite = &owsvt;
1268 sl->iShowCmd = SW_SHOWNORMAL;
1272 sl->filepath = NULL;
1274 TRACE("(%p)->()\n",sl);
1276 r = ShellLink_QueryInterface( sl, riid, ppv );
1277 ShellLink_Release( sl );
1282 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1284 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1289 /**************************************************************************
1290 * ShellLink_UpdatePath
1291 * update absolute path in sPath using relative path in sPathRel
1293 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1295 if (!path || !psPath)
1296 return E_INVALIDARG;
1298 if (!*psPath && sPathRel) {
1299 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1300 LPWSTR final = NULL;
1302 /* first try if [directory of link file] + [relative path] finds an existing file */
1304 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1307 lstrcpyW(final, sPathRel);
1311 if (SHELL_ExistsFileW(buffer)) {
1312 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1313 lstrcpyW(abs_path, buffer);
1315 /* try if [working directory] + [relative path] finds an existing file */
1317 lstrcpyW(buffer, sWorkDir);
1318 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1320 if (SHELL_ExistsFileW(buffer))
1321 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1322 lstrcpyW(abs_path, buffer);
1326 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1328 lstrcpyW(abs_path, sPathRel);
1330 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1332 return E_OUTOFMEMORY;
1334 lstrcpyW(*psPath, abs_path);
1340 /**************************************************************************
1341 * IShellLink_ConstructFromFile
1343 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1344 LPCITEMIDLIST pidl, LPVOID* ppv)
1348 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1350 if (SUCCEEDED(hr)) {
1355 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1357 if (SUCCEEDED(hr)) {
1358 WCHAR path[MAX_PATH];
1360 if (SHGetPathFromIDListW(pidl, path))
1361 hr = IPersistFile_Load(ppf, path, 0);
1368 IPersistFile_Release(ppf);
1372 IShellLinkW_Release(psl);
1378 /**************************************************************************
1379 * IShellLinkA_QueryInterface
1381 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1383 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1384 return ShellLink_QueryInterface( This, riid, ppvObj );
1387 /******************************************************************************
1388 * IShellLinkA_AddRef
1390 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1392 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1393 return ShellLink_AddRef( This );
1396 /******************************************************************************
1397 * IShellLinkA_Release
1399 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1401 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1402 return ShellLink_Release( This );
1405 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1406 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1408 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1410 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1411 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1413 if (This->sComponent || This->sProduct)
1419 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1420 pszFile, cchMaxPath, NULL, NULL);
1422 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1427 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1429 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1431 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1433 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1436 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1438 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1440 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1443 ILFree(This->pPidl);
1444 This->pPidl = ILClone (pidl);
1445 This->bDirty = TRUE;
1450 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1452 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1454 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1458 if( This->sDescription )
1459 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1460 pszName, cchMaxName, NULL, NULL);
1465 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1467 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1469 TRACE("(%p)->(pName=%s)\n", This, pszName);
1471 HeapFree(GetProcessHeap(), 0, This->sDescription);
1472 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1473 if ( !This->sDescription )
1474 return E_OUTOFMEMORY;
1476 This->bDirty = TRUE;
1481 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1483 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1485 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1489 if( This->sWorkDir )
1490 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1491 pszDir, cchMaxPath, NULL, NULL);
1496 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1498 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1500 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1502 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1503 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1504 if ( !This->sWorkDir )
1505 return E_OUTOFMEMORY;
1507 This->bDirty = TRUE;
1512 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1514 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1516 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1521 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1522 pszArgs, cchMaxPath, NULL, NULL);
1527 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1529 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1531 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1533 HeapFree(GetProcessHeap(), 0, This->sArgs);
1536 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1538 return E_OUTOFMEMORY;
1540 else This->sArgs = NULL;
1542 This->bDirty = TRUE;
1547 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1549 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1551 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1553 *pwHotkey = This->wHotKey;
1558 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1560 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1562 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1564 This->wHotKey = wHotkey;
1565 This->bDirty = TRUE;
1570 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1572 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1574 TRACE("(%p)->(%p)\n",This, piShowCmd);
1575 *piShowCmd = This->iShowCmd;
1579 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1581 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1583 TRACE("(%p) %d\n",This, iShowCmd);
1585 This->iShowCmd = iShowCmd;
1586 This->bDirty = TRUE;
1591 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPCITEMIDLIST pidl,
1592 LPSTR pszIconPath, int cchIconPath, int* piIcon)
1594 LPCITEMIDLIST pidlLast;
1596 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1598 if (SUCCEEDED(hr)) {
1601 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1603 if (SUCCEEDED(hr)) {
1604 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1606 IExtractIconA_Release(pei);
1609 IShellFolder_Release(psf);
1615 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1617 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1619 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1622 *piIcon = This->iIcoNdx;
1626 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1630 if (This->pPidl || This->sPath)
1634 HRESULT hr = SHGetDesktopFolder(&pdsk);
1638 /* first look for an icon using the PIDL (if present) */
1640 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1644 /* if we couldn't find an icon yet, look for it using the file system path */
1645 if (FAILED(hr) && This->sPath)
1649 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1651 if (SUCCEEDED(hr)) {
1652 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1658 IShellFolder_Release(pdsk);
1666 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1668 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1670 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1672 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1673 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1674 if ( !This->sIcoPath )
1675 return E_OUTOFMEMORY;
1677 This->iIcoNdx = iIcon;
1678 This->bDirty = TRUE;
1683 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1685 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1687 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1689 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1690 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1691 This->bDirty = TRUE;
1693 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1696 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1698 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1700 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1702 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1705 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1709 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1711 TRACE("(%p)->(path=%s)\n",This, pszFile);
1713 if (!pszFile) return E_INVALIDARG;
1715 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1717 return E_OUTOFMEMORY;
1719 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1720 HeapFree( GetProcessHeap(), 0, str );
1725 /**************************************************************************
1726 * IShellLink Implementation
1729 static const IShellLinkAVtbl slvt =
1731 IShellLinkA_fnQueryInterface,
1732 IShellLinkA_fnAddRef,
1733 IShellLinkA_fnRelease,
1734 IShellLinkA_fnGetPath,
1735 IShellLinkA_fnGetIDList,
1736 IShellLinkA_fnSetIDList,
1737 IShellLinkA_fnGetDescription,
1738 IShellLinkA_fnSetDescription,
1739 IShellLinkA_fnGetWorkingDirectory,
1740 IShellLinkA_fnSetWorkingDirectory,
1741 IShellLinkA_fnGetArguments,
1742 IShellLinkA_fnSetArguments,
1743 IShellLinkA_fnGetHotkey,
1744 IShellLinkA_fnSetHotkey,
1745 IShellLinkA_fnGetShowCmd,
1746 IShellLinkA_fnSetShowCmd,
1747 IShellLinkA_fnGetIconLocation,
1748 IShellLinkA_fnSetIconLocation,
1749 IShellLinkA_fnSetRelativePath,
1750 IShellLinkA_fnResolve,
1751 IShellLinkA_fnSetPath
1755 /**************************************************************************
1756 * IShellLinkW_fnQueryInterface
1758 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1759 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1761 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1762 return ShellLink_QueryInterface( This, riid, ppvObj );
1765 /******************************************************************************
1766 * IShellLinkW_fnAddRef
1768 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1770 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1771 return ShellLink_AddRef( This );
1774 /******************************************************************************
1775 * IShellLinkW_fnRelease
1777 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1779 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1780 return ShellLink_Release( This );
1783 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1785 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1787 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1788 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1790 if (This->sComponent || This->sProduct)
1796 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1798 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1803 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1805 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1807 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1814 *ppidl = ILClone(This->pPidl);
1818 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1820 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1822 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1825 ILFree( This->pPidl );
1826 This->pPidl = ILClone( pidl );
1830 This->bDirty = TRUE;
1835 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1837 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1839 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1842 if( This->sDescription )
1843 lstrcpynW( pszName, This->sDescription, cchMaxName );
1848 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1850 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1852 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1854 HeapFree(GetProcessHeap(), 0, This->sDescription);
1855 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1856 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1857 if ( !This->sDescription )
1858 return E_OUTOFMEMORY;
1860 lstrcpyW( This->sDescription, pszName );
1861 This->bDirty = TRUE;
1866 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1868 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1870 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1874 if( This->sWorkDir )
1875 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1880 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1882 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1884 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1886 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1887 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1888 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1889 if ( !This->sWorkDir )
1890 return E_OUTOFMEMORY;
1891 lstrcpyW( This->sWorkDir, pszDir );
1892 This->bDirty = TRUE;
1897 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1899 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1901 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1906 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1911 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1913 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1915 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1917 HeapFree(GetProcessHeap(), 0, This->sArgs);
1920 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1921 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1923 return E_OUTOFMEMORY;
1924 lstrcpyW( This->sArgs, pszArgs );
1926 else This->sArgs = NULL;
1928 This->bDirty = TRUE;
1933 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1935 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1937 TRACE("(%p)->(%p)\n",This, pwHotkey);
1939 *pwHotkey=This->wHotKey;
1944 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1946 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1948 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1950 This->wHotKey = wHotkey;
1951 This->bDirty = TRUE;
1956 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1958 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1960 TRACE("(%p)->(%p)\n",This, piShowCmd);
1962 *piShowCmd = This->iShowCmd;
1967 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1969 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1971 This->iShowCmd = iShowCmd;
1972 This->bDirty = TRUE;
1977 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPCITEMIDLIST pidl,
1978 LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1980 LPCITEMIDLIST pidlLast;
1982 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1984 if (SUCCEEDED(hr)) {
1987 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, &pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1989 if (SUCCEEDED(hr)) {
1990 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1992 IExtractIconW_Release(pei);
1995 IShellFolder_Release(psf);
2001 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
2003 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2005 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
2008 *piIcon = This->iIcoNdx;
2012 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
2016 if (This->pPidl || This->sPath)
2020 HRESULT hr = SHGetDesktopFolder(&pdsk);
2024 /* first look for an icon using the PIDL (if present) */
2026 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
2030 /* if we couldn't find an icon yet, look for it using the file system path */
2031 if (FAILED(hr) && This->sPath)
2035 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
2039 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
2045 IShellFolder_Release(pdsk);
2052 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
2054 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2056 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
2058 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
2059 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
2060 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
2061 if ( !This->sIcoPath )
2062 return E_OUTOFMEMORY;
2063 lstrcpyW( This->sIcoPath, pszIconPath );
2065 This->iIcoNdx = iIcon;
2066 This->bDirty = TRUE;
2071 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2073 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2075 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2077 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2078 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2079 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2080 if ( !This->sPathRel )
2081 return E_OUTOFMEMORY;
2082 lstrcpyW( This->sPathRel, pszPathRel );
2083 This->bDirty = TRUE;
2085 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2088 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2093 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2095 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2097 /*FIXME: use IResolveShellLink interface */
2099 if (!This->sPath && This->pPidl) {
2100 WCHAR buffer[MAX_PATH];
2102 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2104 if (bSuccess && *buffer) {
2105 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2107 return E_OUTOFMEMORY;
2109 lstrcpyW(This->sPath, buffer);
2111 This->bDirty = TRUE;
2113 hr = S_OK; /* don't report an error occurred while just caching information */
2116 if (!This->sIcoPath && This->sPath) {
2117 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2118 if (!This->sIcoPath)
2119 return E_OUTOFMEMORY;
2121 lstrcpyW(This->sIcoPath, This->sPath);
2124 This->bDirty = TRUE;
2130 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2139 p = strchrW( str, ':' );
2143 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2146 memcpy( ret, str, sizeof(WCHAR)*len );
2151 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2153 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2161 /* each segment must start with two colons */
2162 if( str[0] != ':' || str[1] != ':' )
2165 /* the last segment is just two colons */
2170 /* there must be a colon straight after a guid */
2171 p = strchrW( str, ':' );
2178 /* get the guid, and check it's validly formatted */
2179 memcpy( szGuid, str, sizeof(WCHAR)*len );
2181 r = CLSIDFromString( szGuid, &guid );
2186 /* match it up to a guid that we care about */
2187 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2189 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2194 /* skip to the next field */
2195 str = strchrW( str, ':' );
2200 /* we have to have a component for an advertised shortcut */
2204 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2205 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2207 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2208 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2213 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2215 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2216 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2219 volume->type = GetDriveTypeW(drive);
2220 r = GetVolumeInformationW(drive, volume->label, label_sz,
2221 &volume->serial, NULL, NULL, NULL, 0);
2222 TRACE("r = %d type %d serial %08x name %s\n", r,
2223 volume->type, volume->serial, debugstr_w(volume->label));
2227 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2229 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2230 WCHAR buffer[MAX_PATH];
2231 LPWSTR fname, unquoted = NULL;
2235 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2237 if (!pszFile) return E_INVALIDARG;
2239 /* quotes at the ends of the string are stripped */
2240 len = lstrlenW(pszFile);
2241 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2243 unquoted = strdupW(pszFile);
2244 PathUnquoteSpacesW(unquoted);
2248 /* any other quote marks are invalid */
2249 if (strchrW(pszFile, '"'))
2251 HeapFree(GetProcessHeap(), 0, unquoted);
2255 HeapFree(GetProcessHeap(), 0, This->sPath);
2258 HeapFree(GetProcessHeap(), 0, This->sComponent);
2259 This->sComponent = NULL;
2262 ILFree(This->pPidl);
2265 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2267 if (*pszFile == '\0')
2269 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2271 else if(!PathFileExistsW(buffer) &&
2272 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2275 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2276 ShellLink_GetVolumeInfo(buffer, &This->volume);
2278 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2279 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2282 HeapFree(GetProcessHeap(), 0, unquoted);
2283 return E_OUTOFMEMORY;
2286 lstrcpyW(This->sPath, buffer);
2288 This->bDirty = TRUE;
2289 HeapFree(GetProcessHeap(), 0, unquoted);
2294 /**************************************************************************
2295 * IShellLinkW Implementation
2298 static const IShellLinkWVtbl slvtw =
2300 IShellLinkW_fnQueryInterface,
2301 IShellLinkW_fnAddRef,
2302 IShellLinkW_fnRelease,
2303 IShellLinkW_fnGetPath,
2304 IShellLinkW_fnGetIDList,
2305 IShellLinkW_fnSetIDList,
2306 IShellLinkW_fnGetDescription,
2307 IShellLinkW_fnSetDescription,
2308 IShellLinkW_fnGetWorkingDirectory,
2309 IShellLinkW_fnSetWorkingDirectory,
2310 IShellLinkW_fnGetArguments,
2311 IShellLinkW_fnSetArguments,
2312 IShellLinkW_fnGetHotkey,
2313 IShellLinkW_fnSetHotkey,
2314 IShellLinkW_fnGetShowCmd,
2315 IShellLinkW_fnSetShowCmd,
2316 IShellLinkW_fnGetIconLocation,
2317 IShellLinkW_fnSetIconLocation,
2318 IShellLinkW_fnSetRelativePath,
2319 IShellLinkW_fnResolve,
2320 IShellLinkW_fnSetPath
2323 static HRESULT WINAPI
2324 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2326 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2327 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2331 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2333 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2334 return IShellLinkA_AddRef((IShellLinkA*)This);
2338 ShellLink_DataList_Release( IShellLinkDataList* iface )
2340 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2341 return ShellLink_Release( This );
2344 static HRESULT WINAPI
2345 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2347 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2351 static HRESULT WINAPI
2352 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2354 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2355 LPVOID block = NULL;
2358 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2362 case EXP_DARWIN_ID_SIG:
2363 if (!This->sComponent)
2365 block = shelllink_build_darwinid( This->sComponent, dwSig );
2368 case EXP_SZ_LINK_SIG:
2369 case NT_CONSOLE_PROPS_SIG:
2370 case NT_FE_CONSOLE_PROPS_SIG:
2371 case EXP_SPECIAL_FOLDER_SIG:
2372 case EXP_SZ_ICON_SIG:
2373 FIXME("valid but unhandled datablock %08x\n", dwSig);
2376 ERR("unknown datablock %08x\n", dwSig);
2378 *ppDataBlock = block;
2382 static HRESULT WINAPI
2383 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2385 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2389 static HRESULT WINAPI
2390 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2392 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2395 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2397 /* FIXME: add more */
2399 flags |= SLDF_HAS_ARGS;
2400 if (This->sComponent)
2401 flags |= SLDF_HAS_DARWINID;
2403 flags |= SLDF_HAS_ICONLOCATION;
2405 flags |= SLDF_HAS_LOGO3ID;
2407 flags |= SLDF_HAS_ID_LIST;
2414 static HRESULT WINAPI
2415 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2417 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2421 static const IShellLinkDataListVtbl dlvt =
2423 ShellLink_DataList_QueryInterface,
2424 ShellLink_DataList_AddRef,
2425 ShellLink_DataList_Release,
2426 ShellLink_AddDataBlock,
2427 ShellLink_CopyDataBlock,
2428 ShellLink_RemoveDataBlock,
2433 static HRESULT WINAPI
2434 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2436 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2437 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2441 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2443 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2444 return IShellLinkA_AddRef((IShellLinkA*)This);
2448 ShellLink_ExtInit_Release( IShellExtInit* iface )
2450 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2451 return ShellLink_Release( This );
2454 /**************************************************************************
2455 * ShellLink implementation of IShellExtInit::Initialize()
2457 * Loads the shelllink from the dataobject the shell is pointing to.
2459 static HRESULT WINAPI
2460 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2461 IDataObject *pdtobj, HKEY hkeyProgID )
2463 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2469 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2474 format.cfFormat = CF_HDROP;
2476 format.dwAspect = DVASPECT_CONTENT;
2478 format.tymed = TYMED_HGLOBAL;
2480 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2483 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2488 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2490 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2493 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2495 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2496 r = IPersistFile_Load( pf, path, 0 );
2497 HeapFree( GetProcessHeap(), 0, path );
2500 ReleaseStgMedium( &stgm );
2505 static const IShellExtInitVtbl eivt =
2507 ShellLink_ExtInit_QueryInterface,
2508 ShellLink_ExtInit_AddRef,
2509 ShellLink_ExtInit_Release,
2510 ShellLink_ExtInit_Initialize
2513 static HRESULT WINAPI
2514 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2516 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2517 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2521 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2523 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2524 return IShellLinkA_AddRef((IShellLinkA*)This);
2528 ShellLink_ContextMenu_Release( IContextMenu* iface )
2530 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2531 return ShellLink_Release( This );
2534 static HRESULT WINAPI
2535 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2536 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2538 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2539 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2543 TRACE("%p %p %u %u %u %u\n", This,
2544 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2547 return E_INVALIDARG;
2549 memset( &mii, 0, sizeof mii );
2550 mii.cbSize = sizeof mii;
2551 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2552 mii.dwTypeData = szOpen;
2553 mii.cch = strlenW( mii.dwTypeData );
2554 mii.wID = idCmdFirst + id++;
2555 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2556 mii.fType = MFT_STRING;
2557 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2561 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2565 shelllink_get_msi_component_path( LPWSTR component )
2570 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2571 if (r != ERROR_SUCCESS)
2575 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2576 r = CommandLineFromMsiDescriptor( component, path, &sz );
2577 if (r != ERROR_SUCCESS)
2579 HeapFree( GetProcessHeap(), 0, path );
2583 TRACE("returning %s\n", debugstr_w( path ) );
2588 static HRESULT WINAPI
2589 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2591 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2592 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2593 SHELLEXECUTEINFOW sei;
2594 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2599 TRACE("%p %p\n", This, lpici );
2601 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2602 return E_INVALIDARG;
2604 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2606 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2607 return E_INVALIDARG;
2610 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2614 if ( This->sComponent )
2616 path = shelllink_get_msi_component_path( This->sComponent );
2621 path = strdupW( This->sPath );
2623 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2624 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2626 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2630 len += lstrlenW( This->sArgs );
2631 if ( iciex->lpParametersW )
2632 len += lstrlenW( iciex->lpParametersW );
2634 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2637 lstrcatW( args, This->sArgs );
2638 if ( iciex->lpParametersW )
2640 static const WCHAR space[] = { ' ', 0 };
2641 lstrcatW( args, space );
2642 lstrcatW( args, iciex->lpParametersW );
2646 memset( &sei, 0, sizeof sei );
2647 sei.cbSize = sizeof sei;
2648 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2650 sei.nShow = This->iShowCmd;
2651 sei.lpIDList = This->pPidl;
2652 sei.lpDirectory = This->sWorkDir;
2653 sei.lpParameters = args;
2654 sei.lpVerb = szOpen;
2656 if( ShellExecuteExW( &sei ) )
2661 HeapFree( GetProcessHeap(), 0, args );
2662 HeapFree( GetProcessHeap(), 0, path );
2667 static HRESULT WINAPI
2668 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2669 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2671 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2673 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2674 idCmd, uType, pwReserved, pszName, cchMax );
2679 static const IContextMenuVtbl cmvt =
2681 ShellLink_ContextMenu_QueryInterface,
2682 ShellLink_ContextMenu_AddRef,
2683 ShellLink_ContextMenu_Release,
2684 ShellLink_QueryContextMenu,
2685 ShellLink_InvokeCommand,
2686 ShellLink_GetCommandString
2689 static HRESULT WINAPI
2690 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2692 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2693 return ShellLink_QueryInterface( This, riid, ppvObject );
2697 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2699 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2700 return ShellLink_AddRef( This );
2704 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2706 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2707 return ShellLink_Release( This );
2710 static HRESULT WINAPI
2711 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2713 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2715 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2719 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2722 static HRESULT WINAPI
2723 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2725 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2727 TRACE("%p %p\n", iface, punk);
2730 IUnknown_AddRef( punk );
2736 static const IObjectWithSiteVtbl owsvt =
2738 ShellLink_ObjectWithSite_QueryInterface,
2739 ShellLink_ObjectWithSite_AddRef,
2740 ShellLink_ObjectWithSite_Release,