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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Nearly complete informations 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"
58 WINE_DEFAULT_DEBUG_CHANNEL(shell);
60 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
61 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
62 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
63 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
65 /* link file formats */
69 typedef struct _LINK_HEADER
71 DWORD dwSize; /* 0x00 size of the header - 0x4c */
72 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
73 DWORD dwFlags; /* 0x14 describes elements following */
74 DWORD dwFileAttr; /* 0x18 attributes of the target file */
75 FILETIME Time1; /* 0x1c */
76 FILETIME Time2; /* 0x24 */
77 FILETIME Time3; /* 0x2c */
78 DWORD dwFileLength; /* 0x34 File length */
79 DWORD nIcon; /* 0x38 icon number */
80 DWORD fStartup; /* 0x3c startup type */
81 DWORD wHotKey; /* 0x40 hotkey */
82 DWORD Unknown5; /* 0x44 */
83 DWORD Unknown6; /* 0x48 */
84 } LINK_HEADER, * PLINK_HEADER;
86 #define SHLINK_LOCAL 0
87 #define SHLINK_REMOTE 1
89 typedef struct _LOCATION_INFO
96 DWORD dwNetworkVolTableOfs;
100 typedef struct _LOCAL_VOLUME_INFO
108 typedef struct volume_info_t
112 WCHAR label[12]; /* assume 8.3 */
117 static const IShellLinkAVtbl slvt;
118 static const IShellLinkWVtbl slvtw;
119 static const IPersistFileVtbl pfvt;
120 static const IPersistStreamVtbl psvt;
121 static const IShellLinkDataListVtbl dlvt;
122 static const IShellExtInitVtbl eivt;
123 static const IContextMenuVtbl cmvt;
124 static const IObjectWithSiteVtbl owsvt;
126 /* IShellLink Implementation */
130 const IShellLinkAVtbl *lpVtbl;
131 const IShellLinkWVtbl *lpvtblw;
132 const IPersistFileVtbl *lpvtblPersistFile;
133 const IPersistStreamVtbl *lpvtblPersistStream;
134 const IShellLinkDataListVtbl *lpvtblShellLinkDataList;
135 const IShellExtInitVtbl *lpvtblShellExtInit;
136 const IContextMenuVtbl *lpvtblContextMenu;
137 const IObjectWithSiteVtbl *lpvtblObjectWithSite;
141 /* data structures according to the informations in the link */
161 INT iIdOpen; /* id of the "Open" entry in the context menu */
165 static inline IShellLinkImpl *impl_from_IShellLinkW( IShellLinkW *iface )
167 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
170 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
172 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
175 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
177 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
180 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
182 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
185 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
187 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
190 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
192 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
195 static inline IShellLinkImpl *impl_from_IObjectWithSite( IObjectWithSite *iface )
197 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblObjectWithSite));
200 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
202 /* strdup on the process heap */
203 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
205 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
206 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
209 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
213 inline static LPWSTR strdupW( LPCWSTR src )
216 if (!src) return NULL;
217 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
223 /**************************************************************************
224 * ShellLink::QueryInterface implementation
226 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
228 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
232 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
236 else if(IsEqualIID(riid, &IID_IShellLinkW))
238 *ppvObj = &(This->lpvtblw);
240 else if(IsEqualIID(riid, &IID_IPersistFile))
242 *ppvObj = &(This->lpvtblPersistFile);
244 else if(IsEqualIID(riid, &IID_IPersistStream))
246 *ppvObj = &(This->lpvtblPersistStream);
248 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
250 *ppvObj = &(This->lpvtblShellLinkDataList);
252 else if(IsEqualIID(riid, &IID_IShellExtInit))
254 *ppvObj = &(This->lpvtblShellExtInit);
256 else if(IsEqualIID(riid, &IID_IContextMenu))
258 *ppvObj = &(This->lpvtblContextMenu);
260 else if(IsEqualIID(riid, &IID_IObjectWithSite))
262 *ppvObj = &(This->lpvtblObjectWithSite);
267 IUnknown_AddRef((IUnknown*)(*ppvObj));
268 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
271 ERR("-- Interface: E_NOINTERFACE\n");
272 return E_NOINTERFACE;
275 /**************************************************************************
276 * ShellLink::AddRef implementation
278 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
280 ULONG refCount = InterlockedIncrement(&This->ref);
282 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
287 /**************************************************************************
288 * ShellLink::Release implementation
290 static ULONG ShellLink_Release( IShellLinkImpl *This )
292 ULONG refCount = InterlockedDecrement(&This->ref);
294 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
299 TRACE("-- destroying IShellLink(%p)\n",This);
301 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
302 HeapFree(GetProcessHeap(), 0, This->sArgs);
303 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
304 HeapFree(GetProcessHeap(), 0, This->sDescription);
305 HeapFree(GetProcessHeap(),0,This->sPath);
308 IUnknown_Release( This->site );
313 LocalFree((HANDLE)This);
318 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
320 TRACE("%p %p\n", This, pclsid);
322 memcpy( pclsid, &CLSID_ShellLink, sizeof (CLSID) );
326 /**************************************************************************
327 * IPersistFile_QueryInterface
329 static HRESULT WINAPI IPersistFile_fnQueryInterface(
334 IShellLinkImpl *This = impl_from_IPersistFile(iface);
335 return ShellLink_QueryInterface( This, riid, ppvObj );
338 /******************************************************************************
339 * IPersistFile_AddRef
341 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
343 IShellLinkImpl *This = impl_from_IPersistFile(iface);
344 return ShellLink_AddRef( This );
347 /******************************************************************************
348 * IPersistFile_Release
350 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
352 IShellLinkImpl *This = impl_from_IPersistFile(iface);
353 return IShellLinkA_Release((IShellLinkA*)This);
356 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
358 IShellLinkImpl *This = impl_from_IPersistFile(iface);
359 return ShellLink_GetClassID( This, pClassID );
362 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
364 IShellLinkImpl *This = impl_from_IPersistFile(iface);
366 TRACE("(%p)\n",This);
374 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
376 IShellLinkImpl *This = impl_from_IPersistFile(iface);
377 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
381 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
384 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
385 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
388 r = IPersistStream_Load(StreamThis, stm);
389 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
390 IStream_Release( stm );
391 This->bDirty = FALSE;
393 TRACE("-- returning hr %08lx\n", r);
397 static BOOL StartLinkProcessor( LPCOLESTR szLink )
399 static const WCHAR szFormat[] = {
400 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
401 ' ','-','r',' ','"','%','s','"',0 };
405 PROCESS_INFORMATION pi;
407 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
408 buffer = HeapAlloc( GetProcessHeap(), 0, len );
412 wsprintfW( buffer, szFormat, szLink );
414 TRACE("starting %s\n",debugstr_w(buffer));
416 memset(&si, 0, sizeof(si));
418 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
420 /* wait for a while to throttle the creation of linker processes */
421 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
422 WARN("Timed out waiting for shell linker\n");
424 CloseHandle( pi.hProcess );
425 CloseHandle( pi.hThread );
430 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
432 IShellLinkImpl *This = impl_from_IPersistFile(iface);
433 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
437 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
442 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
445 r = IPersistStream_Save(StreamThis, stm, FALSE);
446 IStream_Release( stm );
450 StartLinkProcessor( pszFileName );
452 This->bDirty = FALSE;
456 DeleteFileW( pszFileName );
457 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
464 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
466 IShellLinkImpl *This = impl_from_IPersistFile(iface);
467 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
471 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
473 IShellLinkImpl *This = impl_from_IPersistFile(iface);
474 FIXME("(%p)\n",This);
478 static const IPersistFileVtbl pfvt =
480 IPersistFile_fnQueryInterface,
481 IPersistFile_fnAddRef,
482 IPersistFile_fnRelease,
483 IPersistFile_fnGetClassID,
484 IPersistFile_fnIsDirty,
487 IPersistFile_fnSaveCompleted,
488 IPersistFile_fnGetCurFile
491 /************************************************************************
492 * IPersistStream_QueryInterface
494 static HRESULT WINAPI IPersistStream_fnQueryInterface(
495 IPersistStream* iface,
499 IShellLinkImpl *This = impl_from_IPersistStream(iface);
500 return ShellLink_QueryInterface( This, riid, ppvObj );
503 /************************************************************************
504 * IPersistStream_Release
506 static ULONG WINAPI IPersistStream_fnRelease(
507 IPersistStream* iface)
509 IShellLinkImpl *This = impl_from_IPersistStream(iface);
510 return IShellLinkA_Release((IShellLinkA*)This);
513 /************************************************************************
514 * IPersistStream_AddRef
516 static ULONG WINAPI IPersistStream_fnAddRef(
517 IPersistStream* iface)
519 IShellLinkImpl *This = impl_from_IPersistStream(iface);
520 return ShellLink_AddRef( This );
523 /************************************************************************
524 * IPersistStream_GetClassID
527 static HRESULT WINAPI IPersistStream_fnGetClassID(
528 IPersistStream* iface,
531 IShellLinkImpl *This = impl_from_IPersistStream(iface);
532 return ShellLink_GetClassID( This, pClassID );
535 /************************************************************************
536 * IPersistStream_IsDirty (IPersistStream)
538 static HRESULT WINAPI IPersistStream_fnIsDirty(
539 IPersistStream* iface)
541 IShellLinkImpl *This = impl_from_IPersistStream(iface);
543 TRACE("(%p)\n", This);
549 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
560 r = IStream_Read(stm, &len, sizeof(len), &count);
561 if ( FAILED (r) || ( count != sizeof(len) ) )
565 len *= sizeof (WCHAR);
567 TRACE("reading %d\n", len);
568 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
570 return E_OUTOFMEMORY;
572 r = IStream_Read(stm, temp, len, &count);
573 if( FAILED (r) || ( count != len ) )
575 HeapFree( GetProcessHeap(), 0, temp );
579 TRACE("read %s\n", debugstr_an(temp,len));
581 /* convert to unicode if necessary */
584 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
585 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
587 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
588 HeapFree( GetProcessHeap(), 0, temp );
602 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
609 unsigned char data[1];
614 r = IStream_Read( stm, &size, sizeof(size), &count );
615 if( FAILED( r ) || count != sizeof(size) )
618 chunk = HeapAlloc( GetProcessHeap(), 0, size );
620 return E_OUTOFMEMORY;
623 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
624 if( FAILED( r ) || count != (size - sizeof(size)) )
626 HeapFree( GetProcessHeap(), 0, chunk );
630 TRACE("Read %ld bytes\n",chunk->size);
632 *data = (LPVOID) chunk;
637 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
639 const int label_sz = sizeof volume->label/sizeof volume->label[0];
643 volume->serial = vol->dwVolSerial;
644 volume->type = vol->dwType;
646 if( !vol->dwVolLabelOfs )
648 if( vol->dwSize <= vol->dwVolLabelOfs )
650 len = vol->dwSize - vol->dwVolLabelOfs;
653 label += vol->dwVolLabelOfs;
654 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
659 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
664 while( p[len] && (len < maxlen) )
667 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
668 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
669 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
675 static HRESULT Stream_LoadLocation( IStream *stm,
676 volume_info *volume, LPWSTR *path )
683 r = Stream_ReadChunk( stm, (LPVOID*) &p );
687 loc = (LOCATION_INFO*) p;
688 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
690 HeapFree( GetProcessHeap(), 0, p );
694 /* if there's valid local volume information, load it */
695 if( loc->dwVolTableOfs &&
696 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
698 LOCAL_VOLUME_INFO *volume_info;
700 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
701 Stream_LoadVolume( volume_info, volume );
704 /* if there's a local path, load it */
705 n = loc->dwLocalPathOfs;
706 if( n && (n < loc->dwTotalSize) )
707 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
709 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
710 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
712 HeapFree( GetProcessHeap(), 0, p );
717 * The format of the advertised shortcut info seems to be:
722 * 0 Length of the block (4 bytes, usually 0x314)
724 * 8 string data in ASCII
725 * 8+0x104 string data in UNICODE
727 * In the original Win32 implementation the buffers are not initialized
728 * to zero, so data trailing the string is random garbage.
730 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
735 EXP_DARWIN_LINK buffer;
739 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
743 /* make sure that we read the size of the structure even on error */
744 size = sizeof buffer - sizeof (DWORD);
745 if( buffer.dbh.cbSize != sizeof buffer )
747 ERR("Ooops. This structure is not as expected...\n");
751 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
758 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
760 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
762 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
766 *str = HeapAlloc( GetProcessHeap(), 0,
767 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
768 lstrcpyW( *str, buffer.szwDarwinID );
773 /************************************************************************
774 * IPersistStream_Load (IPersistStream)
776 static HRESULT WINAPI IPersistStream_fnLoad(
777 IPersistStream* iface,
786 IShellLinkImpl *This = impl_from_IPersistStream(iface);
788 TRACE("%p %p\n", This, stm);
791 return STG_E_INVALIDPOINTER;
794 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
798 if( dwBytesRead != sizeof(hdr))
800 if( hdr.dwSize != sizeof(hdr))
802 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
805 /* free all the old stuff */
808 memset( &This->volume, 0, sizeof This->volume );
809 HeapFree(GetProcessHeap(), 0, This->sPath);
811 HeapFree(GetProcessHeap(), 0, This->sDescription);
812 This->sDescription = NULL;
813 HeapFree(GetProcessHeap(), 0, This->sPathRel);
814 This->sPathRel = NULL;
815 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
816 This->sWorkDir = NULL;
817 HeapFree(GetProcessHeap(), 0, This->sArgs);
819 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
820 This->sIcoPath = NULL;
821 HeapFree(GetProcessHeap(), 0, This->sProduct);
822 This->sProduct = NULL;
823 HeapFree(GetProcessHeap(), 0, This->sComponent);
824 This->sComponent = NULL;
826 This->wHotKey = (WORD)hdr.wHotKey;
827 This->iIcoNdx = hdr.nIcon;
828 FileTimeToSystemTime (&hdr.Time1, &This->time1);
829 FileTimeToSystemTime (&hdr.Time2, &This->time2);
830 FileTimeToSystemTime (&hdr.Time3, &This->time3);
833 WCHAR sTemp[MAX_PATH];
834 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
835 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
836 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
837 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
838 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
839 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
840 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
841 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
842 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
845 /* load all the new stuff */
846 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
848 r = ILLoadFromStream( stm, &This->pPidl );
854 /* load the location information */
855 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
856 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
860 unicode = hdr.dwFlags & SLDF_UNICODE;
861 if( hdr.dwFlags & SLDF_HAS_NAME )
863 r = Stream_LoadString( stm, unicode, &This->sDescription );
864 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
869 if( hdr.dwFlags & SLDF_HAS_RELPATH )
871 r = Stream_LoadString( stm, unicode, &This->sPathRel );
872 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
877 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
879 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
880 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
885 if( hdr.dwFlags & SLDF_HAS_ARGS )
887 r = Stream_LoadString( stm, unicode, &This->sArgs );
888 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
893 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
895 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
896 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
901 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
903 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
904 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
909 if( hdr.dwFlags & SLDF_HAS_DARWINID )
911 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
912 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
917 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
918 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
919 ERR("Last word was not zero\n");
930 /************************************************************************
933 * Helper function for IPersistStream_Save. Writes a unicode string
934 * with terminating nul byte to a stream, preceded by the its length.
936 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
938 USHORT len = lstrlenW( str ) + 1;
942 r = IStream_Write( stm, &len, sizeof(len), &count );
946 len *= sizeof(WCHAR);
948 r = IStream_Write( stm, str, len, &count );
955 /************************************************************************
956 * Stream_WriteLocationInfo
958 * Writes the location info to a stream
960 * FIXME: One day we might want to write the network volume information
961 * and the final path.
962 * Figure out how Windows deals with unicode paths here.
964 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
965 volume_info *volume )
967 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
968 LOCAL_VOLUME_INFO *vol;
970 LPSTR szLabel, szPath, szFinalPath;
973 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
975 /* figure out the size of everything */
976 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
977 NULL, 0, NULL, NULL );
978 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
979 NULL, 0, NULL, NULL );
980 volume_info_size = sizeof *vol + label_size;
982 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
984 /* create pointers to everything */
985 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
986 vol = (LOCAL_VOLUME_INFO*) &loc[1];
987 szLabel = (LPSTR) &vol[1];
988 szPath = &szLabel[label_size];
989 szFinalPath = &szPath[path_size];
991 /* fill in the location information header */
992 loc->dwTotalSize = total_size;
993 loc->dwHeaderSize = sizeof (*loc);
995 loc->dwVolTableOfs = sizeof (*loc);
996 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
997 loc->dwNetworkVolTableOfs = 0;
998 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1000 /* fill in the volume information */
1001 vol->dwSize = volume_info_size;
1002 vol->dwType = volume->type;
1003 vol->dwVolSerial = volume->serial;
1004 vol->dwVolLabelOfs = sizeof (*vol);
1006 /* copy in the strings */
1007 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1008 szLabel, label_size, NULL, NULL );
1009 WideCharToMultiByte( CP_ACP, 0, path, -1,
1010 szPath, path_size, NULL, NULL );
1013 return IStream_Write( stm, loc, total_size, &count );
1016 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1019 EXP_DARWIN_LINK buffer;
1023 memset( &buffer, 0, sizeof buffer );
1024 buffer.dbh.cbSize = sizeof buffer;
1025 buffer.dbh.dwSignature = magic;
1026 lstrcpynW( buffer.szwDarwinID, string, MAX_PATH );
1027 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.szDarwinID, MAX_PATH, NULL, NULL );
1029 return IStream_Write( stm, &buffer, buffer.dbh.cbSize, &count );
1032 /************************************************************************
1033 * IPersistStream_Save (IPersistStream)
1035 * FIXME: makes assumptions about byte order
1037 static HRESULT WINAPI IPersistStream_fnSave(
1038 IPersistStream* iface,
1042 static const WCHAR wOpen[] = {'o','p','e','n',0};
1045 WCHAR exePath[MAX_PATH];
1050 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1052 TRACE("%p %p %x\n", This, stm, fClearDirty);
1058 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
1059 NULL, NULL, NULL, NULL);
1061 * windows can create lnk files to executables that do not exist yet
1062 * so if the executable does not exist the just trust the path they
1065 if (!*exePath) lstrcpyW(exePath,This->sPath);
1068 memset(&header, 0, sizeof(header));
1069 header.dwSize = sizeof(header);
1070 header.fStartup = This->iShowCmd;
1071 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
1073 header.wHotKey = This->wHotKey;
1074 header.nIcon = This->iIcoNdx;
1075 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1077 header.dwFlags |= SLDF_HAS_ID_LIST;
1079 header.dwFlags |= SLDF_HAS_LINK_INFO;
1080 if( This->sDescription )
1081 header.dwFlags |= SLDF_HAS_NAME;
1082 if( This->sWorkDir )
1083 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1085 header.dwFlags |= SLDF_HAS_ARGS;
1086 if( This->sIcoPath )
1087 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1088 if( This->sProduct )
1089 header.dwFlags |= SLDF_HAS_LOGO3ID;
1090 if( This->sComponent )
1091 header.dwFlags |= SLDF_HAS_DARWINID;
1093 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1094 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1095 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1097 /* write the Shortcut header */
1098 r = IStream_Write( stm, &header, sizeof(header), &count );
1101 ERR("Write failed at %d\n",__LINE__);
1105 TRACE("Writing pidl \n");
1107 /* write the PIDL to the shortcut */
1110 r = ILSaveToStream( stm, This->pPidl );
1113 ERR("Failed to write PIDL at %d\n",__LINE__);
1119 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1121 if( This->sDescription )
1122 r = Stream_WriteString( stm, This->sDescription );
1124 if( This->sPathRel )
1125 r = Stream_WriteString( stm, This->sPathRel );
1127 if( This->sWorkDir )
1128 r = Stream_WriteString( stm, This->sWorkDir );
1131 r = Stream_WriteString( stm, This->sArgs );
1133 if( This->sIcoPath )
1134 r = Stream_WriteString( stm, This->sIcoPath );
1136 if( This->sProduct )
1137 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1139 if( This->sComponent )
1140 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1142 /* the last field is a single zero dword */
1144 r = IStream_Write( stm, &zero, sizeof zero, &count );
1149 /************************************************************************
1150 * IPersistStream_GetSizeMax (IPersistStream)
1152 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1153 IPersistStream* iface,
1154 ULARGE_INTEGER* pcbSize)
1156 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1158 TRACE("(%p)\n", This);
1163 static const IPersistStreamVtbl psvt =
1165 IPersistStream_fnQueryInterface,
1166 IPersistStream_fnAddRef,
1167 IPersistStream_fnRelease,
1168 IPersistStream_fnGetClassID,
1169 IPersistStream_fnIsDirty,
1170 IPersistStream_fnLoad,
1171 IPersistStream_fnSave,
1172 IPersistStream_fnGetSizeMax
1175 /**************************************************************************
1176 * IShellLink_Constructor
1178 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1179 REFIID riid, LPVOID *ppv )
1181 IShellLinkImpl * sl;
1184 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1189 return CLASS_E_NOAGGREGATION;
1190 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1192 return E_OUTOFMEMORY;
1196 sl->lpvtblw = &slvtw;
1197 sl->lpvtblPersistFile = &pfvt;
1198 sl->lpvtblPersistStream = &psvt;
1199 sl->lpvtblShellLinkDataList = &dlvt;
1200 sl->lpvtblShellExtInit = &eivt;
1201 sl->lpvtblContextMenu = &cmvt;
1202 sl->lpvtblObjectWithSite = &owsvt;
1203 sl->iShowCmd = SW_SHOWNORMAL;
1208 TRACE("(%p)->()\n",sl);
1210 r = ShellLink_QueryInterface( sl, riid, ppv );
1211 ShellLink_Release( sl );
1216 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1218 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1223 /**************************************************************************
1224 * ShellLink_UpdatePath
1225 * update absolute path in sPath using relative path in sPathRel
1227 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1229 if (!path || !psPath)
1230 return E_INVALIDARG;
1232 if (!*psPath && sPathRel) {
1233 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1234 LPWSTR final = NULL;
1236 /* first try if [directory of link file] + [relative path] finds an existing file */
1238 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1241 lstrcpyW(final, sPathRel);
1245 if (SHELL_ExistsFileW(buffer)) {
1246 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1247 lstrcpyW(abs_path, buffer);
1249 /* try if [working directory] + [relative path] finds an existing file */
1251 lstrcpyW(buffer, sWorkDir);
1252 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1254 if (SHELL_ExistsFileW(buffer))
1255 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1256 lstrcpyW(abs_path, buffer);
1260 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1262 lstrcpyW(abs_path, sPathRel);
1264 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1266 return E_OUTOFMEMORY;
1268 lstrcpyW(*psPath, abs_path);
1274 /**************************************************************************
1275 * IShellLink_ConstructFromFile
1277 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1278 LPCITEMIDLIST pidl, LPVOID* ppv)
1282 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1284 if (SUCCEEDED(hr)) {
1289 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1291 if (SUCCEEDED(hr)) {
1292 WCHAR path[MAX_PATH];
1294 if (SHGetPathFromIDListW(pidl, path))
1295 hr = IPersistFile_Load(ppf, path, 0);
1300 *ppv = (IUnknown*) psl;
1302 IPersistFile_Release(ppf);
1306 IShellLinkW_Release(psl);
1312 /**************************************************************************
1313 * IShellLinkA_QueryInterface
1315 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1317 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1318 return ShellLink_QueryInterface( This, riid, ppvObj );
1321 /******************************************************************************
1322 * IShellLinkA_AddRef
1324 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1326 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1327 return ShellLink_AddRef( This );
1330 /******************************************************************************
1331 * IShellLinkA_Release
1333 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1335 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1336 return ShellLink_Release( This );
1339 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1340 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1342 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1344 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1345 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1347 if (This->sComponent || This->sProduct)
1353 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1354 pszFile, cchMaxPath, NULL, NULL);
1356 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1361 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1363 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1365 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1367 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1370 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1372 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1374 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1377 ILFree(This->pPidl);
1378 This->pPidl = ILClone (pidl);
1379 This->bDirty = TRUE;
1384 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1386 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1388 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1392 if( This->sDescription )
1393 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1394 pszName, cchMaxName, NULL, NULL);
1399 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1401 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1403 TRACE("(%p)->(pName=%s)\n", This, pszName);
1405 HeapFree(GetProcessHeap(), 0, This->sDescription);
1406 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1407 if ( !This->sDescription )
1408 return E_OUTOFMEMORY;
1410 This->bDirty = TRUE;
1415 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1417 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1419 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1423 if( This->sWorkDir )
1424 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1425 pszDir, cchMaxPath, NULL, NULL);
1430 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1432 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1434 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1436 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1437 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1438 if ( !This->sWorkDir )
1439 return E_OUTOFMEMORY;
1441 This->bDirty = TRUE;
1446 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1448 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1450 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1455 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1456 pszArgs, cchMaxPath, NULL, NULL);
1461 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1463 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1465 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1467 HeapFree(GetProcessHeap(), 0, This->sArgs);
1468 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1470 return E_OUTOFMEMORY;
1472 This->bDirty = TRUE;
1477 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1479 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1481 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1483 *pwHotkey = This->wHotKey;
1488 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1490 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1492 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1494 This->wHotKey = wHotkey;
1495 This->bDirty = TRUE;
1500 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1502 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1504 TRACE("(%p)->(%p)\n",This, piShowCmd);
1505 *piShowCmd = This->iShowCmd;
1509 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1511 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1513 TRACE("(%p) %d\n",This, iShowCmd);
1515 This->iShowCmd = iShowCmd;
1516 This->bDirty = TRUE;
1521 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1523 LPCITEMIDLIST pidlLast;
1525 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1527 if (SUCCEEDED(hr)) {
1530 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1532 if (SUCCEEDED(hr)) {
1533 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1535 IExtractIconA_Release(pei);
1538 IShellFolder_Release(psf);
1544 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1546 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1548 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1551 *piIcon = This->iIcoNdx;
1555 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1559 if (This->pPidl || This->sPath)
1563 HRESULT hr = SHGetDesktopFolder(&pdsk);
1567 /* first look for an icon using the PIDL (if present) */
1569 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1573 /* if we couldn't find an icon yet, look for it using the file system path */
1574 if (FAILED(hr) && This->sPath)
1578 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1580 if (SUCCEEDED(hr)) {
1581 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1587 IShellFolder_Release(pdsk);
1595 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1597 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1599 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1601 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1602 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1603 if ( !This->sIcoPath )
1604 return E_OUTOFMEMORY;
1606 This->iIcoNdx = iIcon;
1607 This->bDirty = TRUE;
1612 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1614 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1616 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1618 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1619 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1620 This->bDirty = TRUE;
1622 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1625 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1627 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1629 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1631 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1634 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1638 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1640 TRACE("(%p)->(path=%s)\n",This, pszFile);
1642 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1644 return E_OUTOFMEMORY;
1646 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1647 HeapFree( GetProcessHeap(), 0, str );
1652 /**************************************************************************
1653 * IShellLink Implementation
1656 static const IShellLinkAVtbl slvt =
1658 IShellLinkA_fnQueryInterface,
1659 IShellLinkA_fnAddRef,
1660 IShellLinkA_fnRelease,
1661 IShellLinkA_fnGetPath,
1662 IShellLinkA_fnGetIDList,
1663 IShellLinkA_fnSetIDList,
1664 IShellLinkA_fnGetDescription,
1665 IShellLinkA_fnSetDescription,
1666 IShellLinkA_fnGetWorkingDirectory,
1667 IShellLinkA_fnSetWorkingDirectory,
1668 IShellLinkA_fnGetArguments,
1669 IShellLinkA_fnSetArguments,
1670 IShellLinkA_fnGetHotkey,
1671 IShellLinkA_fnSetHotkey,
1672 IShellLinkA_fnGetShowCmd,
1673 IShellLinkA_fnSetShowCmd,
1674 IShellLinkA_fnGetIconLocation,
1675 IShellLinkA_fnSetIconLocation,
1676 IShellLinkA_fnSetRelativePath,
1677 IShellLinkA_fnResolve,
1678 IShellLinkA_fnSetPath
1682 /**************************************************************************
1683 * IShellLinkW_fnQueryInterface
1685 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1686 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1688 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1689 return ShellLink_QueryInterface( This, riid, ppvObj );
1692 /******************************************************************************
1693 * IShellLinkW_fnAddRef
1695 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1697 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1698 return ShellLink_AddRef( This );
1701 /******************************************************************************
1702 * IShellLinkW_fnRelease
1704 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1706 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1707 return ShellLink_Release( This );
1710 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1712 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1714 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1715 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1717 if (This->sComponent || This->sProduct)
1723 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1725 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1730 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1732 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1734 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1738 *ppidl = ILClone(This->pPidl);
1742 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1744 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1746 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1749 ILFree( This->pPidl );
1750 This->pPidl = ILClone( pidl );
1754 This->bDirty = TRUE;
1759 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1761 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1763 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1766 if( This->sDescription )
1767 lstrcpynW( pszName, This->sDescription, cchMaxName );
1772 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1774 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1776 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1778 HeapFree(GetProcessHeap(), 0, This->sDescription);
1779 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1780 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1781 if ( !This->sDescription )
1782 return E_OUTOFMEMORY;
1784 lstrcpyW( This->sDescription, pszName );
1785 This->bDirty = TRUE;
1790 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1792 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1794 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1798 if( This->sWorkDir )
1799 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1804 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1806 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1808 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1810 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1811 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1812 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1813 if ( !This->sWorkDir )
1814 return E_OUTOFMEMORY;
1815 lstrcpyW( This->sWorkDir, pszDir );
1816 This->bDirty = TRUE;
1821 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1823 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1825 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1830 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1835 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1837 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1839 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1841 HeapFree(GetProcessHeap(), 0, This->sArgs);
1842 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1843 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1845 return E_OUTOFMEMORY;
1846 lstrcpyW( This->sArgs, pszArgs );
1847 This->bDirty = TRUE;
1852 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1854 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1856 TRACE("(%p)->(%p)\n",This, pwHotkey);
1858 *pwHotkey=This->wHotKey;
1863 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1865 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1867 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1869 This->wHotKey = wHotkey;
1870 This->bDirty = TRUE;
1875 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1877 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1879 TRACE("(%p)->(%p)\n",This, piShowCmd);
1881 *piShowCmd = This->iShowCmd;
1886 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1888 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1890 This->iShowCmd = iShowCmd;
1891 This->bDirty = TRUE;
1896 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1898 LPCITEMIDLIST pidlLast;
1900 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1902 if (SUCCEEDED(hr)) {
1905 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1907 if (SUCCEEDED(hr)) {
1908 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1910 IExtractIconW_Release(pei);
1913 IShellFolder_Release(psf);
1919 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1921 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1923 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1926 *piIcon = This->iIcoNdx;
1930 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1934 if (This->pPidl || This->sPath)
1938 HRESULT hr = SHGetDesktopFolder(&pdsk);
1942 /* first look for an icon using the PIDL (if present) */
1944 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1948 /* if we couldn't find an icon yet, look for it using the file system path */
1949 if (FAILED(hr) && This->sPath)
1953 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1957 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1963 IShellFolder_Release(pdsk);
1970 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1972 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1974 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1976 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1977 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1978 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1979 if ( !This->sIcoPath )
1980 return E_OUTOFMEMORY;
1981 lstrcpyW( This->sIcoPath, pszIconPath );
1983 This->iIcoNdx = iIcon;
1984 This->bDirty = TRUE;
1989 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1991 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1993 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1995 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1996 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1997 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1998 if ( !This->sPathRel )
1999 return E_OUTOFMEMORY;
2000 lstrcpyW( This->sPathRel, pszPathRel );
2001 This->bDirty = TRUE;
2003 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2006 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2011 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2013 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
2015 /*FIXME: use IResolveShellLink interface */
2017 if (!This->sPath && This->pPidl) {
2018 WCHAR buffer[MAX_PATH];
2020 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2022 if (bSuccess && *buffer) {
2023 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2025 return E_OUTOFMEMORY;
2027 lstrcpyW(This->sPath, buffer);
2029 This->bDirty = TRUE;
2031 hr = S_OK; /* don't report an error occurred while just caching information */
2034 if (!This->sIcoPath && This->sPath) {
2035 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2036 if (!This->sIcoPath)
2037 return E_OUTOFMEMORY;
2039 lstrcpyW(This->sIcoPath, This->sPath);
2042 This->bDirty = TRUE;
2048 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2057 p = strchrW( str, ':' );
2061 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2064 memcpy( ret, str, sizeof(WCHAR)*len );
2069 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2071 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2079 /* each segment must start with two colons */
2080 if( str[0] != ':' || str[1] != ':' )
2083 /* the last segment is just two colons */
2088 /* there must be a colon straight after a guid */
2089 p = strchrW( str, ':' );
2096 /* get the guid, and check it's validly formatted */
2097 memcpy( szGuid, str, sizeof(WCHAR)*len );
2099 r = CLSIDFromString( szGuid, &guid );
2104 /* match it up to a guid that we care about */
2105 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2107 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2112 /* skip to the next field */
2113 str = strchrW( str, ':' );
2118 /* we have to have a component for an advertised shortcut */
2122 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2123 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2125 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2126 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2131 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2133 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2134 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2137 volume->type = GetDriveTypeW(drive);
2138 r = GetVolumeInformationW(drive, volume->label, label_sz,
2139 &volume->serial, NULL, NULL, NULL, 0);
2140 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2141 volume->type, volume->serial, debugstr_w(volume->label));
2145 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2147 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2148 WCHAR buffer[MAX_PATH];
2152 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2154 HeapFree(GetProcessHeap(), 0, This->sPath);
2157 HeapFree(GetProcessHeap(), 0, This->sComponent);
2158 This->sComponent = NULL;
2161 ILFree(This->pPidl);
2164 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2166 if (*pszFile == '\0')
2168 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2170 else if(!PathFileExistsW(buffer))
2173 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2174 ShellLink_GetVolumeInfo(buffer, &This->volume);
2176 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2177 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2179 return E_OUTOFMEMORY;
2181 lstrcpyW(This->sPath, buffer);
2183 This->bDirty = TRUE;
2188 /**************************************************************************
2189 * IShellLinkW Implementation
2192 static const IShellLinkWVtbl slvtw =
2194 IShellLinkW_fnQueryInterface,
2195 IShellLinkW_fnAddRef,
2196 IShellLinkW_fnRelease,
2197 IShellLinkW_fnGetPath,
2198 IShellLinkW_fnGetIDList,
2199 IShellLinkW_fnSetIDList,
2200 IShellLinkW_fnGetDescription,
2201 IShellLinkW_fnSetDescription,
2202 IShellLinkW_fnGetWorkingDirectory,
2203 IShellLinkW_fnSetWorkingDirectory,
2204 IShellLinkW_fnGetArguments,
2205 IShellLinkW_fnSetArguments,
2206 IShellLinkW_fnGetHotkey,
2207 IShellLinkW_fnSetHotkey,
2208 IShellLinkW_fnGetShowCmd,
2209 IShellLinkW_fnSetShowCmd,
2210 IShellLinkW_fnGetIconLocation,
2211 IShellLinkW_fnSetIconLocation,
2212 IShellLinkW_fnSetRelativePath,
2213 IShellLinkW_fnResolve,
2214 IShellLinkW_fnSetPath
2217 static HRESULT WINAPI
2218 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2220 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2221 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2225 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2227 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2228 return IShellLinkA_AddRef((IShellLinkA*)This);
2232 ShellLink_DataList_Release( IShellLinkDataList* iface )
2234 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2235 return ShellLink_Release( This );
2238 static HRESULT WINAPI
2239 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2245 static HRESULT WINAPI
2246 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2252 static HRESULT WINAPI
2253 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2259 static HRESULT WINAPI
2260 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2266 static HRESULT WINAPI
2267 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2273 static const IShellLinkDataListVtbl dlvt =
2275 ShellLink_DataList_QueryInterface,
2276 ShellLink_DataList_AddRef,
2277 ShellLink_DataList_Release,
2278 ShellLink_AddDataBlock,
2279 ShellLink_CopyDataBlock,
2280 ShellLink_RemoveDataBlock,
2285 static HRESULT WINAPI
2286 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2288 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2289 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2293 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2295 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2296 return IShellLinkA_AddRef((IShellLinkA*)This);
2300 ShellLink_ExtInit_Release( IShellExtInit* iface )
2302 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2303 return ShellLink_Release( This );
2306 /**************************************************************************
2307 * ShellLink implementation of IShellExtInit::Initialize()
2309 * Loads the shelllink from the dataobject the shell is pointing to.
2311 static HRESULT WINAPI
2312 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2313 IDataObject *pdtobj, HKEY hkeyProgID )
2315 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2321 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2326 format.cfFormat = CF_HDROP;
2328 format.dwAspect = DVASPECT_CONTENT;
2330 format.tymed = TYMED_HGLOBAL;
2332 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2335 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2340 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2342 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2345 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2347 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2348 r = IPersistFile_Load( pf, path, 0 );
2349 HeapFree( GetProcessHeap(), 0, path );
2352 ReleaseStgMedium( &stgm );
2357 static const IShellExtInitVtbl eivt =
2359 ShellLink_ExtInit_QueryInterface,
2360 ShellLink_ExtInit_AddRef,
2361 ShellLink_ExtInit_Release,
2362 ShellLink_ExtInit_Initialize
2365 static HRESULT WINAPI
2366 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2368 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2369 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2373 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2375 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2376 return IShellLinkA_AddRef((IShellLinkA*)This);
2380 ShellLink_ContextMenu_Release( IContextMenu* iface )
2382 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2383 return ShellLink_Release( This );
2386 static HRESULT WINAPI
2387 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2388 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2390 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2391 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2395 TRACE("%p %p %u %u %u %u\n", This,
2396 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2399 return E_INVALIDARG;
2401 memset( &mii, 0, sizeof mii );
2402 mii.cbSize = sizeof mii;
2403 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2404 mii.dwTypeData = (LPWSTR)szOpen;
2405 mii.cch = strlenW( mii.dwTypeData );
2406 mii.wID = idCmdFirst + id++;
2407 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2408 mii.fType = MFT_STRING;
2409 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2413 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2416 static LPWSTR shelllink_get_msi_component_path( LPCWSTR component )
2418 UINT WINAPI (*pMsiDecomposeDescriptorW)(LPCWSTR,LPWSTR,LPWSTR,LPWSTR,DWORD*);
2419 INSTALLSTATE WINAPI (*pMsiGetComponentPathW)(LPCWSTR,LPCWSTR,LPWSTR,DWORD*);
2420 WCHAR szProd[MAX_FEATURE_CHARS+1], szFeat[MAX_FEATURE_CHARS+1],
2421 szComp[MAX_FEATURE_CHARS+1], szCompPath[MAX_PATH];
2424 HMODULE hmsi = NULL;
2428 TRACE("%s\n", debugstr_w( component ) );
2430 hmsi = LoadLibraryA("msi");
2434 pMsiDecomposeDescriptorW = (LPVOID) GetProcAddress(hmsi, "MsiDecomposeDescriptorW");
2435 pMsiGetComponentPathW = (LPVOID) GetProcAddress(hmsi, "MsiGetComponentPathW");
2436 if (!pMsiDecomposeDescriptorW || !pMsiGetComponentPathW)
2439 ret = pMsiDecomposeDescriptorW( component, szProd, szFeat, szComp, &sz );
2440 if (ret != ERROR_SUCCESS)
2442 ERR("failed to decompose descriptor %s\n", debugstr_w( component ) );
2447 state = pMsiGetComponentPathW( szProd, szComp, szCompPath, &sz );
2448 if (state != INSTALLSTATE_LOCAL)
2450 ERR("MsiGetComponentPathW failed with error %d\n", ret );
2454 path = strdupW( szCompPath );
2458 FreeLibrary( hmsi );
2460 TRACE("returning %s\n", debugstr_w( path ) );
2465 static HRESULT WINAPI
2466 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2468 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2469 SHELLEXECUTEINFOW sei;
2470 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2475 TRACE("%p %p\n", This, lpici );
2477 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2478 return E_INVALIDARG;
2480 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2482 ERR("Unknown id %d != %d\n", (INT)lpici->lpVerb, This->iIdOpen );
2483 return E_INVALIDARG;
2486 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2490 if ( This->sComponent )
2492 path = shelllink_get_msi_component_path( This->sComponent );
2497 path = strdupW( This->sPath );
2499 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2500 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2502 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2506 len += lstrlenW( This->sArgs );
2507 if ( iciex->lpParametersW )
2508 len += lstrlenW( iciex->lpParametersW );
2510 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2513 lstrcatW( args, This->sArgs );
2514 if ( iciex->lpParametersW )
2515 lstrcatW( args, iciex->lpParametersW );
2518 memset( &sei, 0, sizeof sei );
2519 sei.cbSize = sizeof sei;
2521 sei.nShow = This->iShowCmd;
2522 sei.lpIDList = This->pPidl;
2523 sei.lpDirectory = This->sWorkDir;
2524 sei.lpParameters = args;
2526 if( ShellExecuteExW( &sei ) )
2531 HeapFree( GetProcessHeap(), 0, args );
2532 HeapFree( GetProcessHeap(), 0, path );
2537 static HRESULT WINAPI
2538 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2539 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2541 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2543 FIXME("%p %u %u %p %p %u\n", This,
2544 idCmd, uType, pwReserved, pszName, cchMax );
2549 static const IContextMenuVtbl cmvt =
2551 ShellLink_ContextMenu_QueryInterface,
2552 ShellLink_ContextMenu_AddRef,
2553 ShellLink_ContextMenu_Release,
2554 ShellLink_QueryContextMenu,
2555 ShellLink_InvokeCommand,
2556 ShellLink_GetCommandString
2559 static HRESULT WINAPI
2560 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2562 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2563 return ShellLink_QueryInterface( This, riid, ppvObject );
2567 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2569 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2570 return ShellLink_AddRef( This );
2574 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2576 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2577 return ShellLink_Release( This );
2580 static HRESULT WINAPI
2581 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2583 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2585 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2589 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2592 static HRESULT WINAPI
2593 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2595 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2597 TRACE("%p %p\n", iface, punk);
2600 IUnknown_AddRef( punk );
2606 static const IObjectWithSiteVtbl owsvt =
2608 ShellLink_ObjectWithSite_QueryInterface,
2609 ShellLink_ObjectWithSite_AddRef,
2610 ShellLink_ObjectWithSite_Release,