mf/session: Forward more events to the application.
[wine/zf.git] / dlls / shell32 / shelllink.c
blob69d610f25030eb4445a7bbf279c0ad5e75b0dafa
1 /*
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * NOTES
22 * Nearly complete information about the binary formats
23 * of .lnk files available at http://www.wotsit.org
25 * You can use winedump to examine the contents of a link file:
26 * winedump lnk sc.lnk
28 * MSI advertised shortcuts are totally undocumented. They provide an
29 * icon for a program that is not yet installed, and invoke MSI to
30 * install the program when the shortcut is clicked on. They are
31 * created by passing a special string to SetPath, and the information
32 * in that string is parsed an stored.
35 #define COBJMACROS
36 #define NONAMELESSUNION
38 #include "wine/debug.h"
39 #include "winerror.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winreg.h"
45 #include "winuser.h"
46 #include "wingdi.h"
47 #include "shlobj.h"
48 #include "undocshell.h"
50 #include "pidl.h"
51 #include "shell32_main.h"
52 #include "shlguid.h"
53 #include "shlwapi.h"
54 #include "msi.h"
55 #include "appmgmt.h"
57 #include "initguid.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(shell);
61 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
62 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
64 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
66 /* link file formats */
68 #include "pshpack1.h"
70 typedef struct _LINK_HEADER
72 DWORD dwSize; /* 0x00 size of the header - 0x4c */
73 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
74 DWORD dwFlags; /* 0x14 describes elements following */
75 DWORD dwFileAttr; /* 0x18 attributes of the target file */
76 FILETIME CreationTime; /* 0x1c creation time of target file */
77 FILETIME AccessTime; /* 0x24 access time of target file */
78 FILETIME WriteTime; /* 0x2c write time of target file */
79 DWORD dwFileSize; /* 0x34 File size of target file */
80 DWORD nIcon; /* 0x38 icon number or index */
81 DWORD fStartup; /* 0x3c startup type or window state of application */
82 WORD wHotKey; /* 0x40 hotkey */
83 WORD Reserved1; /* 0x42 reserved = 0 */
84 DWORD Reserved2; /* 0x44 reserved = 0 */
85 DWORD Reserved3; /* 0x48 reserved = 0 */
86 } LINK_HEADER, * PLINK_HEADER;
88 #define SHLINK_LOCAL 0
89 #define SHLINK_REMOTE 1
91 typedef struct _LOCATION_INFO
93 DWORD dwTotalSize;
94 DWORD dwHeaderSize;
95 DWORD dwFlags;
96 DWORD dwVolTableOfs;
97 DWORD dwLocalPathOfs;
98 DWORD dwNetworkVolTableOfs;
99 DWORD dwFinalPathOfs;
100 } LOCATION_INFO;
102 typedef struct _LOCAL_VOLUME_INFO
104 DWORD dwSize;
105 DWORD dwType;
106 DWORD dwVolSerial;
107 DWORD dwVolLabelOfs;
108 } LOCAL_VOLUME_INFO;
110 typedef struct volume_info_t
112 DWORD type;
113 DWORD serial;
114 WCHAR label[12]; /* assume 8.3 */
115 } volume_info;
117 #include "poppack.h"
119 /* IShellLink Implementation */
121 typedef struct
123 IShellLinkA IShellLinkA_iface;
124 IShellLinkW IShellLinkW_iface;
125 IPersistFile IPersistFile_iface;
126 IPersistStream IPersistStream_iface;
127 IShellLinkDataList IShellLinkDataList_iface;
128 IShellExtInit IShellExtInit_iface;
129 IContextMenu IContextMenu_iface;
130 IObjectWithSite IObjectWithSite_iface;
131 IPropertyStore IPropertyStore_iface;
133 LONG ref;
135 /* data structures according to the information in the link */
136 LPITEMIDLIST pPidl;
137 WORD wHotKey;
138 SYSTEMTIME CreationTime;
139 SYSTEMTIME AccessTime;
140 SYSTEMTIME WriteTime;
142 DWORD iShowCmd;
143 LPWSTR sIcoPath;
144 INT iIcoNdx;
145 LPWSTR sPath;
146 LPWSTR sArgs;
147 LPWSTR sWorkDir;
148 LPWSTR sDescription;
149 LPWSTR sPathRel;
150 LPWSTR sProduct;
151 LPWSTR sComponent;
152 volume_info volume;
154 BOOL bDirty;
155 INT iIdOpen; /* id of the "Open" entry in the context menu */
156 IUnknown *site;
158 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
159 } IShellLinkImpl;
161 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
163 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
166 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
168 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
171 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
173 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
176 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
178 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
181 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
183 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
186 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
188 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
191 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
193 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
196 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
198 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
201 static inline IShellLinkImpl *impl_from_IPropertyStore(IPropertyStore *iface)
203 return CONTAINING_RECORD(iface, IShellLinkImpl, IPropertyStore_iface);
206 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
208 /* strdup on the process heap */
209 static inline LPWSTR heap_strdupAtoW( LPCSTR str)
211 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
212 LPWSTR p = heap_alloc( len*sizeof (WCHAR) );
213 if( !p )
214 return p;
215 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
216 return p;
219 /**************************************************************************
220 * IPersistFile_QueryInterface
222 static HRESULT WINAPI IPersistFile_fnQueryInterface(
223 IPersistFile* iface,
224 REFIID riid,
225 LPVOID *ppvObj)
227 IShellLinkImpl *This = impl_from_IPersistFile(iface);
228 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
231 /******************************************************************************
232 * IPersistFile_AddRef
234 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
236 IShellLinkImpl *This = impl_from_IPersistFile(iface);
237 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
240 /******************************************************************************
241 * IPersistFile_Release
243 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
245 IShellLinkImpl *This = impl_from_IPersistFile(iface);
246 return IShellLinkW_Release(&This->IShellLinkW_iface);
249 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
251 IShellLinkImpl *This = impl_from_IPersistFile(iface);
253 TRACE("(%p)->(%p)\n", This, pClassID);
255 *pClassID = CLSID_ShellLink;
257 return S_OK;
260 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
262 IShellLinkImpl *This = impl_from_IPersistFile(iface);
264 TRACE("(%p)\n",This);
266 if (This->bDirty)
267 return S_OK;
269 return S_FALSE;
272 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
274 IShellLinkImpl *This = impl_from_IPersistFile(iface);
275 IPersistStream *StreamThis = &This->IPersistStream_iface;
276 HRESULT r;
277 IStream *stm;
279 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
281 if( dwMode == 0 )
282 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
283 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
284 if( SUCCEEDED( r ) )
286 r = IPersistStream_Load(StreamThis, stm);
287 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
288 IStream_Release( stm );
290 /* update file path */
291 heap_free(This->filepath);
292 This->filepath = strdupW(pszFileName);
294 This->bDirty = FALSE;
296 TRACE("-- returning hr %08x\n", r);
297 return r;
300 BOOL run_winemenubuilder( const WCHAR *args )
302 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
303 LONG len;
304 LPWSTR buffer;
305 STARTUPINFOW si;
306 PROCESS_INFORMATION pi;
307 BOOL ret;
308 WCHAR app[MAX_PATH];
309 void *redir;
311 GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE(menubuilder) );
312 strcatW( app, menubuilder );
314 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
315 buffer = heap_alloc( len );
316 if( !buffer )
317 return FALSE;
319 strcpyW( buffer, app );
320 strcatW( buffer, args );
322 TRACE("starting %s\n",debugstr_w(buffer));
324 memset(&si, 0, sizeof(si));
325 si.cb = sizeof(si);
327 Wow64DisableWow64FsRedirection( &redir );
328 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
329 Wow64RevertWow64FsRedirection( redir );
331 heap_free( buffer );
333 if (ret)
335 CloseHandle( pi.hProcess );
336 CloseHandle( pi.hThread );
339 return ret;
342 static BOOL StartLinkProcessor( LPCOLESTR szLink )
344 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
345 LONG len;
346 LPWSTR buffer;
347 BOOL ret;
349 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
350 buffer = heap_alloc( len );
351 if( !buffer )
352 return FALSE;
354 wsprintfW( buffer, szFormat, szLink );
355 ret = run_winemenubuilder( buffer );
356 heap_free( buffer );
357 return ret;
360 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
362 IShellLinkImpl *This = impl_from_IPersistFile(iface);
363 IPersistStream *StreamThis = &This->IPersistStream_iface;
364 HRESULT r;
365 IStream *stm;
367 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
369 if (!pszFileName)
371 if (!This->filepath) return S_OK;
373 pszFileName = This->filepath;
374 fRemember = FALSE;
377 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
378 if( SUCCEEDED( r ) )
380 r = IPersistStream_Save(StreamThis, stm, FALSE);
381 IStream_Release( stm );
383 if( SUCCEEDED( r ) )
385 StartLinkProcessor( pszFileName );
387 if (fRemember)
389 /* update file path */
390 heap_free(This->filepath);
391 This->filepath = strdupW(pszFileName);
394 This->bDirty = FALSE;
396 else
398 DeleteFileW( pszFileName );
399 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
403 return r;
406 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
408 IShellLinkImpl *This = impl_from_IPersistFile(iface);
409 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
410 return S_OK;
413 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
415 IShellLinkImpl *This = impl_from_IPersistFile(iface);
417 TRACE("(%p)->(%p)\n", This, filename);
419 if (!This->filepath)
421 *filename = NULL;
422 return S_FALSE;
425 *filename = CoTaskMemAlloc((strlenW(This->filepath) + 1) * sizeof(WCHAR));
426 if (!*filename) return E_OUTOFMEMORY;
428 strcpyW(*filename, This->filepath);
430 return S_OK;
433 static const IPersistFileVtbl pfvt =
435 IPersistFile_fnQueryInterface,
436 IPersistFile_fnAddRef,
437 IPersistFile_fnRelease,
438 IPersistFile_fnGetClassID,
439 IPersistFile_fnIsDirty,
440 IPersistFile_fnLoad,
441 IPersistFile_fnSave,
442 IPersistFile_fnSaveCompleted,
443 IPersistFile_fnGetCurFile
446 /************************************************************************
447 * IPersistStream_QueryInterface
449 static HRESULT WINAPI IPersistStream_fnQueryInterface(
450 IPersistStream* iface,
451 REFIID riid,
452 VOID** ppvObj)
454 IShellLinkImpl *This = impl_from_IPersistStream(iface);
455 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
458 /************************************************************************
459 * IPersistStream_Release
461 static ULONG WINAPI IPersistStream_fnRelease(
462 IPersistStream* iface)
464 IShellLinkImpl *This = impl_from_IPersistStream(iface);
465 return IShellLinkW_Release(&This->IShellLinkW_iface);
468 /************************************************************************
469 * IPersistStream_AddRef
471 static ULONG WINAPI IPersistStream_fnAddRef(
472 IPersistStream* iface)
474 IShellLinkImpl *This = impl_from_IPersistStream(iface);
475 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
478 /************************************************************************
479 * IPersistStream_GetClassID
482 static HRESULT WINAPI IPersistStream_fnGetClassID(
483 IPersistStream* iface,
484 CLSID* pClassID)
486 IShellLinkImpl *This = impl_from_IPersistStream(iface);
487 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
490 /************************************************************************
491 * IPersistStream_IsDirty (IPersistStream)
493 static HRESULT WINAPI IPersistStream_fnIsDirty(
494 IPersistStream* iface)
496 IShellLinkImpl *This = impl_from_IPersistStream(iface);
498 TRACE("(%p)\n", This);
500 return S_OK;
504 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
506 DWORD count;
507 USHORT len;
508 LPVOID temp;
509 LPWSTR str;
510 HRESULT r;
512 TRACE("%p\n", stm);
514 count = 0;
515 r = IStream_Read(stm, &len, sizeof(len), &count);
516 if ( FAILED (r) || ( count != sizeof(len) ) )
517 return E_FAIL;
519 if( unicode )
520 len *= sizeof (WCHAR);
522 TRACE("reading %d\n", len);
523 temp = heap_alloc(len + sizeof(WCHAR));
524 if( !temp )
525 return E_OUTOFMEMORY;
526 count = 0;
527 r = IStream_Read(stm, temp, len, &count);
528 if( FAILED (r) || ( count != len ) )
530 heap_free( temp );
531 return E_FAIL;
534 TRACE("read %s\n", debugstr_an(temp,len));
536 /* convert to unicode if necessary */
537 if( !unicode )
539 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
540 str = heap_alloc( (count+1)*sizeof (WCHAR) );
541 if( !str )
543 heap_free( temp );
544 return E_OUTOFMEMORY;
546 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
547 heap_free( temp );
549 else
551 count /= 2;
552 str = temp;
554 str[count] = 0;
556 *pstr = str;
558 return S_OK;
561 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
563 DWORD size;
564 ULONG count;
565 HRESULT r;
566 struct sized_chunk {
567 DWORD size;
568 unsigned char data[1];
569 } *chunk;
571 TRACE("%p\n",stm);
573 r = IStream_Read( stm, &size, sizeof(size), &count );
574 if( FAILED( r ) || count != sizeof(size) )
575 return E_FAIL;
577 chunk = heap_alloc( size );
578 if( !chunk )
579 return E_OUTOFMEMORY;
581 chunk->size = size;
582 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
583 if( FAILED( r ) || count != (size - sizeof(size)) )
585 heap_free( chunk );
586 return E_FAIL;
589 TRACE("Read %d bytes\n",chunk->size);
591 *data = chunk;
593 return S_OK;
596 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
598 const int label_sz = ARRAY_SIZE(volume->label);
599 LPSTR label;
600 int len;
602 volume->serial = vol->dwVolSerial;
603 volume->type = vol->dwType;
605 if( !vol->dwVolLabelOfs )
606 return FALSE;
607 if( vol->dwSize <= vol->dwVolLabelOfs )
608 return FALSE;
609 len = vol->dwSize - vol->dwVolLabelOfs;
611 label = (LPSTR) vol;
612 label += vol->dwVolLabelOfs;
613 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
615 return TRUE;
618 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
620 int len = 0, wlen;
621 LPWSTR path;
623 while( (len < maxlen) && p[len] )
624 len++;
626 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
627 path = heap_alloc((wlen + 1) * sizeof(WCHAR));
628 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
629 path[wlen] = 0;
631 return path;
634 static HRESULT Stream_LoadLocation( IStream *stm,
635 volume_info *volume, LPWSTR *path )
637 char *p = NULL;
638 LOCATION_INFO *loc;
639 HRESULT r;
640 DWORD n;
642 r = Stream_ReadChunk( stm, (LPVOID*) &p );
643 if( FAILED(r) )
644 return r;
646 loc = (LOCATION_INFO*) p;
647 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
649 heap_free( p );
650 return E_FAIL;
653 /* if there's valid local volume information, load it */
654 if( loc->dwVolTableOfs &&
655 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
657 LOCAL_VOLUME_INFO *volume_info;
659 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
660 Stream_LoadVolume( volume_info, volume );
663 /* if there's a local path, load it */
664 n = loc->dwLocalPathOfs;
665 if( n && (n < loc->dwTotalSize) )
666 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
668 TRACE("type %d serial %08x name %s path %s\n", volume->type,
669 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
671 heap_free( p );
672 return S_OK;
676 * The format of the advertised shortcut info seems to be:
678 * Offset Description
679 * ------ -----------
681 * 0 Length of the block (4 bytes, usually 0x314)
682 * 4 tag (dword)
683 * 8 string data in ASCII
684 * 8+0x104 string data in UNICODE
686 * In the original Win32 implementation the buffers are not initialized
687 * to zero, so data trailing the string is random garbage.
689 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
691 DWORD size;
692 ULONG count;
693 HRESULT r;
694 EXP_DARWIN_LINK buffer;
696 TRACE("%p\n",stm);
698 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
699 if( FAILED( r ) )
700 return r;
702 /* make sure that we read the size of the structure even on error */
703 size = sizeof buffer - sizeof (DWORD);
704 if( buffer.dbh.cbSize != sizeof buffer )
706 ERR("Ooops. This structure is not as expected...\n");
707 return E_FAIL;
710 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
711 if( FAILED( r ) )
712 return r;
714 if( count != size )
715 return E_FAIL;
717 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
719 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
721 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
722 return E_FAIL;
725 *str = heap_alloc((lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
726 lstrcpyW( *str, buffer.szwDarwinID );
728 return S_OK;
731 /************************************************************************
732 * IPersistStream_Load (IPersistStream)
734 static HRESULT WINAPI IPersistStream_fnLoad(
735 IPersistStream* iface,
736 IStream* stm)
738 LINK_HEADER hdr;
739 ULONG dwBytesRead;
740 BOOL unicode;
741 HRESULT r;
742 DWORD zero;
744 IShellLinkImpl *This = impl_from_IPersistStream(iface);
746 TRACE("%p %p\n", This, stm);
748 if( !stm )
749 return STG_E_INVALIDPOINTER;
751 dwBytesRead = 0;
752 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
753 if( FAILED( r ) )
754 return r;
756 if( dwBytesRead != sizeof(hdr))
757 return E_FAIL;
758 if( hdr.dwSize != sizeof(hdr))
759 return E_FAIL;
760 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
761 return E_FAIL;
763 /* free all the old stuff */
764 ILFree(This->pPidl);
765 This->pPidl = NULL;
766 memset( &This->volume, 0, sizeof This->volume );
767 heap_free(This->sPath);
768 This->sPath = NULL;
769 heap_free(This->sDescription);
770 This->sDescription = NULL;
771 heap_free(This->sPathRel);
772 This->sPathRel = NULL;
773 heap_free(This->sWorkDir);
774 This->sWorkDir = NULL;
775 heap_free(This->sArgs);
776 This->sArgs = NULL;
777 heap_free(This->sIcoPath);
778 This->sIcoPath = NULL;
779 heap_free(This->sProduct);
780 This->sProduct = NULL;
781 heap_free(This->sComponent);
782 This->sComponent = NULL;
784 This->wHotKey = hdr.wHotKey;
785 This->iIcoNdx = hdr.nIcon;
786 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
787 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
788 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
789 if (TRACE_ON(shell))
791 WCHAR sTemp[MAX_PATH];
792 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
793 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
794 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
795 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
796 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
797 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
800 /* load all the new stuff */
801 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
803 r = ILLoadFromStream( stm, &This->pPidl );
804 if( FAILED( r ) )
805 return r;
807 pdump(This->pPidl);
809 /* load the location information */
810 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
811 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
812 if( FAILED( r ) )
813 goto end;
815 unicode = hdr.dwFlags & SLDF_UNICODE;
816 if( hdr.dwFlags & SLDF_HAS_NAME )
818 r = Stream_LoadString( stm, unicode, &This->sDescription );
819 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
821 if( FAILED( r ) )
822 goto end;
824 if( hdr.dwFlags & SLDF_HAS_RELPATH )
826 r = Stream_LoadString( stm, unicode, &This->sPathRel );
827 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
829 if( FAILED( r ) )
830 goto end;
832 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
834 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
835 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
837 if( FAILED( r ) )
838 goto end;
840 if( hdr.dwFlags & SLDF_HAS_ARGS )
842 r = Stream_LoadString( stm, unicode, &This->sArgs );
843 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
845 if( FAILED( r ) )
846 goto end;
848 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
850 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
851 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
853 if( FAILED( r ) )
854 goto end;
856 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
858 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
859 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
861 if( FAILED( r ) )
862 goto end;
864 if( hdr.dwFlags & SLDF_HAS_DARWINID )
866 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
867 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
869 if( FAILED( r ) )
870 goto end;
872 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
873 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
875 /* Some lnk files have extra data blocks starting with a
876 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
877 * one with a 0xa0000003 signature. However these don't seem to matter
878 * too much.
880 WARN("Last word was not zero\n");
883 TRACE("OK\n");
885 pdump (This->pPidl);
887 return S_OK;
888 end:
889 return r;
892 /************************************************************************
893 * Stream_WriteString
895 * Helper function for IPersistStream_Save. Writes a unicode string
896 * with terminating nul byte to a stream, preceded by the its length.
898 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
900 USHORT len = lstrlenW( str ) + 1;
901 DWORD count;
902 HRESULT r;
904 r = IStream_Write( stm, &len, sizeof(len), &count );
905 if( FAILED( r ) )
906 return r;
908 len *= sizeof(WCHAR);
910 r = IStream_Write( stm, str, len, &count );
911 if( FAILED( r ) )
912 return r;
914 return S_OK;
917 /************************************************************************
918 * Stream_WriteLocationInfo
920 * Writes the location info to a stream
922 * FIXME: One day we might want to write the network volume information
923 * and the final path.
924 * Figure out how Windows deals with unicode paths here.
926 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
927 volume_info *volume )
929 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
930 LOCAL_VOLUME_INFO *vol;
931 LOCATION_INFO *loc;
932 LPSTR szLabel, szPath, szFinalPath;
933 ULONG count = 0;
934 HRESULT hr;
936 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
938 /* figure out the size of everything */
939 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
940 NULL, 0, NULL, NULL );
941 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
942 NULL, 0, NULL, NULL );
943 volume_info_size = sizeof *vol + label_size;
944 final_path_size = 1;
945 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
947 /* create pointers to everything */
948 loc = heap_alloc_zero(total_size);
949 vol = (LOCAL_VOLUME_INFO*) &loc[1];
950 szLabel = (LPSTR) &vol[1];
951 szPath = &szLabel[label_size];
952 szFinalPath = &szPath[path_size];
954 /* fill in the location information header */
955 loc->dwTotalSize = total_size;
956 loc->dwHeaderSize = sizeof (*loc);
957 loc->dwFlags = 1;
958 loc->dwVolTableOfs = sizeof (*loc);
959 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
960 loc->dwNetworkVolTableOfs = 0;
961 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
963 /* fill in the volume information */
964 vol->dwSize = volume_info_size;
965 vol->dwType = volume->type;
966 vol->dwVolSerial = volume->serial;
967 vol->dwVolLabelOfs = sizeof (*vol);
969 /* copy in the strings */
970 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
971 szLabel, label_size, NULL, NULL );
972 WideCharToMultiByte( CP_ACP, 0, path, -1,
973 szPath, path_size, NULL, NULL );
974 szFinalPath[0] = 0;
976 hr = IStream_Write( stm, loc, total_size, &count );
977 heap_free(loc);
979 return hr;
982 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
984 EXP_DARWIN_LINK *buffer;
986 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
987 buffer->dbh.cbSize = sizeof *buffer;
988 buffer->dbh.dwSignature = magic;
989 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
990 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
992 return buffer;
995 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
997 EXP_DARWIN_LINK *buffer;
998 ULONG count;
1000 TRACE("%p\n",stm);
1002 buffer = shelllink_build_darwinid( string, magic );
1004 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1007 /************************************************************************
1008 * IPersistStream_Save (IPersistStream)
1010 * FIXME: makes assumptions about byte order
1012 static HRESULT WINAPI IPersistStream_fnSave(
1013 IPersistStream* iface,
1014 IStream* stm,
1015 BOOL fClearDirty)
1017 LINK_HEADER header;
1018 ULONG count;
1019 DWORD zero;
1020 HRESULT r;
1022 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1024 TRACE("%p %p %x\n", This, stm, fClearDirty);
1026 memset(&header, 0, sizeof(header));
1027 header.dwSize = sizeof(header);
1028 header.fStartup = This->iShowCmd;
1029 header.MagicGuid = CLSID_ShellLink;
1031 header.wHotKey = This->wHotKey;
1032 header.nIcon = This->iIcoNdx;
1033 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1034 if( This->pPidl )
1035 header.dwFlags |= SLDF_HAS_ID_LIST;
1036 if( This->sPath )
1037 header.dwFlags |= SLDF_HAS_LINK_INFO;
1038 if( This->sDescription )
1039 header.dwFlags |= SLDF_HAS_NAME;
1040 if( This->sWorkDir )
1041 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1042 if( This->sArgs )
1043 header.dwFlags |= SLDF_HAS_ARGS;
1044 if( This->sIcoPath )
1045 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1046 if( This->sProduct )
1047 header.dwFlags |= SLDF_HAS_LOGO3ID;
1048 if( This->sComponent )
1049 header.dwFlags |= SLDF_HAS_DARWINID;
1051 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1052 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1053 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1055 /* write the Shortcut header */
1056 r = IStream_Write( stm, &header, sizeof(header), &count );
1057 if( FAILED( r ) )
1059 ERR("Write failed at %d\n",__LINE__);
1060 return r;
1063 TRACE("Writing pidl\n");
1065 /* write the PIDL to the shortcut */
1066 if( This->pPidl )
1068 r = ILSaveToStream( stm, This->pPidl );
1069 if( FAILED( r ) )
1071 ERR("Failed to write PIDL at %d\n",__LINE__);
1072 return r;
1076 if( This->sPath )
1077 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1079 if( This->sDescription )
1080 r = Stream_WriteString( stm, This->sDescription );
1082 if( This->sPathRel )
1083 r = Stream_WriteString( stm, This->sPathRel );
1085 if( This->sWorkDir )
1086 r = Stream_WriteString( stm, This->sWorkDir );
1088 if( This->sArgs )
1089 r = Stream_WriteString( stm, This->sArgs );
1091 if( This->sIcoPath )
1092 r = Stream_WriteString( stm, This->sIcoPath );
1094 if( This->sProduct )
1095 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1097 if( This->sComponent )
1098 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1100 /* the last field is a single zero dword */
1101 zero = 0;
1102 r = IStream_Write( stm, &zero, sizeof zero, &count );
1104 return S_OK;
1107 /************************************************************************
1108 * IPersistStream_GetSizeMax (IPersistStream)
1110 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1111 IPersistStream* iface,
1112 ULARGE_INTEGER* pcbSize)
1114 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1116 TRACE("(%p)\n", This);
1118 return E_NOTIMPL;
1121 static const IPersistStreamVtbl psvt =
1123 IPersistStream_fnQueryInterface,
1124 IPersistStream_fnAddRef,
1125 IPersistStream_fnRelease,
1126 IPersistStream_fnGetClassID,
1127 IPersistStream_fnIsDirty,
1128 IPersistStream_fnLoad,
1129 IPersistStream_fnSave,
1130 IPersistStream_fnGetSizeMax
1133 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1135 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1136 return FALSE;
1137 return TRUE;
1140 /**************************************************************************
1141 * ShellLink_UpdatePath
1142 * update absolute path in sPath using relative path in sPathRel
1144 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1146 if (!path || !psPath)
1147 return E_INVALIDARG;
1149 if (!*psPath && sPathRel) {
1150 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1151 LPWSTR final = NULL;
1153 /* first try if [directory of link file] + [relative path] finds an existing file */
1155 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1156 if( !final )
1157 final = buffer;
1158 lstrcpyW(final, sPathRel);
1160 *abs_path = '\0';
1162 if (SHELL_ExistsFileW(buffer)) {
1163 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1164 lstrcpyW(abs_path, buffer);
1165 } else {
1166 /* try if [working directory] + [relative path] finds an existing file */
1167 if (sWorkDir) {
1168 lstrcpyW(buffer, sWorkDir);
1169 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1171 if (SHELL_ExistsFileW(buffer))
1172 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1173 lstrcpyW(abs_path, buffer);
1177 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1178 if (!*abs_path)
1179 lstrcpyW(abs_path, sPathRel);
1181 *psPath = heap_alloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1182 if (!*psPath)
1183 return E_OUTOFMEMORY;
1185 lstrcpyW(*psPath, abs_path);
1188 return S_OK;
1191 /**************************************************************************
1192 * IShellLink_ConstructFromFile
1194 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1195 LPCITEMIDLIST pidl, IUnknown **ppv)
1197 IShellLinkW* psl;
1199 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1201 if (SUCCEEDED(hr)) {
1202 IPersistFile* ppf;
1204 *ppv = NULL;
1206 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1208 if (SUCCEEDED(hr)) {
1209 WCHAR path[MAX_PATH];
1211 if (SHGetPathFromIDListW(pidl, path))
1212 hr = IPersistFile_Load(ppf, path, 0);
1213 else
1214 hr = E_FAIL;
1216 if (SUCCEEDED(hr))
1217 *ppv = (IUnknown*)psl;
1219 IPersistFile_Release(ppf);
1222 if (!*ppv)
1223 IShellLinkW_Release(psl);
1226 return hr;
1229 /**************************************************************************
1230 * IShellLinkA_QueryInterface
1232 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1234 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1235 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1238 /******************************************************************************
1239 * IShellLinkA_AddRef
1241 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1243 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1244 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1247 /******************************************************************************
1248 * IShellLinkA_Release
1250 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1252 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1253 return IShellLinkW_Release(&This->IShellLinkW_iface);
1256 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1257 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1259 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1260 HRESULT res = S_OK;
1262 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1263 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1265 if (This->sComponent || This->sProduct)
1266 return S_FALSE;
1268 if (cchMaxPath)
1269 pszFile[0] = 0;
1270 if (This->sPath && This->sPath[0])
1271 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1272 pszFile, cchMaxPath, NULL, NULL);
1273 else
1274 res = S_FALSE;
1276 if (pfd)
1278 memset(pfd, 0, sizeof(*pfd));
1280 if (res == S_OK)
1282 char path[MAX_PATH];
1283 WIN32_FILE_ATTRIBUTE_DATA fad;
1285 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1287 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1289 pfd->dwFileAttributes = fad.dwFileAttributes;
1290 pfd->ftCreationTime = fad.ftCreationTime;
1291 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1292 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1293 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1294 pfd->nFileSizeLow = fad.nFileSizeLow;
1297 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1299 if (GetShortPathNameA(path, path, MAX_PATH))
1301 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1305 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1306 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1307 wine_dbgstr_a(pfd->cAlternateFileName));
1310 return res;
1313 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1315 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1316 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1319 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1321 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1322 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1325 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1326 INT cchMaxName)
1328 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1330 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1332 if( cchMaxName )
1333 pszName[0] = 0;
1334 if( This->sDescription )
1335 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1336 pszName, cchMaxName, NULL, NULL);
1338 return S_OK;
1341 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1343 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1344 WCHAR *descrW;
1345 HRESULT hr;
1347 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1349 if (pszName)
1351 descrW = heap_strdupAtoW(pszName);
1352 if (!descrW) return E_OUTOFMEMORY;
1354 else
1355 descrW = NULL;
1357 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1358 heap_free(descrW);
1360 return hr;
1363 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1364 INT cchMaxPath)
1366 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1368 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1370 if( cchMaxPath )
1371 pszDir[0] = 0;
1372 if( This->sWorkDir )
1373 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1374 pszDir, cchMaxPath, NULL, NULL);
1376 return S_OK;
1379 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1381 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1382 WCHAR *dirW;
1383 HRESULT hr;
1385 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1387 dirW = heap_strdupAtoW(pszDir);
1388 if (!dirW) return E_OUTOFMEMORY;
1390 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1391 heap_free(dirW);
1393 return hr;
1396 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1398 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1400 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1402 if( cchMaxPath )
1403 pszArgs[0] = 0;
1404 if( This->sArgs )
1405 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1406 pszArgs, cchMaxPath, NULL, NULL);
1408 return S_OK;
1411 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1413 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1414 WCHAR *argsW;
1415 HRESULT hr;
1417 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1419 if (pszArgs)
1421 argsW = heap_strdupAtoW(pszArgs);
1422 if (!argsW) return E_OUTOFMEMORY;
1424 else
1425 argsW = NULL;
1427 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1428 heap_free(argsW);
1430 return hr;
1433 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1435 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1436 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1439 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1441 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1442 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1445 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1447 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1448 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1451 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1453 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1454 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1457 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1458 INT cchIconPath, INT *piIcon)
1460 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1462 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1464 *piIcon = This->iIcoNdx;
1466 if (This->sIcoPath)
1467 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1468 else
1469 pszIconPath[0] = 0;
1471 return S_OK;
1474 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1476 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1477 WCHAR *pathW = NULL;
1478 HRESULT hr;
1480 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1482 if (path)
1484 pathW = heap_strdupAtoW(path);
1485 if (!pathW)
1486 return E_OUTOFMEMORY;
1489 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1490 heap_free(pathW);
1492 return hr;
1495 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1496 DWORD dwReserved)
1498 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1499 WCHAR *pathW;
1500 HRESULT hr;
1502 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1504 pathW = heap_strdupAtoW(pszPathRel);
1505 if (!pathW) return E_OUTOFMEMORY;
1507 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1508 heap_free(pathW);
1510 return hr;
1513 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1515 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1517 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1519 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1522 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1524 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1525 HRESULT r;
1526 LPWSTR str;
1528 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1530 if (!pszFile) return E_INVALIDARG;
1532 str = heap_strdupAtoW(pszFile);
1533 if( !str )
1534 return E_OUTOFMEMORY;
1536 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1537 heap_free( str );
1539 return r;
1542 /**************************************************************************
1543 * IShellLink Implementation
1546 static const IShellLinkAVtbl slvt =
1548 IShellLinkA_fnQueryInterface,
1549 IShellLinkA_fnAddRef,
1550 IShellLinkA_fnRelease,
1551 IShellLinkA_fnGetPath,
1552 IShellLinkA_fnGetIDList,
1553 IShellLinkA_fnSetIDList,
1554 IShellLinkA_fnGetDescription,
1555 IShellLinkA_fnSetDescription,
1556 IShellLinkA_fnGetWorkingDirectory,
1557 IShellLinkA_fnSetWorkingDirectory,
1558 IShellLinkA_fnGetArguments,
1559 IShellLinkA_fnSetArguments,
1560 IShellLinkA_fnGetHotkey,
1561 IShellLinkA_fnSetHotkey,
1562 IShellLinkA_fnGetShowCmd,
1563 IShellLinkA_fnSetShowCmd,
1564 IShellLinkA_fnGetIconLocation,
1565 IShellLinkA_fnSetIconLocation,
1566 IShellLinkA_fnSetRelativePath,
1567 IShellLinkA_fnResolve,
1568 IShellLinkA_fnSetPath
1572 /**************************************************************************
1573 * IShellLinkW_fnQueryInterface
1575 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1576 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1578 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1580 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1582 *ppvObj = NULL;
1584 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1586 *ppvObj = &This->IShellLinkA_iface;
1588 else if(IsEqualIID(riid, &IID_IShellLinkW))
1590 *ppvObj = &This->IShellLinkW_iface;
1592 else if(IsEqualIID(riid, &IID_IPersistFile))
1594 *ppvObj = &This->IPersistFile_iface;
1596 else if(IsEqualIID(riid, &IID_IPersistStream))
1598 *ppvObj = &This->IPersistStream_iface;
1600 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1602 *ppvObj = &This->IShellLinkDataList_iface;
1604 else if(IsEqualIID(riid, &IID_IShellExtInit))
1606 *ppvObj = &This->IShellExtInit_iface;
1608 else if(IsEqualIID(riid, &IID_IContextMenu))
1610 *ppvObj = &This->IContextMenu_iface;
1612 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1614 *ppvObj = &This->IObjectWithSite_iface;
1616 else if(IsEqualIID(riid, &IID_IPropertyStore))
1618 *ppvObj = &This->IPropertyStore_iface;
1621 if(*ppvObj)
1623 IUnknown_AddRef((IUnknown*)*ppvObj);
1624 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1625 return S_OK;
1627 ERR("-- Interface: E_NOINTERFACE\n");
1628 return E_NOINTERFACE;
1631 /******************************************************************************
1632 * IShellLinkW_fnAddRef
1634 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1636 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1637 ULONG ref = InterlockedIncrement(&This->ref);
1639 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1641 return ref;
1644 /******************************************************************************
1645 * IShellLinkW_fnRelease
1647 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1649 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1650 ULONG refCount = InterlockedDecrement(&This->ref);
1652 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1654 if (refCount)
1655 return refCount;
1657 TRACE("-- destroying IShellLink(%p)\n",This);
1659 heap_free(This->sIcoPath);
1660 heap_free(This->sArgs);
1661 heap_free(This->sWorkDir);
1662 heap_free(This->sDescription);
1663 heap_free(This->sPath);
1664 heap_free(This->sPathRel);
1665 heap_free(This->sProduct);
1666 heap_free(This->sComponent);
1667 heap_free(This->filepath);
1669 if (This->site)
1670 IUnknown_Release( This->site );
1672 if (This->pPidl)
1673 ILFree(This->pPidl);
1675 LocalFree(This);
1677 return 0;
1680 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1682 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1683 HRESULT res = S_OK;
1685 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1686 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1688 if (This->sComponent || This->sProduct)
1689 return S_FALSE;
1691 if (cchMaxPath)
1692 pszFile[0] = 0;
1693 if (This->sPath)
1694 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1695 else
1696 res = S_FALSE;
1698 if (pfd)
1700 memset(pfd, 0, sizeof(*pfd));
1702 if (res == S_OK)
1704 WCHAR path[MAX_PATH];
1705 WIN32_FILE_ATTRIBUTE_DATA fad;
1707 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1709 pfd->dwFileAttributes = fad.dwFileAttributes;
1710 pfd->ftCreationTime = fad.ftCreationTime;
1711 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1712 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1713 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1714 pfd->nFileSizeLow = fad.nFileSizeLow;
1717 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1719 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1721 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1725 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1726 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1727 wine_dbgstr_w(pfd->cAlternateFileName));
1730 return res;
1733 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1735 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1737 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1739 if (!This->pPidl)
1741 *ppidl = NULL;
1742 return S_FALSE;
1744 *ppidl = ILClone(This->pPidl);
1745 return S_OK;
1748 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1750 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1751 WCHAR path[MAX_PATH];
1753 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1755 if( This->pPidl )
1756 ILFree( This->pPidl );
1757 This->pPidl = ILClone( pidl );
1758 if( !This->pPidl )
1759 return E_FAIL;
1761 heap_free( This->sPath );
1762 This->sPath = NULL;
1764 if ( SHGetPathFromIDListW( pidl, path ) )
1766 This->sPath = heap_alloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1767 if (!This->sPath)
1768 return E_OUTOFMEMORY;
1770 lstrcpyW(This->sPath, path);
1773 This->bDirty = TRUE;
1775 return S_OK;
1778 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1780 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1782 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1784 pszName[0] = 0;
1785 if( This->sDescription )
1786 lstrcpynW( pszName, This->sDescription, cchMaxName );
1788 return S_OK;
1791 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1793 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1795 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1797 heap_free(This->sDescription);
1798 if (pszName)
1800 This->sDescription = heap_alloc((lstrlenW( pszName )+1)*sizeof(WCHAR) );
1801 if ( !This->sDescription )
1802 return E_OUTOFMEMORY;
1804 lstrcpyW( This->sDescription, pszName );
1806 else
1807 This->sDescription = NULL;
1808 This->bDirty = TRUE;
1810 return S_OK;
1813 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1815 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1817 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1819 if( cchMaxPath )
1820 pszDir[0] = 0;
1821 if( This->sWorkDir )
1822 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1824 return S_OK;
1827 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1829 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1831 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1833 heap_free(This->sWorkDir);
1834 This->sWorkDir = heap_alloc((lstrlenW( pszDir ) + 1) * sizeof (WCHAR) );
1835 if ( !This->sWorkDir )
1836 return E_OUTOFMEMORY;
1837 lstrcpyW( This->sWorkDir, pszDir );
1838 This->bDirty = TRUE;
1840 return S_OK;
1843 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1845 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1847 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1849 if( cchMaxPath )
1850 pszArgs[0] = 0;
1851 if( This->sArgs )
1852 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1854 return S_OK;
1857 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1859 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1861 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1863 heap_free(This->sArgs);
1864 if (pszArgs)
1866 This->sArgs = heap_alloc((lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1867 if ( !This->sArgs )
1868 return E_OUTOFMEMORY;
1869 lstrcpyW( This->sArgs, pszArgs );
1871 else This->sArgs = NULL;
1873 This->bDirty = TRUE;
1875 return S_OK;
1878 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1880 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1882 TRACE("(%p)->(%p)\n",This, pwHotkey);
1884 *pwHotkey=This->wHotKey;
1886 return S_OK;
1889 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1891 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1893 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1895 This->wHotKey = wHotkey;
1896 This->bDirty = TRUE;
1898 return S_OK;
1901 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1903 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1905 TRACE("(%p)->(%p)\n",This, piShowCmd);
1907 *piShowCmd = This->iShowCmd;
1909 return S_OK;
1912 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1914 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1916 TRACE("(%p)->(%d)\n", This, iShowCmd);
1918 This->iShowCmd = iShowCmd;
1919 This->bDirty = TRUE;
1921 return S_OK;
1924 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1926 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1928 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1930 *piIcon = This->iIcoNdx;
1932 if (This->sIcoPath)
1933 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1934 else
1935 pszIconPath[0] = 0;
1937 return S_OK;
1940 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1942 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1944 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1946 heap_free(This->sIcoPath);
1947 if (path)
1949 size_t len = (strlenW(path) + 1) * sizeof(WCHAR);
1950 This->sIcoPath = heap_alloc(len);
1951 if (!This->sIcoPath)
1952 return E_OUTOFMEMORY;
1953 memcpy(This->sIcoPath, path, len);
1955 else
1956 This->sIcoPath = NULL;
1957 This->iIcoNdx = icon;
1958 This->bDirty = TRUE;
1960 return S_OK;
1963 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1965 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1967 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1969 heap_free(This->sPathRel);
1970 This->sPathRel = heap_alloc((lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1971 if ( !This->sPathRel )
1972 return E_OUTOFMEMORY;
1973 lstrcpyW( This->sPathRel, pszPathRel );
1974 This->bDirty = TRUE;
1976 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1979 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1981 HRESULT hr = S_OK;
1982 BOOL bSuccess;
1984 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1986 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1988 /*FIXME: use IResolveShellLink interface */
1990 if (!This->sPath && This->pPidl) {
1991 WCHAR buffer[MAX_PATH];
1993 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1995 if (bSuccess && *buffer) {
1996 This->sPath = heap_alloc((lstrlenW(buffer)+1)*sizeof(WCHAR));
1997 if (!This->sPath)
1998 return E_OUTOFMEMORY;
2000 lstrcpyW(This->sPath, buffer);
2002 This->bDirty = TRUE;
2003 } else
2004 hr = S_OK; /* don't report an error occurred while just caching information */
2007 if (!This->sIcoPath && This->sPath) {
2008 This->sIcoPath = heap_alloc((lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2009 if (!This->sIcoPath)
2010 return E_OUTOFMEMORY;
2012 lstrcpyW(This->sIcoPath, This->sPath);
2013 This->iIcoNdx = 0;
2015 This->bDirty = TRUE;
2018 return hr;
2021 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2023 LPWSTR ret;
2024 LPCWSTR p;
2025 DWORD len;
2027 if( !str )
2028 return NULL;
2030 p = strchrW( str, ':' );
2031 if( !p )
2032 return NULL;
2033 len = p - str;
2034 ret = heap_alloc(sizeof(WCHAR)*(len+1));
2035 if( !ret )
2036 return ret;
2037 memcpy( ret, str, sizeof(WCHAR)*len );
2038 ret[len] = 0;
2039 return ret;
2042 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2044 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2045 WCHAR szGuid[39];
2046 HRESULT r;
2047 GUID guid;
2048 int len;
2050 while( str[0] )
2052 /* each segment must start with two colons */
2053 if( str[0] != ':' || str[1] != ':' )
2054 return E_FAIL;
2056 /* the last segment is just two colons */
2057 if( !str[2] )
2058 break;
2059 str += 2;
2061 /* there must be a colon straight after a guid */
2062 p = strchrW( str, ':' );
2063 if( !p )
2064 return E_FAIL;
2065 len = p - str;
2066 if( len != 38 )
2067 return E_FAIL;
2069 /* get the guid, and check it's validly formatted */
2070 memcpy( szGuid, str, sizeof(WCHAR)*len );
2071 szGuid[len] = 0;
2072 r = CLSIDFromString( szGuid, &guid );
2073 if( r != S_OK )
2074 return r;
2075 str = p + 1;
2077 /* match it up to a guid that we care about */
2078 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2079 szComponent = str;
2080 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2081 szProduct = str;
2082 else
2083 return E_FAIL;
2085 /* skip to the next field */
2086 str = strchrW( str, ':' );
2087 if( !str )
2088 return E_FAIL;
2091 /* we have to have a component for an advertised shortcut */
2092 if( !szComponent )
2093 return E_FAIL;
2095 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2096 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2098 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2099 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2101 return S_OK;
2104 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2106 const int label_sz = ARRAY_SIZE(volume->label);
2107 WCHAR drive[] = { path[0], ':', '\\', 0 };
2108 BOOL r;
2110 volume->type = GetDriveTypeW(drive);
2111 r = GetVolumeInformationW(drive, volume->label, label_sz,
2112 &volume->serial, NULL, NULL, NULL, 0);
2113 TRACE("r = %d type %d serial %08x name %s\n", r,
2114 volume->type, volume->serial, debugstr_w(volume->label));
2115 return r;
2118 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2120 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2121 WCHAR buffer[MAX_PATH];
2122 LPWSTR fname, unquoted = NULL;
2123 HRESULT hr = S_OK;
2124 UINT len;
2126 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2128 if (!pszFile) return E_INVALIDARG;
2130 /* quotes at the ends of the string are stripped */
2131 len = lstrlenW(pszFile);
2132 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2134 unquoted = strdupW(pszFile);
2135 PathUnquoteSpacesW(unquoted);
2136 pszFile = unquoted;
2139 /* any other quote marks are invalid */
2140 if (strchrW(pszFile, '"'))
2142 heap_free(unquoted);
2143 return S_FALSE;
2146 heap_free(This->sPath);
2147 This->sPath = NULL;
2149 heap_free(This->sComponent);
2150 This->sComponent = NULL;
2152 if (This->pPidl)
2153 ILFree(This->pPidl);
2154 This->pPidl = NULL;
2156 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2158 if (*pszFile == '\0')
2159 *buffer = '\0';
2160 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2161 return E_FAIL;
2162 else if(!PathFileExistsW(buffer) &&
2163 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2164 hr = S_FALSE;
2166 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2167 ShellLink_GetVolumeInfo(buffer, &This->volume);
2169 This->sPath = heap_alloc( (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2170 if (!This->sPath)
2172 heap_free(unquoted);
2173 return E_OUTOFMEMORY;
2176 lstrcpyW(This->sPath, buffer);
2178 This->bDirty = TRUE;
2179 heap_free(unquoted);
2181 return hr;
2184 /**************************************************************************
2185 * IShellLinkW Implementation
2188 static const IShellLinkWVtbl slvtw =
2190 IShellLinkW_fnQueryInterface,
2191 IShellLinkW_fnAddRef,
2192 IShellLinkW_fnRelease,
2193 IShellLinkW_fnGetPath,
2194 IShellLinkW_fnGetIDList,
2195 IShellLinkW_fnSetIDList,
2196 IShellLinkW_fnGetDescription,
2197 IShellLinkW_fnSetDescription,
2198 IShellLinkW_fnGetWorkingDirectory,
2199 IShellLinkW_fnSetWorkingDirectory,
2200 IShellLinkW_fnGetArguments,
2201 IShellLinkW_fnSetArguments,
2202 IShellLinkW_fnGetHotkey,
2203 IShellLinkW_fnSetHotkey,
2204 IShellLinkW_fnGetShowCmd,
2205 IShellLinkW_fnSetShowCmd,
2206 IShellLinkW_fnGetIconLocation,
2207 IShellLinkW_fnSetIconLocation,
2208 IShellLinkW_fnSetRelativePath,
2209 IShellLinkW_fnResolve,
2210 IShellLinkW_fnSetPath
2213 static HRESULT WINAPI
2214 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2216 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2217 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2220 static ULONG WINAPI
2221 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2223 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2224 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2227 static ULONG WINAPI
2228 ShellLink_DataList_Release( IShellLinkDataList* iface )
2230 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2231 return IShellLinkW_Release(&This->IShellLinkW_iface);
2234 static HRESULT WINAPI
2235 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2237 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2238 return E_NOTIMPL;
2241 static HRESULT WINAPI
2242 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2244 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2245 LPVOID block = NULL;
2246 HRESULT r = E_FAIL;
2248 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2250 switch (dwSig)
2252 case EXP_DARWIN_ID_SIG:
2253 if (!This->sComponent)
2254 break;
2255 block = shelllink_build_darwinid( This->sComponent, dwSig );
2256 r = S_OK;
2257 break;
2258 case EXP_SZ_LINK_SIG:
2259 case NT_CONSOLE_PROPS_SIG:
2260 case NT_FE_CONSOLE_PROPS_SIG:
2261 case EXP_SPECIAL_FOLDER_SIG:
2262 case EXP_SZ_ICON_SIG:
2263 FIXME("valid but unhandled datablock %08x\n", dwSig);
2264 break;
2265 default:
2266 ERR("unknown datablock %08x\n", dwSig);
2268 *ppDataBlock = block;
2269 return r;
2272 static HRESULT WINAPI
2273 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2275 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2276 return E_NOTIMPL;
2279 static HRESULT WINAPI
2280 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2282 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2283 DWORD flags = 0;
2285 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2287 /* FIXME: add more */
2288 if (This->sArgs)
2289 flags |= SLDF_HAS_ARGS;
2290 if (This->sComponent)
2291 flags |= SLDF_HAS_DARWINID;
2292 if (This->sIcoPath)
2293 flags |= SLDF_HAS_ICONLOCATION;
2294 if (This->sProduct)
2295 flags |= SLDF_HAS_LOGO3ID;
2296 if (This->pPidl)
2297 flags |= SLDF_HAS_ID_LIST;
2299 *pdwFlags = flags;
2301 return S_OK;
2304 static HRESULT WINAPI
2305 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2307 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2308 return S_OK;
2311 static const IShellLinkDataListVtbl dlvt =
2313 ShellLink_DataList_QueryInterface,
2314 ShellLink_DataList_AddRef,
2315 ShellLink_DataList_Release,
2316 ShellLink_AddDataBlock,
2317 ShellLink_CopyDataBlock,
2318 ShellLink_RemoveDataBlock,
2319 ShellLink_GetFlags,
2320 ShellLink_SetFlags
2323 static HRESULT WINAPI
2324 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2326 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2327 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2330 static ULONG WINAPI
2331 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2333 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2334 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2337 static ULONG WINAPI
2338 ShellLink_ExtInit_Release( IShellExtInit* iface )
2340 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2341 return IShellLinkW_Release(&This->IShellLinkW_iface);
2344 /**************************************************************************
2345 * ShellLink implementation of IShellExtInit::Initialize()
2347 * Loads the shelllink from the dataobject the shell is pointing to.
2349 static HRESULT WINAPI
2350 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2351 IDataObject *pdtobj, HKEY hkeyProgID )
2353 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2354 FORMATETC format;
2355 STGMEDIUM stgm;
2356 UINT count;
2357 HRESULT r = E_FAIL;
2359 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2361 if( !pdtobj )
2362 return r;
2364 format.cfFormat = CF_HDROP;
2365 format.ptd = NULL;
2366 format.dwAspect = DVASPECT_CONTENT;
2367 format.lindex = -1;
2368 format.tymed = TYMED_HGLOBAL;
2370 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2371 return r;
2373 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2374 if( count == 1 )
2376 LPWSTR path;
2378 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2379 count++;
2380 path = heap_alloc(count*sizeof(WCHAR) );
2381 if( path )
2383 IPersistFile *pf = &This->IPersistFile_iface;
2385 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2386 r = IPersistFile_Load( pf, path, 0 );
2387 heap_free( path );
2390 ReleaseStgMedium( &stgm );
2392 return r;
2395 static const IShellExtInitVtbl eivt =
2397 ShellLink_ExtInit_QueryInterface,
2398 ShellLink_ExtInit_AddRef,
2399 ShellLink_ExtInit_Release,
2400 ShellLink_ExtInit_Initialize
2403 static HRESULT WINAPI
2404 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2406 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2407 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2410 static ULONG WINAPI
2411 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2413 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2414 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2417 static ULONG WINAPI
2418 ShellLink_ContextMenu_Release( IContextMenu* iface )
2420 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2421 return IShellLinkW_Release(&This->IShellLinkW_iface);
2424 static HRESULT WINAPI
2425 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2426 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2428 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2429 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2430 MENUITEMINFOW mii;
2431 int id = 1;
2433 TRACE("%p %p %u %u %u %u\n", This,
2434 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2436 if ( !hmenu )
2437 return E_INVALIDARG;
2439 memset( &mii, 0, sizeof mii );
2440 mii.cbSize = sizeof mii;
2441 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2442 mii.dwTypeData = szOpen;
2443 mii.cch = strlenW( mii.dwTypeData );
2444 mii.wID = idCmdFirst + id++;
2445 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2446 mii.fType = MFT_STRING;
2447 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2448 return E_FAIL;
2449 This->iIdOpen = 0;
2451 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2454 static LPWSTR
2455 shelllink_get_msi_component_path( LPWSTR component )
2457 LPWSTR path;
2458 DWORD r, sz = 0;
2460 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2461 if (r != ERROR_SUCCESS)
2462 return NULL;
2464 sz++;
2465 path = heap_alloc( sz*sizeof(WCHAR) );
2466 r = CommandLineFromMsiDescriptor( component, path, &sz );
2467 if (r != ERROR_SUCCESS)
2469 heap_free( path );
2470 path = NULL;
2473 TRACE("returning %s\n", debugstr_w( path ) );
2475 return path;
2478 static HRESULT WINAPI
2479 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2481 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2482 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2483 SHELLEXECUTEINFOW sei;
2484 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2485 LPWSTR args = NULL;
2486 LPWSTR path = NULL;
2487 HRESULT r;
2489 TRACE("%p %p\n", This, lpici );
2491 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2492 return E_INVALIDARG;
2494 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2496 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2497 return E_INVALIDARG;
2500 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2501 if ( FAILED( r ) )
2502 return r;
2504 if ( This->sComponent )
2506 path = shelllink_get_msi_component_path( This->sComponent );
2507 if (!path)
2508 return E_FAIL;
2510 else
2511 path = strdupW( This->sPath );
2513 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2514 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2516 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2517 DWORD len = 2;
2519 if ( This->sArgs )
2520 len += lstrlenW( This->sArgs );
2521 if ( iciex->lpParametersW )
2522 len += lstrlenW( iciex->lpParametersW );
2524 args = heap_alloc( len*sizeof(WCHAR) );
2525 args[0] = 0;
2526 if ( This->sArgs )
2527 lstrcatW( args, This->sArgs );
2528 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2530 static const WCHAR space[] = { ' ', 0 };
2531 lstrcatW( args, space );
2532 lstrcatW( args, iciex->lpParametersW );
2536 memset( &sei, 0, sizeof sei );
2537 sei.cbSize = sizeof sei;
2538 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2539 sei.lpFile = path;
2540 sei.nShow = This->iShowCmd;
2541 sei.lpIDList = This->pPidl;
2542 sei.lpDirectory = This->sWorkDir;
2543 sei.lpParameters = args;
2544 sei.lpVerb = szOpen;
2546 if( ShellExecuteExW( &sei ) )
2547 r = S_OK;
2548 else
2549 r = E_FAIL;
2551 heap_free( args );
2552 heap_free( path );
2554 return r;
2557 static HRESULT WINAPI
2558 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2559 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2561 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2563 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2564 idCmd, uType, pwReserved, pszName, cchMax );
2566 return E_NOTIMPL;
2569 static const IContextMenuVtbl cmvt =
2571 ShellLink_ContextMenu_QueryInterface,
2572 ShellLink_ContextMenu_AddRef,
2573 ShellLink_ContextMenu_Release,
2574 ShellLink_QueryContextMenu,
2575 ShellLink_InvokeCommand,
2576 ShellLink_GetCommandString
2579 static HRESULT WINAPI
2580 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2582 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2583 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2586 static ULONG WINAPI
2587 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2589 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2590 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2593 static ULONG WINAPI
2594 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2596 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2597 return IShellLinkW_Release(&This->IShellLinkW_iface);
2600 static HRESULT WINAPI
2601 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2603 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2605 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2607 if ( !This->site )
2608 return E_FAIL;
2609 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2612 static HRESULT WINAPI
2613 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2615 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2617 TRACE("%p %p\n", iface, punk);
2619 if ( punk )
2620 IUnknown_AddRef( punk );
2622 if( This->site )
2623 IUnknown_Release( This->site );
2625 This->site = punk;
2627 return S_OK;
2630 static const IObjectWithSiteVtbl owsvt =
2632 ShellLink_ObjectWithSite_QueryInterface,
2633 ShellLink_ObjectWithSite_AddRef,
2634 ShellLink_ObjectWithSite_Release,
2635 ShellLink_SetSite,
2636 ShellLink_GetSite,
2639 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2641 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2642 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2645 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2647 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2648 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2651 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2653 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2654 return IShellLinkW_Release(&This->IShellLinkW_iface);
2657 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2659 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2660 FIXME("(%p)->(%p): stub\n", This, props);
2661 return E_NOTIMPL;
2664 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2666 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2667 FIXME("(%p)->(%d %p): stub\n", This, propid, key);
2668 return E_NOTIMPL;
2671 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2673 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2674 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2675 return E_NOTIMPL;
2678 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2680 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2681 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2682 return S_OK;
2685 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2687 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2688 FIXME("(%p): stub\n", This);
2689 return S_OK;
2692 static const IPropertyStoreVtbl propertystorevtbl = {
2693 propertystore_QueryInterface,
2694 propertystore_AddRef,
2695 propertystore_Release,
2696 propertystore_GetCount,
2697 propertystore_GetAt,
2698 propertystore_GetValue,
2699 propertystore_SetValue,
2700 propertystore_Commit
2703 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2705 IShellLinkImpl * sl;
2706 HRESULT r;
2708 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2710 *obj = NULL;
2712 if (outer)
2713 return CLASS_E_NOAGGREGATION;
2715 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2716 if (!sl)
2717 return E_OUTOFMEMORY;
2719 sl->ref = 1;
2720 sl->IShellLinkA_iface.lpVtbl = &slvt;
2721 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2722 sl->IPersistFile_iface.lpVtbl = &pfvt;
2723 sl->IPersistStream_iface.lpVtbl = &psvt;
2724 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2725 sl->IShellExtInit_iface.lpVtbl = &eivt;
2726 sl->IContextMenu_iface.lpVtbl = &cmvt;
2727 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2728 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2729 sl->iShowCmd = SW_SHOWNORMAL;
2730 sl->bDirty = FALSE;
2731 sl->iIdOpen = -1;
2732 sl->site = NULL;
2733 sl->filepath = NULL;
2735 TRACE("(%p)\n", sl);
2737 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2738 IShellLinkW_Release( &sl->IShellLinkW_iface );
2739 return r;