user32: Add wsprintfW %C tests.
[wine.git] / dlls / shell32 / shelllink.c
blob5b044d4be1d9f5a33b0de2227d912fcd5893816e
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)
370 return E_FAIL;
372 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
373 if( SUCCEEDED( r ) )
375 r = IPersistStream_Save(StreamThis, stm, FALSE);
376 IStream_Release( stm );
378 if( SUCCEEDED( r ) )
380 StartLinkProcessor( pszFileName );
382 /* update file path */
383 heap_free(This->filepath);
384 This->filepath = strdupW(pszFileName);
386 This->bDirty = FALSE;
388 else
390 DeleteFileW( pszFileName );
391 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
395 return r;
398 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
400 IShellLinkImpl *This = impl_from_IPersistFile(iface);
401 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
402 return S_OK;
405 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
407 IShellLinkImpl *This = impl_from_IPersistFile(iface);
409 TRACE("(%p)->(%p)\n", This, filename);
411 if (!This->filepath)
413 *filename = NULL;
414 return S_FALSE;
417 *filename = CoTaskMemAlloc((strlenW(This->filepath) + 1) * sizeof(WCHAR));
418 if (!*filename) return E_OUTOFMEMORY;
420 strcpyW(*filename, This->filepath);
422 return S_OK;
425 static const IPersistFileVtbl pfvt =
427 IPersistFile_fnQueryInterface,
428 IPersistFile_fnAddRef,
429 IPersistFile_fnRelease,
430 IPersistFile_fnGetClassID,
431 IPersistFile_fnIsDirty,
432 IPersistFile_fnLoad,
433 IPersistFile_fnSave,
434 IPersistFile_fnSaveCompleted,
435 IPersistFile_fnGetCurFile
438 /************************************************************************
439 * IPersistStream_QueryInterface
441 static HRESULT WINAPI IPersistStream_fnQueryInterface(
442 IPersistStream* iface,
443 REFIID riid,
444 VOID** ppvObj)
446 IShellLinkImpl *This = impl_from_IPersistStream(iface);
447 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
450 /************************************************************************
451 * IPersistStream_Release
453 static ULONG WINAPI IPersistStream_fnRelease(
454 IPersistStream* iface)
456 IShellLinkImpl *This = impl_from_IPersistStream(iface);
457 return IShellLinkW_Release(&This->IShellLinkW_iface);
460 /************************************************************************
461 * IPersistStream_AddRef
463 static ULONG WINAPI IPersistStream_fnAddRef(
464 IPersistStream* iface)
466 IShellLinkImpl *This = impl_from_IPersistStream(iface);
467 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
470 /************************************************************************
471 * IPersistStream_GetClassID
474 static HRESULT WINAPI IPersistStream_fnGetClassID(
475 IPersistStream* iface,
476 CLSID* pClassID)
478 IShellLinkImpl *This = impl_from_IPersistStream(iface);
479 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
482 /************************************************************************
483 * IPersistStream_IsDirty (IPersistStream)
485 static HRESULT WINAPI IPersistStream_fnIsDirty(
486 IPersistStream* iface)
488 IShellLinkImpl *This = impl_from_IPersistStream(iface);
490 TRACE("(%p)\n", This);
492 return S_OK;
496 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
498 DWORD count;
499 USHORT len;
500 LPVOID temp;
501 LPWSTR str;
502 HRESULT r;
504 TRACE("%p\n", stm);
506 count = 0;
507 r = IStream_Read(stm, &len, sizeof(len), &count);
508 if ( FAILED (r) || ( count != sizeof(len) ) )
509 return E_FAIL;
511 if( unicode )
512 len *= sizeof (WCHAR);
514 TRACE("reading %d\n", len);
515 temp = heap_alloc(len + sizeof(WCHAR));
516 if( !temp )
517 return E_OUTOFMEMORY;
518 count = 0;
519 r = IStream_Read(stm, temp, len, &count);
520 if( FAILED (r) || ( count != len ) )
522 heap_free( temp );
523 return E_FAIL;
526 TRACE("read %s\n", debugstr_an(temp,len));
528 /* convert to unicode if necessary */
529 if( !unicode )
531 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
532 str = heap_alloc( (count+1)*sizeof (WCHAR) );
533 if( !str )
535 heap_free( temp );
536 return E_OUTOFMEMORY;
538 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
539 heap_free( temp );
541 else
543 count /= 2;
544 str = temp;
546 str[count] = 0;
548 *pstr = str;
550 return S_OK;
553 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
555 DWORD size;
556 ULONG count;
557 HRESULT r;
558 struct sized_chunk {
559 DWORD size;
560 unsigned char data[1];
561 } *chunk;
563 TRACE("%p\n",stm);
565 r = IStream_Read( stm, &size, sizeof(size), &count );
566 if( FAILED( r ) || count != sizeof(size) )
567 return E_FAIL;
569 chunk = heap_alloc( size );
570 if( !chunk )
571 return E_OUTOFMEMORY;
573 chunk->size = size;
574 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
575 if( FAILED( r ) || count != (size - sizeof(size)) )
577 heap_free( chunk );
578 return E_FAIL;
581 TRACE("Read %d bytes\n",chunk->size);
583 *data = chunk;
585 return S_OK;
588 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
590 const int label_sz = ARRAY_SIZE(volume->label);
591 LPSTR label;
592 int len;
594 volume->serial = vol->dwVolSerial;
595 volume->type = vol->dwType;
597 if( !vol->dwVolLabelOfs )
598 return FALSE;
599 if( vol->dwSize <= vol->dwVolLabelOfs )
600 return FALSE;
601 len = vol->dwSize - vol->dwVolLabelOfs;
603 label = (LPSTR) vol;
604 label += vol->dwVolLabelOfs;
605 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
607 return TRUE;
610 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
612 int len = 0, wlen;
613 LPWSTR path;
615 while( (len < maxlen) && p[len] )
616 len++;
618 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
619 path = heap_alloc((wlen + 1) * sizeof(WCHAR));
620 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
621 path[wlen] = 0;
623 return path;
626 static HRESULT Stream_LoadLocation( IStream *stm,
627 volume_info *volume, LPWSTR *path )
629 char *p = NULL;
630 LOCATION_INFO *loc;
631 HRESULT r;
632 DWORD n;
634 r = Stream_ReadChunk( stm, (LPVOID*) &p );
635 if( FAILED(r) )
636 return r;
638 loc = (LOCATION_INFO*) p;
639 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
641 heap_free( p );
642 return E_FAIL;
645 /* if there's valid local volume information, load it */
646 if( loc->dwVolTableOfs &&
647 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
649 LOCAL_VOLUME_INFO *volume_info;
651 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
652 Stream_LoadVolume( volume_info, volume );
655 /* if there's a local path, load it */
656 n = loc->dwLocalPathOfs;
657 if( n && (n < loc->dwTotalSize) )
658 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
660 TRACE("type %d serial %08x name %s path %s\n", volume->type,
661 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
663 heap_free( p );
664 return S_OK;
668 * The format of the advertised shortcut info seems to be:
670 * Offset Description
671 * ------ -----------
673 * 0 Length of the block (4 bytes, usually 0x314)
674 * 4 tag (dword)
675 * 8 string data in ASCII
676 * 8+0x104 string data in UNICODE
678 * In the original Win32 implementation the buffers are not initialized
679 * to zero, so data trailing the string is random garbage.
681 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
683 DWORD size;
684 ULONG count;
685 HRESULT r;
686 EXP_DARWIN_LINK buffer;
688 TRACE("%p\n",stm);
690 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
691 if( FAILED( r ) )
692 return r;
694 /* make sure that we read the size of the structure even on error */
695 size = sizeof buffer - sizeof (DWORD);
696 if( buffer.dbh.cbSize != sizeof buffer )
698 ERR("Ooops. This structure is not as expected...\n");
699 return E_FAIL;
702 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
703 if( FAILED( r ) )
704 return r;
706 if( count != size )
707 return E_FAIL;
709 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
711 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
713 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
714 return E_FAIL;
717 *str = heap_alloc((lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
718 lstrcpyW( *str, buffer.szwDarwinID );
720 return S_OK;
723 /************************************************************************
724 * IPersistStream_Load (IPersistStream)
726 static HRESULT WINAPI IPersistStream_fnLoad(
727 IPersistStream* iface,
728 IStream* stm)
730 LINK_HEADER hdr;
731 ULONG dwBytesRead;
732 BOOL unicode;
733 HRESULT r;
734 DWORD zero;
736 IShellLinkImpl *This = impl_from_IPersistStream(iface);
738 TRACE("%p %p\n", This, stm);
740 if( !stm )
741 return STG_E_INVALIDPOINTER;
743 dwBytesRead = 0;
744 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
745 if( FAILED( r ) )
746 return r;
748 if( dwBytesRead != sizeof(hdr))
749 return E_FAIL;
750 if( hdr.dwSize != sizeof(hdr))
751 return E_FAIL;
752 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
753 return E_FAIL;
755 /* free all the old stuff */
756 ILFree(This->pPidl);
757 This->pPidl = NULL;
758 memset( &This->volume, 0, sizeof This->volume );
759 heap_free(This->sPath);
760 This->sPath = NULL;
761 heap_free(This->sDescription);
762 This->sDescription = NULL;
763 heap_free(This->sPathRel);
764 This->sPathRel = NULL;
765 heap_free(This->sWorkDir);
766 This->sWorkDir = NULL;
767 heap_free(This->sArgs);
768 This->sArgs = NULL;
769 heap_free(This->sIcoPath);
770 This->sIcoPath = NULL;
771 heap_free(This->sProduct);
772 This->sProduct = NULL;
773 heap_free(This->sComponent);
774 This->sComponent = NULL;
776 This->wHotKey = hdr.wHotKey;
777 This->iIcoNdx = hdr.nIcon;
778 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
779 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
780 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
781 if (TRACE_ON(shell))
783 WCHAR sTemp[MAX_PATH];
784 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
785 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
786 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
787 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
788 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
789 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
792 /* load all the new stuff */
793 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
795 r = ILLoadFromStream( stm, &This->pPidl );
796 if( FAILED( r ) )
797 return r;
799 pdump(This->pPidl);
801 /* load the location information */
802 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
803 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
804 if( FAILED( r ) )
805 goto end;
807 unicode = hdr.dwFlags & SLDF_UNICODE;
808 if( hdr.dwFlags & SLDF_HAS_NAME )
810 r = Stream_LoadString( stm, unicode, &This->sDescription );
811 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
813 if( FAILED( r ) )
814 goto end;
816 if( hdr.dwFlags & SLDF_HAS_RELPATH )
818 r = Stream_LoadString( stm, unicode, &This->sPathRel );
819 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
821 if( FAILED( r ) )
822 goto end;
824 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
826 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
827 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
829 if( FAILED( r ) )
830 goto end;
832 if( hdr.dwFlags & SLDF_HAS_ARGS )
834 r = Stream_LoadString( stm, unicode, &This->sArgs );
835 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
837 if( FAILED( r ) )
838 goto end;
840 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
842 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
843 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
845 if( FAILED( r ) )
846 goto end;
848 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
850 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
851 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
853 if( FAILED( r ) )
854 goto end;
856 if( hdr.dwFlags & SLDF_HAS_DARWINID )
858 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
859 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
861 if( FAILED( r ) )
862 goto end;
864 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
865 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
867 /* Some lnk files have extra data blocks starting with a
868 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
869 * one with a 0xa0000003 signature. However these don't seem to matter
870 * too much.
872 WARN("Last word was not zero\n");
875 TRACE("OK\n");
877 pdump (This->pPidl);
879 return S_OK;
880 end:
881 return r;
884 /************************************************************************
885 * Stream_WriteString
887 * Helper function for IPersistStream_Save. Writes a unicode string
888 * with terminating nul byte to a stream, preceded by the its length.
890 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
892 USHORT len = lstrlenW( str ) + 1;
893 DWORD count;
894 HRESULT r;
896 r = IStream_Write( stm, &len, sizeof(len), &count );
897 if( FAILED( r ) )
898 return r;
900 len *= sizeof(WCHAR);
902 r = IStream_Write( stm, str, len, &count );
903 if( FAILED( r ) )
904 return r;
906 return S_OK;
909 /************************************************************************
910 * Stream_WriteLocationInfo
912 * Writes the location info to a stream
914 * FIXME: One day we might want to write the network volume information
915 * and the final path.
916 * Figure out how Windows deals with unicode paths here.
918 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
919 volume_info *volume )
921 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
922 LOCAL_VOLUME_INFO *vol;
923 LOCATION_INFO *loc;
924 LPSTR szLabel, szPath, szFinalPath;
925 ULONG count = 0;
926 HRESULT hr;
928 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
930 /* figure out the size of everything */
931 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
932 NULL, 0, NULL, NULL );
933 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
934 NULL, 0, NULL, NULL );
935 volume_info_size = sizeof *vol + label_size;
936 final_path_size = 1;
937 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
939 /* create pointers to everything */
940 loc = heap_alloc_zero(total_size);
941 vol = (LOCAL_VOLUME_INFO*) &loc[1];
942 szLabel = (LPSTR) &vol[1];
943 szPath = &szLabel[label_size];
944 szFinalPath = &szPath[path_size];
946 /* fill in the location information header */
947 loc->dwTotalSize = total_size;
948 loc->dwHeaderSize = sizeof (*loc);
949 loc->dwFlags = 1;
950 loc->dwVolTableOfs = sizeof (*loc);
951 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
952 loc->dwNetworkVolTableOfs = 0;
953 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
955 /* fill in the volume information */
956 vol->dwSize = volume_info_size;
957 vol->dwType = volume->type;
958 vol->dwVolSerial = volume->serial;
959 vol->dwVolLabelOfs = sizeof (*vol);
961 /* copy in the strings */
962 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
963 szLabel, label_size, NULL, NULL );
964 WideCharToMultiByte( CP_ACP, 0, path, -1,
965 szPath, path_size, NULL, NULL );
966 szFinalPath[0] = 0;
968 hr = IStream_Write( stm, loc, total_size, &count );
969 heap_free(loc);
971 return hr;
974 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
976 EXP_DARWIN_LINK *buffer;
978 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
979 buffer->dbh.cbSize = sizeof *buffer;
980 buffer->dbh.dwSignature = magic;
981 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
982 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
984 return buffer;
987 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
989 EXP_DARWIN_LINK *buffer;
990 ULONG count;
992 TRACE("%p\n",stm);
994 buffer = shelllink_build_darwinid( string, magic );
996 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
999 /************************************************************************
1000 * IPersistStream_Save (IPersistStream)
1002 * FIXME: makes assumptions about byte order
1004 static HRESULT WINAPI IPersistStream_fnSave(
1005 IPersistStream* iface,
1006 IStream* stm,
1007 BOOL fClearDirty)
1009 LINK_HEADER header;
1010 ULONG count;
1011 DWORD zero;
1012 HRESULT r;
1014 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1016 TRACE("%p %p %x\n", This, stm, fClearDirty);
1018 memset(&header, 0, sizeof(header));
1019 header.dwSize = sizeof(header);
1020 header.fStartup = This->iShowCmd;
1021 header.MagicGuid = CLSID_ShellLink;
1023 header.wHotKey = This->wHotKey;
1024 header.nIcon = This->iIcoNdx;
1025 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1026 if( This->pPidl )
1027 header.dwFlags |= SLDF_HAS_ID_LIST;
1028 if( This->sPath )
1029 header.dwFlags |= SLDF_HAS_LINK_INFO;
1030 if( This->sDescription )
1031 header.dwFlags |= SLDF_HAS_NAME;
1032 if( This->sWorkDir )
1033 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1034 if( This->sArgs )
1035 header.dwFlags |= SLDF_HAS_ARGS;
1036 if( This->sIcoPath )
1037 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1038 if( This->sProduct )
1039 header.dwFlags |= SLDF_HAS_LOGO3ID;
1040 if( This->sComponent )
1041 header.dwFlags |= SLDF_HAS_DARWINID;
1043 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1044 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1045 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1047 /* write the Shortcut header */
1048 r = IStream_Write( stm, &header, sizeof(header), &count );
1049 if( FAILED( r ) )
1051 ERR("Write failed at %d\n",__LINE__);
1052 return r;
1055 TRACE("Writing pidl\n");
1057 /* write the PIDL to the shortcut */
1058 if( This->pPidl )
1060 r = ILSaveToStream( stm, This->pPidl );
1061 if( FAILED( r ) )
1063 ERR("Failed to write PIDL at %d\n",__LINE__);
1064 return r;
1068 if( This->sPath )
1069 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1071 if( This->sDescription )
1072 r = Stream_WriteString( stm, This->sDescription );
1074 if( This->sPathRel )
1075 r = Stream_WriteString( stm, This->sPathRel );
1077 if( This->sWorkDir )
1078 r = Stream_WriteString( stm, This->sWorkDir );
1080 if( This->sArgs )
1081 r = Stream_WriteString( stm, This->sArgs );
1083 if( This->sIcoPath )
1084 r = Stream_WriteString( stm, This->sIcoPath );
1086 if( This->sProduct )
1087 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1089 if( This->sComponent )
1090 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1092 /* the last field is a single zero dword */
1093 zero = 0;
1094 r = IStream_Write( stm, &zero, sizeof zero, &count );
1096 return S_OK;
1099 /************************************************************************
1100 * IPersistStream_GetSizeMax (IPersistStream)
1102 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1103 IPersistStream* iface,
1104 ULARGE_INTEGER* pcbSize)
1106 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1108 TRACE("(%p)\n", This);
1110 return E_NOTIMPL;
1113 static const IPersistStreamVtbl psvt =
1115 IPersistStream_fnQueryInterface,
1116 IPersistStream_fnAddRef,
1117 IPersistStream_fnRelease,
1118 IPersistStream_fnGetClassID,
1119 IPersistStream_fnIsDirty,
1120 IPersistStream_fnLoad,
1121 IPersistStream_fnSave,
1122 IPersistStream_fnGetSizeMax
1125 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1127 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1128 return FALSE;
1129 return TRUE;
1132 /**************************************************************************
1133 * ShellLink_UpdatePath
1134 * update absolute path in sPath using relative path in sPathRel
1136 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1138 if (!path || !psPath)
1139 return E_INVALIDARG;
1141 if (!*psPath && sPathRel) {
1142 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1143 LPWSTR final = NULL;
1145 /* first try if [directory of link file] + [relative path] finds an existing file */
1147 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1148 if( !final )
1149 final = buffer;
1150 lstrcpyW(final, sPathRel);
1152 *abs_path = '\0';
1154 if (SHELL_ExistsFileW(buffer)) {
1155 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1156 lstrcpyW(abs_path, buffer);
1157 } else {
1158 /* try if [working directory] + [relative path] finds an existing file */
1159 if (sWorkDir) {
1160 lstrcpyW(buffer, sWorkDir);
1161 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1163 if (SHELL_ExistsFileW(buffer))
1164 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1165 lstrcpyW(abs_path, buffer);
1169 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1170 if (!*abs_path)
1171 lstrcpyW(abs_path, sPathRel);
1173 *psPath = heap_alloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1174 if (!*psPath)
1175 return E_OUTOFMEMORY;
1177 lstrcpyW(*psPath, abs_path);
1180 return S_OK;
1183 /**************************************************************************
1184 * IShellLink_ConstructFromFile
1186 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1187 LPCITEMIDLIST pidl, IUnknown **ppv)
1189 IShellLinkW* psl;
1191 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1193 if (SUCCEEDED(hr)) {
1194 IPersistFile* ppf;
1196 *ppv = NULL;
1198 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1200 if (SUCCEEDED(hr)) {
1201 WCHAR path[MAX_PATH];
1203 if (SHGetPathFromIDListW(pidl, path))
1204 hr = IPersistFile_Load(ppf, path, 0);
1205 else
1206 hr = E_FAIL;
1208 if (SUCCEEDED(hr))
1209 *ppv = (IUnknown*)psl;
1211 IPersistFile_Release(ppf);
1214 if (!*ppv)
1215 IShellLinkW_Release(psl);
1218 return hr;
1221 /**************************************************************************
1222 * IShellLinkA_QueryInterface
1224 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1226 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1227 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1230 /******************************************************************************
1231 * IShellLinkA_AddRef
1233 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1235 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1236 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1239 /******************************************************************************
1240 * IShellLinkA_Release
1242 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1244 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1245 return IShellLinkW_Release(&This->IShellLinkW_iface);
1248 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1249 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1251 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1252 HRESULT res = S_OK;
1254 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1255 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1257 if (This->sComponent || This->sProduct)
1258 return S_FALSE;
1260 if (cchMaxPath)
1261 pszFile[0] = 0;
1262 if (This->sPath && This->sPath[0])
1263 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1264 pszFile, cchMaxPath, NULL, NULL);
1265 else
1266 res = S_FALSE;
1268 if (pfd)
1270 memset(pfd, 0, sizeof(*pfd));
1272 if (res == S_OK)
1274 char path[MAX_PATH];
1275 WIN32_FILE_ATTRIBUTE_DATA fad;
1277 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1279 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1281 pfd->dwFileAttributes = fad.dwFileAttributes;
1282 pfd->ftCreationTime = fad.ftCreationTime;
1283 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1284 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1285 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1286 pfd->nFileSizeLow = fad.nFileSizeLow;
1289 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1291 if (GetShortPathNameA(path, path, MAX_PATH))
1293 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1297 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1298 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1299 wine_dbgstr_a(pfd->cAlternateFileName));
1302 return res;
1305 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1307 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1308 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1311 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1313 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1314 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1317 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1318 INT cchMaxName)
1320 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1322 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1324 if( cchMaxName )
1325 pszName[0] = 0;
1326 if( This->sDescription )
1327 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1328 pszName, cchMaxName, NULL, NULL);
1330 return S_OK;
1333 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1335 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1336 WCHAR *descrW;
1337 HRESULT hr;
1339 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1341 if (pszName)
1343 descrW = heap_strdupAtoW(pszName);
1344 if (!descrW) return E_OUTOFMEMORY;
1346 else
1347 descrW = NULL;
1349 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1350 heap_free(descrW);
1352 return hr;
1355 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1356 INT cchMaxPath)
1358 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1360 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1362 if( cchMaxPath )
1363 pszDir[0] = 0;
1364 if( This->sWorkDir )
1365 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1366 pszDir, cchMaxPath, NULL, NULL);
1368 return S_OK;
1371 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1373 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1374 WCHAR *dirW;
1375 HRESULT hr;
1377 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1379 dirW = heap_strdupAtoW(pszDir);
1380 if (!dirW) return E_OUTOFMEMORY;
1382 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1383 heap_free(dirW);
1385 return hr;
1388 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1390 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1392 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1394 if( cchMaxPath )
1395 pszArgs[0] = 0;
1396 if( This->sArgs )
1397 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1398 pszArgs, cchMaxPath, NULL, NULL);
1400 return S_OK;
1403 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1405 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1406 WCHAR *argsW;
1407 HRESULT hr;
1409 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1411 if (pszArgs)
1413 argsW = heap_strdupAtoW(pszArgs);
1414 if (!argsW) return E_OUTOFMEMORY;
1416 else
1417 argsW = NULL;
1419 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1420 heap_free(argsW);
1422 return hr;
1425 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1427 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1428 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1431 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1433 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1434 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1437 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1439 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1440 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1443 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1445 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1446 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1449 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1450 INT cchIconPath, INT *piIcon)
1452 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1454 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1456 *piIcon = This->iIcoNdx;
1458 if (This->sIcoPath)
1459 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1460 else
1461 pszIconPath[0] = 0;
1463 return S_OK;
1466 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1468 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1469 WCHAR *pathW = NULL;
1470 HRESULT hr;
1472 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1474 if (path)
1476 pathW = heap_strdupAtoW(path);
1477 if (!pathW)
1478 return E_OUTOFMEMORY;
1481 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1482 heap_free(pathW);
1484 return hr;
1487 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1488 DWORD dwReserved)
1490 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1491 WCHAR *pathW;
1492 HRESULT hr;
1494 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1496 pathW = heap_strdupAtoW(pszPathRel);
1497 if (!pathW) return E_OUTOFMEMORY;
1499 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1500 heap_free(pathW);
1502 return hr;
1505 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1507 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1509 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1511 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1514 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1516 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1517 HRESULT r;
1518 LPWSTR str;
1520 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1522 if (!pszFile) return E_INVALIDARG;
1524 str = heap_strdupAtoW(pszFile);
1525 if( !str )
1526 return E_OUTOFMEMORY;
1528 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1529 heap_free( str );
1531 return r;
1534 /**************************************************************************
1535 * IShellLink Implementation
1538 static const IShellLinkAVtbl slvt =
1540 IShellLinkA_fnQueryInterface,
1541 IShellLinkA_fnAddRef,
1542 IShellLinkA_fnRelease,
1543 IShellLinkA_fnGetPath,
1544 IShellLinkA_fnGetIDList,
1545 IShellLinkA_fnSetIDList,
1546 IShellLinkA_fnGetDescription,
1547 IShellLinkA_fnSetDescription,
1548 IShellLinkA_fnGetWorkingDirectory,
1549 IShellLinkA_fnSetWorkingDirectory,
1550 IShellLinkA_fnGetArguments,
1551 IShellLinkA_fnSetArguments,
1552 IShellLinkA_fnGetHotkey,
1553 IShellLinkA_fnSetHotkey,
1554 IShellLinkA_fnGetShowCmd,
1555 IShellLinkA_fnSetShowCmd,
1556 IShellLinkA_fnGetIconLocation,
1557 IShellLinkA_fnSetIconLocation,
1558 IShellLinkA_fnSetRelativePath,
1559 IShellLinkA_fnResolve,
1560 IShellLinkA_fnSetPath
1564 /**************************************************************************
1565 * IShellLinkW_fnQueryInterface
1567 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1568 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1570 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1572 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1574 *ppvObj = NULL;
1576 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1578 *ppvObj = &This->IShellLinkA_iface;
1580 else if(IsEqualIID(riid, &IID_IShellLinkW))
1582 *ppvObj = &This->IShellLinkW_iface;
1584 else if(IsEqualIID(riid, &IID_IPersistFile))
1586 *ppvObj = &This->IPersistFile_iface;
1588 else if(IsEqualIID(riid, &IID_IPersistStream))
1590 *ppvObj = &This->IPersistStream_iface;
1592 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1594 *ppvObj = &This->IShellLinkDataList_iface;
1596 else if(IsEqualIID(riid, &IID_IShellExtInit))
1598 *ppvObj = &This->IShellExtInit_iface;
1600 else if(IsEqualIID(riid, &IID_IContextMenu))
1602 *ppvObj = &This->IContextMenu_iface;
1604 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1606 *ppvObj = &This->IObjectWithSite_iface;
1608 else if(IsEqualIID(riid, &IID_IPropertyStore))
1610 *ppvObj = &This->IPropertyStore_iface;
1613 if(*ppvObj)
1615 IUnknown_AddRef((IUnknown*)*ppvObj);
1616 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1617 return S_OK;
1619 ERR("-- Interface: E_NOINTERFACE\n");
1620 return E_NOINTERFACE;
1623 /******************************************************************************
1624 * IShellLinkW_fnAddRef
1626 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1628 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1629 ULONG ref = InterlockedIncrement(&This->ref);
1631 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1633 return ref;
1636 /******************************************************************************
1637 * IShellLinkW_fnRelease
1639 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1641 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1642 ULONG refCount = InterlockedDecrement(&This->ref);
1644 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1646 if (refCount)
1647 return refCount;
1649 TRACE("-- destroying IShellLink(%p)\n",This);
1651 heap_free(This->sIcoPath);
1652 heap_free(This->sArgs);
1653 heap_free(This->sWorkDir);
1654 heap_free(This->sDescription);
1655 heap_free(This->sPath);
1656 heap_free(This->sPathRel);
1657 heap_free(This->sProduct);
1658 heap_free(This->sComponent);
1659 heap_free(This->filepath);
1661 if (This->site)
1662 IUnknown_Release( This->site );
1664 if (This->pPidl)
1665 ILFree(This->pPidl);
1667 LocalFree(This);
1669 return 0;
1672 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1674 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1675 HRESULT res = S_OK;
1677 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1678 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1680 if (This->sComponent || This->sProduct)
1681 return S_FALSE;
1683 if (cchMaxPath)
1684 pszFile[0] = 0;
1685 if (This->sPath)
1686 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1687 else
1688 res = S_FALSE;
1690 if (pfd)
1692 memset(pfd, 0, sizeof(*pfd));
1694 if (res == S_OK)
1696 WCHAR path[MAX_PATH];
1697 WIN32_FILE_ATTRIBUTE_DATA fad;
1699 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1701 pfd->dwFileAttributes = fad.dwFileAttributes;
1702 pfd->ftCreationTime = fad.ftCreationTime;
1703 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1704 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1705 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1706 pfd->nFileSizeLow = fad.nFileSizeLow;
1709 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1711 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1713 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1717 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1718 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1719 wine_dbgstr_w(pfd->cAlternateFileName));
1722 return res;
1725 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1727 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1729 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1731 if (!This->pPidl)
1733 *ppidl = NULL;
1734 return S_FALSE;
1736 *ppidl = ILClone(This->pPidl);
1737 return S_OK;
1740 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1742 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1743 WCHAR path[MAX_PATH];
1745 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1747 if( This->pPidl )
1748 ILFree( This->pPidl );
1749 This->pPidl = ILClone( pidl );
1750 if( !This->pPidl )
1751 return E_FAIL;
1753 heap_free( This->sPath );
1754 This->sPath = NULL;
1756 if ( SHGetPathFromIDListW( pidl, path ) )
1758 This->sPath = heap_alloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1759 if (!This->sPath)
1760 return E_OUTOFMEMORY;
1762 lstrcpyW(This->sPath, path);
1765 This->bDirty = TRUE;
1767 return S_OK;
1770 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1772 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1774 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1776 pszName[0] = 0;
1777 if( This->sDescription )
1778 lstrcpynW( pszName, This->sDescription, cchMaxName );
1780 return S_OK;
1783 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1785 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1787 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1789 heap_free(This->sDescription);
1790 if (pszName)
1792 This->sDescription = heap_alloc((lstrlenW( pszName )+1)*sizeof(WCHAR) );
1793 if ( !This->sDescription )
1794 return E_OUTOFMEMORY;
1796 lstrcpyW( This->sDescription, pszName );
1798 else
1799 This->sDescription = NULL;
1800 This->bDirty = TRUE;
1802 return S_OK;
1805 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1807 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1809 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1811 if( cchMaxPath )
1812 pszDir[0] = 0;
1813 if( This->sWorkDir )
1814 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1816 return S_OK;
1819 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1821 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1823 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1825 heap_free(This->sWorkDir);
1826 This->sWorkDir = heap_alloc((lstrlenW( pszDir ) + 1) * sizeof (WCHAR) );
1827 if ( !This->sWorkDir )
1828 return E_OUTOFMEMORY;
1829 lstrcpyW( This->sWorkDir, pszDir );
1830 This->bDirty = TRUE;
1832 return S_OK;
1835 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1837 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1839 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1841 if( cchMaxPath )
1842 pszArgs[0] = 0;
1843 if( This->sArgs )
1844 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1846 return S_OK;
1849 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1851 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1853 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1855 heap_free(This->sArgs);
1856 if (pszArgs)
1858 This->sArgs = heap_alloc((lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1859 if ( !This->sArgs )
1860 return E_OUTOFMEMORY;
1861 lstrcpyW( This->sArgs, pszArgs );
1863 else This->sArgs = NULL;
1865 This->bDirty = TRUE;
1867 return S_OK;
1870 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1872 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1874 TRACE("(%p)->(%p)\n",This, pwHotkey);
1876 *pwHotkey=This->wHotKey;
1878 return S_OK;
1881 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1883 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1885 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1887 This->wHotKey = wHotkey;
1888 This->bDirty = TRUE;
1890 return S_OK;
1893 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1895 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1897 TRACE("(%p)->(%p)\n",This, piShowCmd);
1899 *piShowCmd = This->iShowCmd;
1901 return S_OK;
1904 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1906 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1908 TRACE("(%p)->(%d)\n", This, iShowCmd);
1910 This->iShowCmd = iShowCmd;
1911 This->bDirty = TRUE;
1913 return S_OK;
1916 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1918 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1920 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1922 *piIcon = This->iIcoNdx;
1924 if (This->sIcoPath)
1925 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1926 else
1927 pszIconPath[0] = 0;
1929 return S_OK;
1932 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1934 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1936 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1938 heap_free(This->sIcoPath);
1939 if (path)
1941 size_t len = (strlenW(path) + 1) * sizeof(WCHAR);
1942 This->sIcoPath = heap_alloc(len);
1943 if (!This->sIcoPath)
1944 return E_OUTOFMEMORY;
1945 memcpy(This->sIcoPath, path, len);
1947 else
1948 This->sIcoPath = NULL;
1949 This->iIcoNdx = icon;
1950 This->bDirty = TRUE;
1952 return S_OK;
1955 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1957 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1959 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1961 heap_free(This->sPathRel);
1962 This->sPathRel = heap_alloc((lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1963 if ( !This->sPathRel )
1964 return E_OUTOFMEMORY;
1965 lstrcpyW( This->sPathRel, pszPathRel );
1966 This->bDirty = TRUE;
1968 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1971 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1973 HRESULT hr = S_OK;
1974 BOOL bSuccess;
1976 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1978 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1980 /*FIXME: use IResolveShellLink interface */
1982 if (!This->sPath && This->pPidl) {
1983 WCHAR buffer[MAX_PATH];
1985 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1987 if (bSuccess && *buffer) {
1988 This->sPath = heap_alloc((lstrlenW(buffer)+1)*sizeof(WCHAR));
1989 if (!This->sPath)
1990 return E_OUTOFMEMORY;
1992 lstrcpyW(This->sPath, buffer);
1994 This->bDirty = TRUE;
1995 } else
1996 hr = S_OK; /* don't report an error occurred while just caching information */
1999 if (!This->sIcoPath && This->sPath) {
2000 This->sIcoPath = heap_alloc((lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2001 if (!This->sIcoPath)
2002 return E_OUTOFMEMORY;
2004 lstrcpyW(This->sIcoPath, This->sPath);
2005 This->iIcoNdx = 0;
2007 This->bDirty = TRUE;
2010 return hr;
2013 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2015 LPWSTR ret;
2016 LPCWSTR p;
2017 DWORD len;
2019 if( !str )
2020 return NULL;
2022 p = strchrW( str, ':' );
2023 if( !p )
2024 return NULL;
2025 len = p - str;
2026 ret = heap_alloc(sizeof(WCHAR)*(len+1));
2027 if( !ret )
2028 return ret;
2029 memcpy( ret, str, sizeof(WCHAR)*len );
2030 ret[len] = 0;
2031 return ret;
2034 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2036 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2037 WCHAR szGuid[39];
2038 HRESULT r;
2039 GUID guid;
2040 int len;
2042 while( str[0] )
2044 /* each segment must start with two colons */
2045 if( str[0] != ':' || str[1] != ':' )
2046 return E_FAIL;
2048 /* the last segment is just two colons */
2049 if( !str[2] )
2050 break;
2051 str += 2;
2053 /* there must be a colon straight after a guid */
2054 p = strchrW( str, ':' );
2055 if( !p )
2056 return E_FAIL;
2057 len = p - str;
2058 if( len != 38 )
2059 return E_FAIL;
2061 /* get the guid, and check it's validly formatted */
2062 memcpy( szGuid, str, sizeof(WCHAR)*len );
2063 szGuid[len] = 0;
2064 r = CLSIDFromString( szGuid, &guid );
2065 if( r != S_OK )
2066 return r;
2067 str = p + 1;
2069 /* match it up to a guid that we care about */
2070 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2071 szComponent = str;
2072 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2073 szProduct = str;
2074 else
2075 return E_FAIL;
2077 /* skip to the next field */
2078 str = strchrW( str, ':' );
2079 if( !str )
2080 return E_FAIL;
2083 /* we have to have a component for an advertised shortcut */
2084 if( !szComponent )
2085 return E_FAIL;
2087 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2088 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2090 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2091 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2093 return S_OK;
2096 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2098 const int label_sz = ARRAY_SIZE(volume->label);
2099 WCHAR drive[] = { path[0], ':', '\\', 0 };
2100 BOOL r;
2102 volume->type = GetDriveTypeW(drive);
2103 r = GetVolumeInformationW(drive, volume->label, label_sz,
2104 &volume->serial, NULL, NULL, NULL, 0);
2105 TRACE("r = %d type %d serial %08x name %s\n", r,
2106 volume->type, volume->serial, debugstr_w(volume->label));
2107 return r;
2110 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2112 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2113 WCHAR buffer[MAX_PATH];
2114 LPWSTR fname, unquoted = NULL;
2115 HRESULT hr = S_OK;
2116 UINT len;
2118 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2120 if (!pszFile) return E_INVALIDARG;
2122 /* quotes at the ends of the string are stripped */
2123 len = lstrlenW(pszFile);
2124 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2126 unquoted = strdupW(pszFile);
2127 PathUnquoteSpacesW(unquoted);
2128 pszFile = unquoted;
2131 /* any other quote marks are invalid */
2132 if (strchrW(pszFile, '"'))
2134 heap_free(unquoted);
2135 return S_FALSE;
2138 heap_free(This->sPath);
2139 This->sPath = NULL;
2141 heap_free(This->sComponent);
2142 This->sComponent = NULL;
2144 if (This->pPidl)
2145 ILFree(This->pPidl);
2146 This->pPidl = NULL;
2148 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2150 if (*pszFile == '\0')
2151 *buffer = '\0';
2152 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2153 return E_FAIL;
2154 else if(!PathFileExistsW(buffer) &&
2155 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2156 hr = S_FALSE;
2158 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2159 ShellLink_GetVolumeInfo(buffer, &This->volume);
2161 This->sPath = heap_alloc( (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2162 if (!This->sPath)
2164 heap_free(unquoted);
2165 return E_OUTOFMEMORY;
2168 lstrcpyW(This->sPath, buffer);
2170 This->bDirty = TRUE;
2171 heap_free(unquoted);
2173 return hr;
2176 /**************************************************************************
2177 * IShellLinkW Implementation
2180 static const IShellLinkWVtbl slvtw =
2182 IShellLinkW_fnQueryInterface,
2183 IShellLinkW_fnAddRef,
2184 IShellLinkW_fnRelease,
2185 IShellLinkW_fnGetPath,
2186 IShellLinkW_fnGetIDList,
2187 IShellLinkW_fnSetIDList,
2188 IShellLinkW_fnGetDescription,
2189 IShellLinkW_fnSetDescription,
2190 IShellLinkW_fnGetWorkingDirectory,
2191 IShellLinkW_fnSetWorkingDirectory,
2192 IShellLinkW_fnGetArguments,
2193 IShellLinkW_fnSetArguments,
2194 IShellLinkW_fnGetHotkey,
2195 IShellLinkW_fnSetHotkey,
2196 IShellLinkW_fnGetShowCmd,
2197 IShellLinkW_fnSetShowCmd,
2198 IShellLinkW_fnGetIconLocation,
2199 IShellLinkW_fnSetIconLocation,
2200 IShellLinkW_fnSetRelativePath,
2201 IShellLinkW_fnResolve,
2202 IShellLinkW_fnSetPath
2205 static HRESULT WINAPI
2206 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2208 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2209 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2212 static ULONG WINAPI
2213 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2215 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2216 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2219 static ULONG WINAPI
2220 ShellLink_DataList_Release( IShellLinkDataList* iface )
2222 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2223 return IShellLinkW_Release(&This->IShellLinkW_iface);
2226 static HRESULT WINAPI
2227 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2229 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2230 return E_NOTIMPL;
2233 static HRESULT WINAPI
2234 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2236 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2237 LPVOID block = NULL;
2238 HRESULT r = E_FAIL;
2240 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2242 switch (dwSig)
2244 case EXP_DARWIN_ID_SIG:
2245 if (!This->sComponent)
2246 break;
2247 block = shelllink_build_darwinid( This->sComponent, dwSig );
2248 r = S_OK;
2249 break;
2250 case EXP_SZ_LINK_SIG:
2251 case NT_CONSOLE_PROPS_SIG:
2252 case NT_FE_CONSOLE_PROPS_SIG:
2253 case EXP_SPECIAL_FOLDER_SIG:
2254 case EXP_SZ_ICON_SIG:
2255 FIXME("valid but unhandled datablock %08x\n", dwSig);
2256 break;
2257 default:
2258 ERR("unknown datablock %08x\n", dwSig);
2260 *ppDataBlock = block;
2261 return r;
2264 static HRESULT WINAPI
2265 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2267 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2268 return E_NOTIMPL;
2271 static HRESULT WINAPI
2272 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2274 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2275 DWORD flags = 0;
2277 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2279 /* FIXME: add more */
2280 if (This->sArgs)
2281 flags |= SLDF_HAS_ARGS;
2282 if (This->sComponent)
2283 flags |= SLDF_HAS_DARWINID;
2284 if (This->sIcoPath)
2285 flags |= SLDF_HAS_ICONLOCATION;
2286 if (This->sProduct)
2287 flags |= SLDF_HAS_LOGO3ID;
2288 if (This->pPidl)
2289 flags |= SLDF_HAS_ID_LIST;
2291 *pdwFlags = flags;
2293 return S_OK;
2296 static HRESULT WINAPI
2297 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2299 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2300 return E_NOTIMPL;
2303 static const IShellLinkDataListVtbl dlvt =
2305 ShellLink_DataList_QueryInterface,
2306 ShellLink_DataList_AddRef,
2307 ShellLink_DataList_Release,
2308 ShellLink_AddDataBlock,
2309 ShellLink_CopyDataBlock,
2310 ShellLink_RemoveDataBlock,
2311 ShellLink_GetFlags,
2312 ShellLink_SetFlags
2315 static HRESULT WINAPI
2316 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2318 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2319 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2322 static ULONG WINAPI
2323 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2325 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2326 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2329 static ULONG WINAPI
2330 ShellLink_ExtInit_Release( IShellExtInit* iface )
2332 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2333 return IShellLinkW_Release(&This->IShellLinkW_iface);
2336 /**************************************************************************
2337 * ShellLink implementation of IShellExtInit::Initialize()
2339 * Loads the shelllink from the dataobject the shell is pointing to.
2341 static HRESULT WINAPI
2342 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2343 IDataObject *pdtobj, HKEY hkeyProgID )
2345 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2346 FORMATETC format;
2347 STGMEDIUM stgm;
2348 UINT count;
2349 HRESULT r = E_FAIL;
2351 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2353 if( !pdtobj )
2354 return r;
2356 format.cfFormat = CF_HDROP;
2357 format.ptd = NULL;
2358 format.dwAspect = DVASPECT_CONTENT;
2359 format.lindex = -1;
2360 format.tymed = TYMED_HGLOBAL;
2362 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2363 return r;
2365 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2366 if( count == 1 )
2368 LPWSTR path;
2370 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2371 count++;
2372 path = heap_alloc(count*sizeof(WCHAR) );
2373 if( path )
2375 IPersistFile *pf = &This->IPersistFile_iface;
2377 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2378 r = IPersistFile_Load( pf, path, 0 );
2379 heap_free( path );
2382 ReleaseStgMedium( &stgm );
2384 return r;
2387 static const IShellExtInitVtbl eivt =
2389 ShellLink_ExtInit_QueryInterface,
2390 ShellLink_ExtInit_AddRef,
2391 ShellLink_ExtInit_Release,
2392 ShellLink_ExtInit_Initialize
2395 static HRESULT WINAPI
2396 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2398 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2399 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2402 static ULONG WINAPI
2403 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2405 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2406 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2409 static ULONG WINAPI
2410 ShellLink_ContextMenu_Release( IContextMenu* iface )
2412 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2413 return IShellLinkW_Release(&This->IShellLinkW_iface);
2416 static HRESULT WINAPI
2417 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2418 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2420 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2421 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2422 MENUITEMINFOW mii;
2423 int id = 1;
2425 TRACE("%p %p %u %u %u %u\n", This,
2426 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2428 if ( !hmenu )
2429 return E_INVALIDARG;
2431 memset( &mii, 0, sizeof mii );
2432 mii.cbSize = sizeof mii;
2433 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2434 mii.dwTypeData = szOpen;
2435 mii.cch = strlenW( mii.dwTypeData );
2436 mii.wID = idCmdFirst + id++;
2437 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2438 mii.fType = MFT_STRING;
2439 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2440 return E_FAIL;
2441 This->iIdOpen = 0;
2443 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2446 static LPWSTR
2447 shelllink_get_msi_component_path( LPWSTR component )
2449 LPWSTR path;
2450 DWORD r, sz = 0;
2452 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2453 if (r != ERROR_SUCCESS)
2454 return NULL;
2456 sz++;
2457 path = heap_alloc( sz*sizeof(WCHAR) );
2458 r = CommandLineFromMsiDescriptor( component, path, &sz );
2459 if (r != ERROR_SUCCESS)
2461 heap_free( path );
2462 path = NULL;
2465 TRACE("returning %s\n", debugstr_w( path ) );
2467 return path;
2470 static HRESULT WINAPI
2471 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2473 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2474 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2475 SHELLEXECUTEINFOW sei;
2476 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2477 LPWSTR args = NULL;
2478 LPWSTR path = NULL;
2479 HRESULT r;
2481 TRACE("%p %p\n", This, lpici );
2483 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2484 return E_INVALIDARG;
2486 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2488 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2489 return E_INVALIDARG;
2492 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2493 if ( FAILED( r ) )
2494 return r;
2496 if ( This->sComponent )
2498 path = shelllink_get_msi_component_path( This->sComponent );
2499 if (!path)
2500 return E_FAIL;
2502 else
2503 path = strdupW( This->sPath );
2505 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2506 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2508 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2509 DWORD len = 2;
2511 if ( This->sArgs )
2512 len += lstrlenW( This->sArgs );
2513 if ( iciex->lpParametersW )
2514 len += lstrlenW( iciex->lpParametersW );
2516 args = heap_alloc( len*sizeof(WCHAR) );
2517 args[0] = 0;
2518 if ( This->sArgs )
2519 lstrcatW( args, This->sArgs );
2520 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2522 static const WCHAR space[] = { ' ', 0 };
2523 lstrcatW( args, space );
2524 lstrcatW( args, iciex->lpParametersW );
2528 memset( &sei, 0, sizeof sei );
2529 sei.cbSize = sizeof sei;
2530 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2531 sei.lpFile = path;
2532 sei.nShow = This->iShowCmd;
2533 sei.lpIDList = This->pPidl;
2534 sei.lpDirectory = This->sWorkDir;
2535 sei.lpParameters = args;
2536 sei.lpVerb = szOpen;
2538 if( ShellExecuteExW( &sei ) )
2539 r = S_OK;
2540 else
2541 r = E_FAIL;
2543 heap_free( args );
2544 heap_free( path );
2546 return r;
2549 static HRESULT WINAPI
2550 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2551 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2553 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2555 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2556 idCmd, uType, pwReserved, pszName, cchMax );
2558 return E_NOTIMPL;
2561 static const IContextMenuVtbl cmvt =
2563 ShellLink_ContextMenu_QueryInterface,
2564 ShellLink_ContextMenu_AddRef,
2565 ShellLink_ContextMenu_Release,
2566 ShellLink_QueryContextMenu,
2567 ShellLink_InvokeCommand,
2568 ShellLink_GetCommandString
2571 static HRESULT WINAPI
2572 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2574 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2575 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2578 static ULONG WINAPI
2579 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2581 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2582 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2585 static ULONG WINAPI
2586 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2588 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2589 return IShellLinkW_Release(&This->IShellLinkW_iface);
2592 static HRESULT WINAPI
2593 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2595 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2597 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2599 if ( !This->site )
2600 return E_FAIL;
2601 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2604 static HRESULT WINAPI
2605 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2607 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2609 TRACE("%p %p\n", iface, punk);
2611 if ( punk )
2612 IUnknown_AddRef( punk );
2614 if( This->site )
2615 IUnknown_Release( This->site );
2617 This->site = punk;
2619 return S_OK;
2622 static const IObjectWithSiteVtbl owsvt =
2624 ShellLink_ObjectWithSite_QueryInterface,
2625 ShellLink_ObjectWithSite_AddRef,
2626 ShellLink_ObjectWithSite_Release,
2627 ShellLink_SetSite,
2628 ShellLink_GetSite,
2631 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2633 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2634 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2637 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2639 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2640 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2643 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2645 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2646 return IShellLinkW_Release(&This->IShellLinkW_iface);
2649 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2651 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2652 FIXME("(%p)->(%p): stub\n", This, props);
2653 return E_NOTIMPL;
2656 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2658 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2659 FIXME("(%p)->(%d %p): stub\n", This, propid, key);
2660 return E_NOTIMPL;
2663 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2665 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2666 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2667 return E_NOTIMPL;
2670 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2672 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2673 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2674 return S_OK;
2677 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2679 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2680 FIXME("(%p): stub\n", This);
2681 return S_OK;
2684 static const IPropertyStoreVtbl propertystorevtbl = {
2685 propertystore_QueryInterface,
2686 propertystore_AddRef,
2687 propertystore_Release,
2688 propertystore_GetCount,
2689 propertystore_GetAt,
2690 propertystore_GetValue,
2691 propertystore_SetValue,
2692 propertystore_Commit
2695 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2697 IShellLinkImpl * sl;
2698 HRESULT r;
2700 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2702 *obj = NULL;
2704 if (outer)
2705 return CLASS_E_NOAGGREGATION;
2707 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2708 if (!sl)
2709 return E_OUTOFMEMORY;
2711 sl->ref = 1;
2712 sl->IShellLinkA_iface.lpVtbl = &slvt;
2713 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2714 sl->IPersistFile_iface.lpVtbl = &pfvt;
2715 sl->IPersistStream_iface.lpVtbl = &psvt;
2716 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2717 sl->IShellExtInit_iface.lpVtbl = &eivt;
2718 sl->IContextMenu_iface.lpVtbl = &cmvt;
2719 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2720 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2721 sl->iShowCmd = SW_SHOWNORMAL;
2722 sl->bDirty = FALSE;
2723 sl->iIdOpen = -1;
2724 sl->site = NULL;
2725 sl->filepath = NULL;
2727 TRACE("(%p)\n", sl);
2729 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2730 IShellLinkW_Release( &sl->IShellLinkW_iface );
2731 return r;