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 */
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(LPWSTR 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) );
210 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
214 static inline LPWSTR strdupW( LPCWSTR src )
217 if (!src) return NULL;
218 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
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));
233 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
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);
268 IUnknown_AddRef((IUnknown*)(*ppvObj));
269 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
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);
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);
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);
309 IUnknown_Release( This->site );
314 LocalFree((HANDLE)This);
319 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
321 TRACE("%p %p\n", This, pclsid);
323 memcpy( pclsid, &CLSID_ShellLink, sizeof (CLSID) );
327 /**************************************************************************
328 * IPersistFile_QueryInterface
330 static HRESULT WINAPI IPersistFile_fnQueryInterface(
335 IShellLinkImpl *This = impl_from_IPersistFile(iface);
336 return ShellLink_QueryInterface( This, riid, ppvObj );
339 /******************************************************************************
340 * IPersistFile_AddRef
342 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
344 IShellLinkImpl *This = impl_from_IPersistFile(iface);
345 return ShellLink_AddRef( This );
348 /******************************************************************************
349 * IPersistFile_Release
351 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
353 IShellLinkImpl *This = impl_from_IPersistFile(iface);
354 return IShellLinkA_Release((IShellLinkA*)This);
357 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
359 IShellLinkImpl *This = impl_from_IPersistFile(iface);
360 return ShellLink_GetClassID( This, pClassID );
363 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
365 IShellLinkImpl *This = impl_from_IPersistFile(iface);
367 TRACE("(%p)\n",This);
375 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
377 IShellLinkImpl *This = impl_from_IPersistFile(iface);
378 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
382 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
385 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
386 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
389 r = IPersistStream_Load(StreamThis, stm);
390 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
391 IStream_Release( stm );
392 This->bDirty = FALSE;
394 TRACE("-- returning hr %08x\n", r);
398 static BOOL StartLinkProcessor( LPCOLESTR szLink )
400 static const WCHAR szFormat[] = {
401 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
402 ' ','-','r',' ','"','%','s','"',0 };
406 PROCESS_INFORMATION pi;
408 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
409 buffer = HeapAlloc( GetProcessHeap(), 0, len );
413 wsprintfW( buffer, szFormat, szLink );
415 TRACE("starting %s\n",debugstr_w(buffer));
417 memset(&si, 0, sizeof(si));
419 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
421 /* wait for a while to throttle the creation of linker processes */
422 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
423 WARN("Timed out waiting for shell linker\n");
425 CloseHandle( pi.hProcess );
426 CloseHandle( pi.hThread );
431 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
433 IShellLinkImpl *This = impl_from_IPersistFile(iface);
434 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
438 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
443 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
446 r = IPersistStream_Save(StreamThis, stm, FALSE);
447 IStream_Release( stm );
451 StartLinkProcessor( pszFileName );
453 This->bDirty = FALSE;
457 DeleteFileW( pszFileName );
458 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
465 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
467 IShellLinkImpl *This = impl_from_IPersistFile(iface);
468 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
472 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
474 IShellLinkImpl *This = impl_from_IPersistFile(iface);
475 FIXME("(%p)\n",This);
479 static const IPersistFileVtbl pfvt =
481 IPersistFile_fnQueryInterface,
482 IPersistFile_fnAddRef,
483 IPersistFile_fnRelease,
484 IPersistFile_fnGetClassID,
485 IPersistFile_fnIsDirty,
488 IPersistFile_fnSaveCompleted,
489 IPersistFile_fnGetCurFile
492 /************************************************************************
493 * IPersistStream_QueryInterface
495 static HRESULT WINAPI IPersistStream_fnQueryInterface(
496 IPersistStream* iface,
500 IShellLinkImpl *This = impl_from_IPersistStream(iface);
501 return ShellLink_QueryInterface( This, riid, ppvObj );
504 /************************************************************************
505 * IPersistStream_Release
507 static ULONG WINAPI IPersistStream_fnRelease(
508 IPersistStream* iface)
510 IShellLinkImpl *This = impl_from_IPersistStream(iface);
511 return IShellLinkA_Release((IShellLinkA*)This);
514 /************************************************************************
515 * IPersistStream_AddRef
517 static ULONG WINAPI IPersistStream_fnAddRef(
518 IPersistStream* iface)
520 IShellLinkImpl *This = impl_from_IPersistStream(iface);
521 return ShellLink_AddRef( This );
524 /************************************************************************
525 * IPersistStream_GetClassID
528 static HRESULT WINAPI IPersistStream_fnGetClassID(
529 IPersistStream* iface,
532 IShellLinkImpl *This = impl_from_IPersistStream(iface);
533 return ShellLink_GetClassID( This, pClassID );
536 /************************************************************************
537 * IPersistStream_IsDirty (IPersistStream)
539 static HRESULT WINAPI IPersistStream_fnIsDirty(
540 IPersistStream* iface)
542 IShellLinkImpl *This = impl_from_IPersistStream(iface);
544 TRACE("(%p)\n", This);
550 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
561 r = IStream_Read(stm, &len, sizeof(len), &count);
562 if ( FAILED (r) || ( count != sizeof(len) ) )
566 len *= sizeof (WCHAR);
568 TRACE("reading %d\n", len);
569 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
571 return E_OUTOFMEMORY;
573 r = IStream_Read(stm, temp, len, &count);
574 if( FAILED (r) || ( count != len ) )
576 HeapFree( GetProcessHeap(), 0, temp );
580 TRACE("read %s\n", debugstr_an(temp,len));
582 /* convert to unicode if necessary */
585 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
586 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
589 HeapFree( GetProcessHeap(), 0, temp );
590 return E_OUTOFMEMORY;
592 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
593 HeapFree( GetProcessHeap(), 0, temp );
607 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
614 unsigned char data[1];
619 r = IStream_Read( stm, &size, sizeof(size), &count );
620 if( FAILED( r ) || count != sizeof(size) )
623 chunk = HeapAlloc( GetProcessHeap(), 0, size );
625 return E_OUTOFMEMORY;
628 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
629 if( FAILED( r ) || count != (size - sizeof(size)) )
631 HeapFree( GetProcessHeap(), 0, chunk );
635 TRACE("Read %d bytes\n",chunk->size);
637 *data = (LPVOID) chunk;
642 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
644 const int label_sz = sizeof volume->label/sizeof volume->label[0];
648 volume->serial = vol->dwVolSerial;
649 volume->type = vol->dwType;
651 if( !vol->dwVolLabelOfs )
653 if( vol->dwSize <= vol->dwVolLabelOfs )
655 len = vol->dwSize - vol->dwVolLabelOfs;
658 label += vol->dwVolLabelOfs;
659 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
664 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
669 while( p[len] && (len < maxlen) )
672 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
673 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
674 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
680 static HRESULT Stream_LoadLocation( IStream *stm,
681 volume_info *volume, LPWSTR *path )
688 r = Stream_ReadChunk( stm, (LPVOID*) &p );
692 loc = (LOCATION_INFO*) p;
693 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
695 HeapFree( GetProcessHeap(), 0, p );
699 /* if there's valid local volume information, load it */
700 if( loc->dwVolTableOfs &&
701 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
703 LOCAL_VOLUME_INFO *volume_info;
705 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
706 Stream_LoadVolume( volume_info, volume );
709 /* if there's a local path, load it */
710 n = loc->dwLocalPathOfs;
711 if( n && (n < loc->dwTotalSize) )
712 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
714 TRACE("type %d serial %08x name %s path %s\n", volume->type,
715 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
717 HeapFree( GetProcessHeap(), 0, p );
722 * The format of the advertised shortcut info seems to be:
727 * 0 Length of the block (4 bytes, usually 0x314)
729 * 8 string data in ASCII
730 * 8+0x104 string data in UNICODE
732 * In the original Win32 implementation the buffers are not initialized
733 * to zero, so data trailing the string is random garbage.
735 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
740 EXP_DARWIN_LINK buffer;
744 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
748 /* make sure that we read the size of the structure even on error */
749 size = sizeof buffer - sizeof (DWORD);
750 if( buffer.dbh.cbSize != sizeof buffer )
752 ERR("Ooops. This structure is not as expected...\n");
756 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
763 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
765 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
767 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
771 *str = HeapAlloc( GetProcessHeap(), 0,
772 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
773 lstrcpyW( *str, buffer.szwDarwinID );
778 /************************************************************************
779 * IPersistStream_Load (IPersistStream)
781 static HRESULT WINAPI IPersistStream_fnLoad(
782 IPersistStream* iface,
791 IShellLinkImpl *This = impl_from_IPersistStream(iface);
793 TRACE("%p %p\n", This, stm);
796 return STG_E_INVALIDPOINTER;
799 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
803 if( dwBytesRead != sizeof(hdr))
805 if( hdr.dwSize != sizeof(hdr))
807 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
810 /* free all the old stuff */
813 memset( &This->volume, 0, sizeof This->volume );
814 HeapFree(GetProcessHeap(), 0, This->sPath);
816 HeapFree(GetProcessHeap(), 0, This->sDescription);
817 This->sDescription = NULL;
818 HeapFree(GetProcessHeap(), 0, This->sPathRel);
819 This->sPathRel = NULL;
820 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
821 This->sWorkDir = NULL;
822 HeapFree(GetProcessHeap(), 0, This->sArgs);
824 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
825 This->sIcoPath = NULL;
826 HeapFree(GetProcessHeap(), 0, This->sProduct);
827 This->sProduct = NULL;
828 HeapFree(GetProcessHeap(), 0, This->sComponent);
829 This->sComponent = NULL;
831 This->wHotKey = (WORD)hdr.wHotKey;
832 This->iIcoNdx = hdr.nIcon;
833 FileTimeToSystemTime (&hdr.Time1, &This->time1);
834 FileTimeToSystemTime (&hdr.Time2, &This->time2);
835 FileTimeToSystemTime (&hdr.Time3, &This->time3);
838 WCHAR sTemp[MAX_PATH];
839 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
840 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
841 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
842 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
843 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
844 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
845 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
846 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
847 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
850 /* load all the new stuff */
851 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
853 r = ILLoadFromStream( stm, &This->pPidl );
859 /* load the location information */
860 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
861 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
865 unicode = hdr.dwFlags & SLDF_UNICODE;
866 if( hdr.dwFlags & SLDF_HAS_NAME )
868 r = Stream_LoadString( stm, unicode, &This->sDescription );
869 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
874 if( hdr.dwFlags & SLDF_HAS_RELPATH )
876 r = Stream_LoadString( stm, unicode, &This->sPathRel );
877 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
882 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
884 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
885 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
890 if( hdr.dwFlags & SLDF_HAS_ARGS )
892 r = Stream_LoadString( stm, unicode, &This->sArgs );
893 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
898 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
900 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
901 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
906 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
908 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
909 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
914 if( hdr.dwFlags & SLDF_HAS_DARWINID )
916 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
917 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
922 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
923 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
924 ERR("Last word was not zero\n");
935 /************************************************************************
938 * Helper function for IPersistStream_Save. Writes a unicode string
939 * with terminating nul byte to a stream, preceded by the its length.
941 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
943 USHORT len = lstrlenW( str ) + 1;
947 r = IStream_Write( stm, &len, sizeof(len), &count );
951 len *= sizeof(WCHAR);
953 r = IStream_Write( stm, str, len, &count );
960 /************************************************************************
961 * Stream_WriteLocationInfo
963 * Writes the location info to a stream
965 * FIXME: One day we might want to write the network volume information
966 * and the final path.
967 * Figure out how Windows deals with unicode paths here.
969 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
970 volume_info *volume )
972 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
973 LOCAL_VOLUME_INFO *vol;
975 LPSTR szLabel, szPath, szFinalPath;
978 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
980 /* figure out the size of everything */
981 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
982 NULL, 0, NULL, NULL );
983 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
984 NULL, 0, NULL, NULL );
985 volume_info_size = sizeof *vol + label_size;
987 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
989 /* create pointers to everything */
990 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
991 vol = (LOCAL_VOLUME_INFO*) &loc[1];
992 szLabel = (LPSTR) &vol[1];
993 szPath = &szLabel[label_size];
994 szFinalPath = &szPath[path_size];
996 /* fill in the location information header */
997 loc->dwTotalSize = total_size;
998 loc->dwHeaderSize = sizeof (*loc);
1000 loc->dwVolTableOfs = sizeof (*loc);
1001 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1002 loc->dwNetworkVolTableOfs = 0;
1003 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1005 /* fill in the volume information */
1006 vol->dwSize = volume_info_size;
1007 vol->dwType = volume->type;
1008 vol->dwVolSerial = volume->serial;
1009 vol->dwVolLabelOfs = sizeof (*vol);
1011 /* copy in the strings */
1012 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1013 szLabel, label_size, NULL, NULL );
1014 WideCharToMultiByte( CP_ACP, 0, path, -1,
1015 szPath, path_size, NULL, NULL );
1018 return IStream_Write( stm, loc, total_size, &count );
1021 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1023 EXP_DARWIN_LINK *buffer;
1025 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1026 buffer->dbh.cbSize = sizeof *buffer;
1027 buffer->dbh.dwSignature = magic;
1028 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1029 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1034 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1036 EXP_DARWIN_LINK *buffer;
1041 buffer = shelllink_build_darwinid( string, magic );
1043 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1046 /************************************************************************
1047 * IPersistStream_Save (IPersistStream)
1049 * FIXME: makes assumptions about byte order
1051 static HRESULT WINAPI IPersistStream_fnSave(
1052 IPersistStream* iface,
1056 static const WCHAR wOpen[] = {'o','p','e','n',0};
1059 WCHAR exePath[MAX_PATH];
1064 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1066 TRACE("%p %p %x\n", This, stm, fClearDirty);
1072 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
1073 NULL, NULL, NULL, NULL);
1075 * windows can create lnk files to executables that do not exist yet
1076 * so if the executable does not exist the just trust the path they
1079 if (!*exePath) lstrcpyW(exePath,This->sPath);
1082 memset(&header, 0, sizeof(header));
1083 header.dwSize = sizeof(header);
1084 header.fStartup = This->iShowCmd;
1085 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
1087 header.wHotKey = This->wHotKey;
1088 header.nIcon = This->iIcoNdx;
1089 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1091 header.dwFlags |= SLDF_HAS_ID_LIST;
1093 header.dwFlags |= SLDF_HAS_LINK_INFO;
1094 if( This->sDescription )
1095 header.dwFlags |= SLDF_HAS_NAME;
1096 if( This->sWorkDir )
1097 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1099 header.dwFlags |= SLDF_HAS_ARGS;
1100 if( This->sIcoPath )
1101 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1102 if( This->sProduct )
1103 header.dwFlags |= SLDF_HAS_LOGO3ID;
1104 if( This->sComponent )
1105 header.dwFlags |= SLDF_HAS_DARWINID;
1107 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1108 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1109 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1111 /* write the Shortcut header */
1112 r = IStream_Write( stm, &header, sizeof(header), &count );
1115 ERR("Write failed at %d\n",__LINE__);
1119 TRACE("Writing pidl\n");
1121 /* write the PIDL to the shortcut */
1124 r = ILSaveToStream( stm, This->pPidl );
1127 ERR("Failed to write PIDL at %d\n",__LINE__);
1133 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1135 if( This->sDescription )
1136 r = Stream_WriteString( stm, This->sDescription );
1138 if( This->sPathRel )
1139 r = Stream_WriteString( stm, This->sPathRel );
1141 if( This->sWorkDir )
1142 r = Stream_WriteString( stm, This->sWorkDir );
1145 r = Stream_WriteString( stm, This->sArgs );
1147 if( This->sIcoPath )
1148 r = Stream_WriteString( stm, This->sIcoPath );
1150 if( This->sProduct )
1151 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1153 if( This->sComponent )
1154 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1156 /* the last field is a single zero dword */
1158 r = IStream_Write( stm, &zero, sizeof zero, &count );
1163 /************************************************************************
1164 * IPersistStream_GetSizeMax (IPersistStream)
1166 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1167 IPersistStream* iface,
1168 ULARGE_INTEGER* pcbSize)
1170 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1172 TRACE("(%p)\n", This);
1177 static const IPersistStreamVtbl psvt =
1179 IPersistStream_fnQueryInterface,
1180 IPersistStream_fnAddRef,
1181 IPersistStream_fnRelease,
1182 IPersistStream_fnGetClassID,
1183 IPersistStream_fnIsDirty,
1184 IPersistStream_fnLoad,
1185 IPersistStream_fnSave,
1186 IPersistStream_fnGetSizeMax
1189 /**************************************************************************
1190 * IShellLink_Constructor
1192 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1193 REFIID riid, LPVOID *ppv )
1195 IShellLinkImpl * sl;
1198 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1203 return CLASS_E_NOAGGREGATION;
1204 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1206 return E_OUTOFMEMORY;
1210 sl->lpvtblw = &slvtw;
1211 sl->lpvtblPersistFile = &pfvt;
1212 sl->lpvtblPersistStream = &psvt;
1213 sl->lpvtblShellLinkDataList = &dlvt;
1214 sl->lpvtblShellExtInit = &eivt;
1215 sl->lpvtblContextMenu = &cmvt;
1216 sl->lpvtblObjectWithSite = &owsvt;
1217 sl->iShowCmd = SW_SHOWNORMAL;
1222 TRACE("(%p)->()\n",sl);
1224 r = ShellLink_QueryInterface( sl, riid, ppv );
1225 ShellLink_Release( sl );
1230 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1232 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1237 /**************************************************************************
1238 * ShellLink_UpdatePath
1239 * update absolute path in sPath using relative path in sPathRel
1241 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1243 if (!path || !psPath)
1244 return E_INVALIDARG;
1246 if (!*psPath && sPathRel) {
1247 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1248 LPWSTR final = NULL;
1250 /* first try if [directory of link file] + [relative path] finds an existing file */
1252 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1255 lstrcpyW(final, sPathRel);
1259 if (SHELL_ExistsFileW(buffer)) {
1260 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1261 lstrcpyW(abs_path, buffer);
1263 /* try if [working directory] + [relative path] finds an existing file */
1265 lstrcpyW(buffer, sWorkDir);
1266 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1268 if (SHELL_ExistsFileW(buffer))
1269 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1270 lstrcpyW(abs_path, buffer);
1274 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1276 lstrcpyW(abs_path, sPathRel);
1278 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1280 return E_OUTOFMEMORY;
1282 lstrcpyW(*psPath, abs_path);
1288 /**************************************************************************
1289 * IShellLink_ConstructFromFile
1291 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1292 LPCITEMIDLIST pidl, LPVOID* ppv)
1296 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1298 if (SUCCEEDED(hr)) {
1303 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1305 if (SUCCEEDED(hr)) {
1306 WCHAR path[MAX_PATH];
1308 if (SHGetPathFromIDListW(pidl, path))
1309 hr = IPersistFile_Load(ppf, path, 0);
1314 *ppv = (IUnknown*) psl;
1316 IPersistFile_Release(ppf);
1320 IShellLinkW_Release(psl);
1326 /**************************************************************************
1327 * IShellLinkA_QueryInterface
1329 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1331 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1332 return ShellLink_QueryInterface( This, riid, ppvObj );
1335 /******************************************************************************
1336 * IShellLinkA_AddRef
1338 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1340 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1341 return ShellLink_AddRef( This );
1344 /******************************************************************************
1345 * IShellLinkA_Release
1347 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1349 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1350 return ShellLink_Release( This );
1353 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1354 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1356 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1358 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1359 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1361 if (This->sComponent || This->sProduct)
1367 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1368 pszFile, cchMaxPath, NULL, NULL);
1370 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1375 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1377 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1379 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1381 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1384 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1386 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1388 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1391 ILFree(This->pPidl);
1392 This->pPidl = ILClone (pidl);
1393 This->bDirty = TRUE;
1398 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1400 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1402 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1406 if( This->sDescription )
1407 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1408 pszName, cchMaxName, NULL, NULL);
1413 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1415 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1417 TRACE("(%p)->(pName=%s)\n", This, pszName);
1419 HeapFree(GetProcessHeap(), 0, This->sDescription);
1420 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1421 if ( !This->sDescription )
1422 return E_OUTOFMEMORY;
1424 This->bDirty = TRUE;
1429 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1431 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1433 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1437 if( This->sWorkDir )
1438 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1439 pszDir, cchMaxPath, NULL, NULL);
1444 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1446 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1448 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1450 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1451 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1452 if ( !This->sWorkDir )
1453 return E_OUTOFMEMORY;
1455 This->bDirty = TRUE;
1460 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1462 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1464 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1469 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1470 pszArgs, cchMaxPath, NULL, NULL);
1475 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1477 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1479 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1481 HeapFree(GetProcessHeap(), 0, This->sArgs);
1482 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1484 return E_OUTOFMEMORY;
1486 This->bDirty = TRUE;
1491 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1493 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1495 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1497 *pwHotkey = This->wHotKey;
1502 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1504 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1506 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1508 This->wHotKey = wHotkey;
1509 This->bDirty = TRUE;
1514 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1516 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1518 TRACE("(%p)->(%p)\n",This, piShowCmd);
1519 *piShowCmd = This->iShowCmd;
1523 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1525 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1527 TRACE("(%p) %d\n",This, iShowCmd);
1529 This->iShowCmd = iShowCmd;
1530 This->bDirty = TRUE;
1535 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1537 LPCITEMIDLIST pidlLast;
1539 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1541 if (SUCCEEDED(hr)) {
1544 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1546 if (SUCCEEDED(hr)) {
1547 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1549 IExtractIconA_Release(pei);
1552 IShellFolder_Release(psf);
1558 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1560 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1562 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1565 *piIcon = This->iIcoNdx;
1569 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1573 if (This->pPidl || This->sPath)
1577 HRESULT hr = SHGetDesktopFolder(&pdsk);
1581 /* first look for an icon using the PIDL (if present) */
1583 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1587 /* if we couldn't find an icon yet, look for it using the file system path */
1588 if (FAILED(hr) && This->sPath)
1592 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1594 if (SUCCEEDED(hr)) {
1595 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1601 IShellFolder_Release(pdsk);
1609 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1611 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1613 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1615 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1616 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1617 if ( !This->sIcoPath )
1618 return E_OUTOFMEMORY;
1620 This->iIcoNdx = iIcon;
1621 This->bDirty = TRUE;
1626 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1628 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1630 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1632 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1633 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1634 This->bDirty = TRUE;
1636 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1639 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1641 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1643 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1645 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1648 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1652 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1654 TRACE("(%p)->(path=%s)\n",This, pszFile);
1656 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1658 return E_OUTOFMEMORY;
1660 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1661 HeapFree( GetProcessHeap(), 0, str );
1666 /**************************************************************************
1667 * IShellLink Implementation
1670 static const IShellLinkAVtbl slvt =
1672 IShellLinkA_fnQueryInterface,
1673 IShellLinkA_fnAddRef,
1674 IShellLinkA_fnRelease,
1675 IShellLinkA_fnGetPath,
1676 IShellLinkA_fnGetIDList,
1677 IShellLinkA_fnSetIDList,
1678 IShellLinkA_fnGetDescription,
1679 IShellLinkA_fnSetDescription,
1680 IShellLinkA_fnGetWorkingDirectory,
1681 IShellLinkA_fnSetWorkingDirectory,
1682 IShellLinkA_fnGetArguments,
1683 IShellLinkA_fnSetArguments,
1684 IShellLinkA_fnGetHotkey,
1685 IShellLinkA_fnSetHotkey,
1686 IShellLinkA_fnGetShowCmd,
1687 IShellLinkA_fnSetShowCmd,
1688 IShellLinkA_fnGetIconLocation,
1689 IShellLinkA_fnSetIconLocation,
1690 IShellLinkA_fnSetRelativePath,
1691 IShellLinkA_fnResolve,
1692 IShellLinkA_fnSetPath
1696 /**************************************************************************
1697 * IShellLinkW_fnQueryInterface
1699 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1700 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1702 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1703 return ShellLink_QueryInterface( This, riid, ppvObj );
1706 /******************************************************************************
1707 * IShellLinkW_fnAddRef
1709 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1711 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1712 return ShellLink_AddRef( This );
1715 /******************************************************************************
1716 * IShellLinkW_fnRelease
1718 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1720 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1721 return ShellLink_Release( This );
1724 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1726 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1728 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1729 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1731 if (This->sComponent || This->sProduct)
1737 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1739 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1744 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1746 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1748 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1755 *ppidl = ILClone(This->pPidl);
1759 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1761 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1763 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1766 ILFree( This->pPidl );
1767 This->pPidl = ILClone( pidl );
1771 This->bDirty = TRUE;
1776 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1778 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1780 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1783 if( This->sDescription )
1784 lstrcpynW( pszName, This->sDescription, cchMaxName );
1789 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1791 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1793 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1795 HeapFree(GetProcessHeap(), 0, This->sDescription);
1796 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1797 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1798 if ( !This->sDescription )
1799 return E_OUTOFMEMORY;
1801 lstrcpyW( This->sDescription, pszName );
1802 This->bDirty = TRUE;
1807 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1809 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1811 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1815 if( This->sWorkDir )
1816 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1821 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1823 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1825 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1827 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1828 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1829 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1830 if ( !This->sWorkDir )
1831 return E_OUTOFMEMORY;
1832 lstrcpyW( This->sWorkDir, pszDir );
1833 This->bDirty = TRUE;
1838 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1840 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1842 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1847 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1852 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1854 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1856 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1858 HeapFree(GetProcessHeap(), 0, This->sArgs);
1859 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1860 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1862 return E_OUTOFMEMORY;
1863 lstrcpyW( This->sArgs, pszArgs );
1864 This->bDirty = TRUE;
1869 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1871 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1873 TRACE("(%p)->(%p)\n",This, pwHotkey);
1875 *pwHotkey=This->wHotKey;
1880 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1882 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1884 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1886 This->wHotKey = wHotkey;
1887 This->bDirty = TRUE;
1892 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1894 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1896 TRACE("(%p)->(%p)\n",This, piShowCmd);
1898 *piShowCmd = This->iShowCmd;
1903 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1905 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1907 This->iShowCmd = iShowCmd;
1908 This->bDirty = TRUE;
1913 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1915 LPCITEMIDLIST pidlLast;
1917 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1919 if (SUCCEEDED(hr)) {
1922 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1924 if (SUCCEEDED(hr)) {
1925 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1927 IExtractIconW_Release(pei);
1930 IShellFolder_Release(psf);
1936 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1938 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1940 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1943 *piIcon = This->iIcoNdx;
1947 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1951 if (This->pPidl || This->sPath)
1955 HRESULT hr = SHGetDesktopFolder(&pdsk);
1959 /* first look for an icon using the PIDL (if present) */
1961 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1965 /* if we couldn't find an icon yet, look for it using the file system path */
1966 if (FAILED(hr) && This->sPath)
1970 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1974 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1980 IShellFolder_Release(pdsk);
1987 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1989 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1991 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1993 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1994 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1995 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1996 if ( !This->sIcoPath )
1997 return E_OUTOFMEMORY;
1998 lstrcpyW( This->sIcoPath, pszIconPath );
2000 This->iIcoNdx = iIcon;
2001 This->bDirty = TRUE;
2006 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2008 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2010 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2012 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2013 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2014 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2015 if ( !This->sPathRel )
2016 return E_OUTOFMEMORY;
2017 lstrcpyW( This->sPathRel, pszPathRel );
2018 This->bDirty = TRUE;
2020 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2023 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2028 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2030 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2032 /*FIXME: use IResolveShellLink interface */
2034 if (!This->sPath && This->pPidl) {
2035 WCHAR buffer[MAX_PATH];
2037 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2039 if (bSuccess && *buffer) {
2040 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2042 return E_OUTOFMEMORY;
2044 lstrcpyW(This->sPath, buffer);
2046 This->bDirty = TRUE;
2048 hr = S_OK; /* don't report an error occurred while just caching information */
2051 if (!This->sIcoPath && This->sPath) {
2052 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2053 if (!This->sIcoPath)
2054 return E_OUTOFMEMORY;
2056 lstrcpyW(This->sIcoPath, This->sPath);
2059 This->bDirty = TRUE;
2065 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2074 p = strchrW( str, ':' );
2078 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2081 memcpy( ret, str, sizeof(WCHAR)*len );
2086 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2088 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2096 /* each segment must start with two colons */
2097 if( str[0] != ':' || str[1] != ':' )
2100 /* the last segment is just two colons */
2105 /* there must be a colon straight after a guid */
2106 p = strchrW( str, ':' );
2113 /* get the guid, and check it's validly formatted */
2114 memcpy( szGuid, str, sizeof(WCHAR)*len );
2116 r = CLSIDFromString( szGuid, &guid );
2121 /* match it up to a guid that we care about */
2122 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2124 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2129 /* skip to the next field */
2130 str = strchrW( str, ':' );
2135 /* we have to have a component for an advertised shortcut */
2139 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2140 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2142 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2143 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2148 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2150 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2151 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2154 volume->type = GetDriveTypeW(drive);
2155 r = GetVolumeInformationW(drive, volume->label, label_sz,
2156 &volume->serial, NULL, NULL, NULL, 0);
2157 TRACE("r = %d type %d serial %08x name %s\n", r,
2158 volume->type, volume->serial, debugstr_w(volume->label));
2162 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2164 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2165 WCHAR buffer[MAX_PATH];
2166 LPWSTR fname, unquoted = NULL;
2170 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2172 /* quotes at the ends of the string are stripped */
2173 len = lstrlenW(pszFile);
2174 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2176 unquoted = strdupW(pszFile);
2177 PathUnquoteSpacesW(unquoted);
2181 /* any other quote marks are invalid */
2182 if (strchrW(pszFile, '"'))
2185 HeapFree(GetProcessHeap(), 0, This->sPath);
2188 HeapFree(GetProcessHeap(), 0, This->sComponent);
2189 This->sComponent = NULL;
2192 ILFree(This->pPidl);
2195 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2197 if (*pszFile == '\0')
2199 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2201 else if(!PathFileExistsW(buffer) &&
2202 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2205 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2206 ShellLink_GetVolumeInfo(buffer, &This->volume);
2208 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2209 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2211 return E_OUTOFMEMORY;
2213 lstrcpyW(This->sPath, buffer);
2215 This->bDirty = TRUE;
2216 HeapFree(GetProcessHeap(), 0, unquoted);
2221 /**************************************************************************
2222 * IShellLinkW Implementation
2225 static const IShellLinkWVtbl slvtw =
2227 IShellLinkW_fnQueryInterface,
2228 IShellLinkW_fnAddRef,
2229 IShellLinkW_fnRelease,
2230 IShellLinkW_fnGetPath,
2231 IShellLinkW_fnGetIDList,
2232 IShellLinkW_fnSetIDList,
2233 IShellLinkW_fnGetDescription,
2234 IShellLinkW_fnSetDescription,
2235 IShellLinkW_fnGetWorkingDirectory,
2236 IShellLinkW_fnSetWorkingDirectory,
2237 IShellLinkW_fnGetArguments,
2238 IShellLinkW_fnSetArguments,
2239 IShellLinkW_fnGetHotkey,
2240 IShellLinkW_fnSetHotkey,
2241 IShellLinkW_fnGetShowCmd,
2242 IShellLinkW_fnSetShowCmd,
2243 IShellLinkW_fnGetIconLocation,
2244 IShellLinkW_fnSetIconLocation,
2245 IShellLinkW_fnSetRelativePath,
2246 IShellLinkW_fnResolve,
2247 IShellLinkW_fnSetPath
2250 static HRESULT WINAPI
2251 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2253 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2254 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2258 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2260 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2261 return IShellLinkA_AddRef((IShellLinkA*)This);
2265 ShellLink_DataList_Release( IShellLinkDataList* iface )
2267 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2268 return ShellLink_Release( This );
2271 static HRESULT WINAPI
2272 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2278 static HRESULT WINAPI
2279 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2281 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2282 LPVOID block = NULL;
2285 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2289 case EXP_DARWIN_ID_SIG:
2290 if (!This->sComponent)
2292 block = shelllink_build_darwinid( This->sComponent, dwSig );
2295 case EXP_SZ_LINK_SIG:
2296 case NT_CONSOLE_PROPS_SIG:
2297 case NT_FE_CONSOLE_PROPS_SIG:
2298 case EXP_SPECIAL_FOLDER_SIG:
2299 case EXP_SZ_ICON_SIG:
2300 FIXME("valid but unhandled datablock %08x\n", dwSig);
2303 ERR("unknown datablock %08x\n", dwSig);
2305 *ppDataBlock = block;
2309 static HRESULT WINAPI
2310 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2316 static HRESULT WINAPI
2317 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2319 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2322 FIXME("%p %p\n", This, pdwFlags );
2324 /* FIXME: add more */
2326 flags |= SLDF_HAS_ARGS;
2327 if (This->sComponent)
2328 flags |= SLDF_HAS_DARWINID;
2330 flags |= SLDF_HAS_ICONLOCATION;
2332 flags |= SLDF_HAS_LOGO3ID;
2334 flags |= SLDF_HAS_ID_LIST;
2341 static HRESULT WINAPI
2342 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2348 static const IShellLinkDataListVtbl dlvt =
2350 ShellLink_DataList_QueryInterface,
2351 ShellLink_DataList_AddRef,
2352 ShellLink_DataList_Release,
2353 ShellLink_AddDataBlock,
2354 ShellLink_CopyDataBlock,
2355 ShellLink_RemoveDataBlock,
2360 static HRESULT WINAPI
2361 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2363 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2364 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2368 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2370 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2371 return IShellLinkA_AddRef((IShellLinkA*)This);
2375 ShellLink_ExtInit_Release( IShellExtInit* iface )
2377 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2378 return ShellLink_Release( This );
2381 /**************************************************************************
2382 * ShellLink implementation of IShellExtInit::Initialize()
2384 * Loads the shelllink from the dataobject the shell is pointing to.
2386 static HRESULT WINAPI
2387 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2388 IDataObject *pdtobj, HKEY hkeyProgID )
2390 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2396 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2401 format.cfFormat = CF_HDROP;
2403 format.dwAspect = DVASPECT_CONTENT;
2405 format.tymed = TYMED_HGLOBAL;
2407 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2410 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2415 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2417 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2420 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2422 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2423 r = IPersistFile_Load( pf, path, 0 );
2424 HeapFree( GetProcessHeap(), 0, path );
2427 ReleaseStgMedium( &stgm );
2432 static const IShellExtInitVtbl eivt =
2434 ShellLink_ExtInit_QueryInterface,
2435 ShellLink_ExtInit_AddRef,
2436 ShellLink_ExtInit_Release,
2437 ShellLink_ExtInit_Initialize
2440 static HRESULT WINAPI
2441 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2443 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2444 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2448 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2450 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2451 return IShellLinkA_AddRef((IShellLinkA*)This);
2455 ShellLink_ContextMenu_Release( IContextMenu* iface )
2457 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2458 return ShellLink_Release( This );
2461 static HRESULT WINAPI
2462 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2463 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2465 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2466 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2470 TRACE("%p %p %u %u %u %u\n", This,
2471 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2474 return E_INVALIDARG;
2476 memset( &mii, 0, sizeof mii );
2477 mii.cbSize = sizeof mii;
2478 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2479 mii.dwTypeData = szOpen;
2480 mii.cch = strlenW( mii.dwTypeData );
2481 mii.wID = idCmdFirst + id++;
2482 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2483 mii.fType = MFT_STRING;
2484 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2488 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2492 shelllink_get_msi_component_path( LPWSTR component )
2497 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2498 if (r != ERROR_SUCCESS)
2502 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2503 r = CommandLineFromMsiDescriptor( component, path, &sz );
2504 if (r != ERROR_SUCCESS)
2506 HeapFree( GetProcessHeap(), 0, path );
2510 TRACE("returning %s\n", debugstr_w( path ) );
2515 static HRESULT WINAPI
2516 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2518 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2519 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2520 SHELLEXECUTEINFOW sei;
2521 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2526 TRACE("%p %p\n", This, lpici );
2528 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2529 return E_INVALIDARG;
2531 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2533 ERR("Unknown id %d != %d\n", (INT)lpici->lpVerb, This->iIdOpen );
2534 return E_INVALIDARG;
2537 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2541 if ( This->sComponent )
2543 path = shelllink_get_msi_component_path( This->sComponent );
2548 path = strdupW( This->sPath );
2550 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2551 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2553 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2557 len += lstrlenW( This->sArgs );
2558 if ( iciex->lpParametersW )
2559 len += lstrlenW( iciex->lpParametersW );
2561 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2564 lstrcatW( args, This->sArgs );
2565 if ( iciex->lpParametersW )
2567 static const WCHAR space[] = { ' ', 0 };
2568 lstrcatW( args, space );
2569 lstrcatW( args, iciex->lpParametersW );
2573 memset( &sei, 0, sizeof sei );
2574 sei.cbSize = sizeof sei;
2575 sei.fMask = SEE_MASK_UNICODE | SEE_MASK_NOCLOSEPROCESS;
2577 sei.nShow = This->iShowCmd;
2578 sei.lpIDList = This->pPidl;
2579 sei.lpDirectory = This->sWorkDir;
2580 sei.lpParameters = args;
2581 sei.lpVerb = szOpen;
2583 if( ShellExecuteExW( &sei ) )
2587 WaitForSingleObject( sei.hProcess, 10000 );
2588 CloseHandle( sei.hProcess );
2595 HeapFree( GetProcessHeap(), 0, args );
2596 HeapFree( GetProcessHeap(), 0, path );
2601 static HRESULT WINAPI
2602 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2603 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2605 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2607 FIXME("%p %lu %u %p %p %u\n", This,
2608 idCmd, uType, pwReserved, pszName, cchMax );
2613 static const IContextMenuVtbl cmvt =
2615 ShellLink_ContextMenu_QueryInterface,
2616 ShellLink_ContextMenu_AddRef,
2617 ShellLink_ContextMenu_Release,
2618 ShellLink_QueryContextMenu,
2619 ShellLink_InvokeCommand,
2620 ShellLink_GetCommandString
2623 static HRESULT WINAPI
2624 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2626 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2627 return ShellLink_QueryInterface( This, riid, ppvObject );
2631 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2633 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2634 return ShellLink_AddRef( This );
2638 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2640 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2641 return ShellLink_Release( This );
2644 static HRESULT WINAPI
2645 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2647 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2649 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2653 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2656 static HRESULT WINAPI
2657 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2659 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2661 TRACE("%p %p\n", iface, punk);
2664 IUnknown_AddRef( punk );
2670 static const IObjectWithSiteVtbl owsvt =
2672 ShellLink_ObjectWithSite_QueryInterface,
2673 ShellLink_ObjectWithSite_AddRef,
2674 ShellLink_ObjectWithSite_Release,