cmd: DIR command outputs free space for the path.
[wine.git] / dlls / shell32 / shelllink.c
blob8fe1529891b7317a022788636b2c6475472fc295
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 #include "wine/debug.h"
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winreg.h"
43 #include "winuser.h"
44 #include "wingdi.h"
45 #include "shlobj.h"
47 #include "pidl.h"
48 #include "shell32_main.h"
49 #include "shlguid.h"
50 #include "shlwapi.h"
51 #include "msi.h"
52 #include "appmgmt.h"
54 #include "initguid.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(shell);
58 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
59 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
60 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
61 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 /* link file formats */
65 #include "pshpack1.h"
67 typedef struct _LINK_HEADER
69 DWORD dwSize; /* 0x00 size of the header - 0x4c */
70 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
71 DWORD dwFlags; /* 0x14 describes elements following */
72 DWORD dwFileAttr; /* 0x18 attributes of the target file */
73 FILETIME CreationTime; /* 0x1c creation time of target file */
74 FILETIME AccessTime; /* 0x24 access time of target file */
75 FILETIME WriteTime; /* 0x2c write time of target file */
76 DWORD dwFileSize; /* 0x34 File size of target file */
77 DWORD nIcon; /* 0x38 icon number or index */
78 DWORD fStartup; /* 0x3c startup type or window state of application */
79 WORD wHotKey; /* 0x40 hotkey */
80 WORD Reserved1; /* 0x42 reserved = 0 */
81 DWORD Reserved2; /* 0x44 reserved = 0 */
82 DWORD Reserved3; /* 0x48 reserved = 0 */
83 } LINK_HEADER, * PLINK_HEADER;
85 #define SHLINK_LOCAL 0
86 #define SHLINK_REMOTE 1
88 typedef struct _LOCATION_INFO
90 DWORD dwTotalSize;
91 DWORD dwHeaderSize;
92 DWORD dwFlags;
93 DWORD dwVolTableOfs;
94 DWORD dwLocalPathOfs;
95 DWORD dwNetworkVolTableOfs;
96 DWORD dwFinalPathOfs;
97 } LOCATION_INFO;
99 typedef struct _LOCAL_VOLUME_INFO
101 DWORD dwSize;
102 DWORD dwType;
103 DWORD dwVolSerial;
104 DWORD dwVolLabelOfs;
105 } LOCAL_VOLUME_INFO;
107 typedef struct volume_info_t
109 DWORD type;
110 DWORD serial;
111 WCHAR label[12]; /* assume 8.3 */
112 } volume_info;
114 #include "poppack.h"
116 /* IShellLink Implementation */
118 typedef struct
120 IShellLinkA IShellLinkA_iface;
121 IShellLinkW IShellLinkW_iface;
122 IPersistFile IPersistFile_iface;
123 IPersistStream IPersistStream_iface;
124 IShellLinkDataList IShellLinkDataList_iface;
125 IShellExtInit IShellExtInit_iface;
126 IContextMenu IContextMenu_iface;
127 IObjectWithSite IObjectWithSite_iface;
128 IPropertyStore IPropertyStore_iface;
130 LONG ref;
132 /* data structures according to the information in the link */
133 LPITEMIDLIST pPidl;
134 WORD wHotKey;
135 SYSTEMTIME CreationTime;
136 SYSTEMTIME AccessTime;
137 SYSTEMTIME WriteTime;
139 DWORD iShowCmd;
140 LPWSTR sIcoPath;
141 INT iIcoNdx;
142 LPWSTR sPath;
143 LPWSTR sArgs;
144 LPWSTR sWorkDir;
145 LPWSTR sDescription;
146 LPWSTR sPathRel;
147 LPWSTR sProduct;
148 LPWSTR sComponent;
149 volume_info volume;
151 BOOL bDirty;
152 INT iIdOpen; /* id of the "Open" entry in the context menu */
153 IUnknown *site;
155 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
156 } IShellLinkImpl;
158 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
160 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
163 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
165 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
168 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
170 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
173 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
175 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
178 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
180 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
183 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
185 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
188 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
190 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
193 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
195 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
198 static inline IShellLinkImpl *impl_from_IPropertyStore(IPropertyStore *iface)
200 return CONTAINING_RECORD(iface, IShellLinkImpl, IPropertyStore_iface);
203 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
205 /**************************************************************************
206 * IPersistFile_QueryInterface
208 static HRESULT WINAPI IPersistFile_fnQueryInterface(
209 IPersistFile* iface,
210 REFIID riid,
211 LPVOID *ppvObj)
213 IShellLinkImpl *This = impl_from_IPersistFile(iface);
214 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
217 /******************************************************************************
218 * IPersistFile_AddRef
220 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
222 IShellLinkImpl *This = impl_from_IPersistFile(iface);
223 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
226 /******************************************************************************
227 * IPersistFile_Release
229 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
231 IShellLinkImpl *This = impl_from_IPersistFile(iface);
232 return IShellLinkW_Release(&This->IShellLinkW_iface);
235 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
237 IShellLinkImpl *This = impl_from_IPersistFile(iface);
239 TRACE("(%p)->(%p)\n", This, pClassID);
241 *pClassID = CLSID_ShellLink;
243 return S_OK;
246 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
248 IShellLinkImpl *This = impl_from_IPersistFile(iface);
250 TRACE("(%p)\n",This);
252 if (This->bDirty)
253 return S_OK;
255 return S_FALSE;
258 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
260 IShellLinkImpl *This = impl_from_IPersistFile(iface);
261 IPersistStream *StreamThis = &This->IPersistStream_iface;
262 HRESULT r;
263 IStream *stm;
265 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
267 if( dwMode == 0 ) dwMode = STGM_READ;
268 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
269 if( SUCCEEDED( r ) )
271 r = IPersistStream_Load(StreamThis, stm);
272 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
273 IStream_Release( stm );
275 /* update file path */
276 free(This->filepath);
277 This->filepath = wcsdup(pszFileName);
279 This->bDirty = FALSE;
281 TRACE("-- returning hr %08lx\n", r);
282 return r;
285 BOOL run_winemenubuilder( const WCHAR *args )
287 LONG len;
288 LPWSTR buffer;
289 STARTUPINFOW si;
290 PROCESS_INFORMATION pi;
291 BOOL ret;
292 WCHAR app[MAX_PATH];
293 void *redir;
295 GetSystemDirectoryW( app, MAX_PATH );
296 lstrcatW( app, L"\\winemenubuilder.exe" );
298 len = (lstrlenW( app ) + lstrlenW( args ) + 1) * sizeof(WCHAR);
299 buffer = malloc( len );
300 if( !buffer )
301 return FALSE;
303 lstrcpyW( buffer, app );
304 lstrcatW( buffer, args );
306 TRACE("starting %s\n",debugstr_w(buffer));
308 memset(&si, 0, sizeof(si));
309 si.cb = sizeof(si);
311 Wow64DisableWow64FsRedirection( &redir );
312 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
313 Wow64RevertWow64FsRedirection( redir );
315 free( buffer );
317 if (ret)
319 CloseHandle( pi.hProcess );
320 CloseHandle( pi.hThread );
323 return ret;
326 static BOOL StartLinkProcessor( LPCOLESTR szLink )
328 LONG len;
329 LPWSTR buffer;
330 BOOL ret;
332 len = (lstrlenW( szLink ) + 7) * sizeof(WCHAR);
333 buffer = malloc( len );
334 if( !buffer )
335 return FALSE;
337 swprintf( buffer, len, L" -w \"%s\"", szLink );
338 ret = run_winemenubuilder( buffer );
339 free( buffer );
340 return ret;
343 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
345 IShellLinkImpl *This = impl_from_IPersistFile(iface);
346 IPersistStream *StreamThis = &This->IPersistStream_iface;
347 HRESULT r;
348 IStream *stm;
350 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
352 if (!pszFileName)
354 if (!This->filepath) return S_OK;
356 pszFileName = This->filepath;
357 fRemember = FALSE;
360 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_DENY_WRITE, &stm );
361 if( SUCCEEDED( r ) )
363 r = IPersistStream_Save(StreamThis, stm, FALSE);
364 IStream_Release( stm );
366 if( SUCCEEDED( r ) )
368 StartLinkProcessor( pszFileName );
370 if (fRemember)
372 /* update file path */
373 free(This->filepath);
374 This->filepath = wcsdup(pszFileName);
377 This->bDirty = FALSE;
379 else
381 DeleteFileW( pszFileName );
382 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
386 return r;
389 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
391 IShellLinkImpl *This = impl_from_IPersistFile(iface);
392 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
393 return S_OK;
396 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
398 IShellLinkImpl *This = impl_from_IPersistFile(iface);
400 TRACE("(%p)->(%p)\n", This, filename);
402 if (!This->filepath)
404 *filename = NULL;
405 return S_FALSE;
408 *filename = CoTaskMemAlloc((lstrlenW(This->filepath) + 1) * sizeof(WCHAR));
409 if (!*filename) return E_OUTOFMEMORY;
411 lstrcpyW(*filename, This->filepath);
413 return S_OK;
416 static const IPersistFileVtbl pfvt =
418 IPersistFile_fnQueryInterface,
419 IPersistFile_fnAddRef,
420 IPersistFile_fnRelease,
421 IPersistFile_fnGetClassID,
422 IPersistFile_fnIsDirty,
423 IPersistFile_fnLoad,
424 IPersistFile_fnSave,
425 IPersistFile_fnSaveCompleted,
426 IPersistFile_fnGetCurFile
429 /************************************************************************
430 * IPersistStream_QueryInterface
432 static HRESULT WINAPI IPersistStream_fnQueryInterface(
433 IPersistStream* iface,
434 REFIID riid,
435 VOID** ppvObj)
437 IShellLinkImpl *This = impl_from_IPersistStream(iface);
438 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
441 /************************************************************************
442 * IPersistStream_Release
444 static ULONG WINAPI IPersistStream_fnRelease(
445 IPersistStream* iface)
447 IShellLinkImpl *This = impl_from_IPersistStream(iface);
448 return IShellLinkW_Release(&This->IShellLinkW_iface);
451 /************************************************************************
452 * IPersistStream_AddRef
454 static ULONG WINAPI IPersistStream_fnAddRef(
455 IPersistStream* iface)
457 IShellLinkImpl *This = impl_from_IPersistStream(iface);
458 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
461 /************************************************************************
462 * IPersistStream_GetClassID
465 static HRESULT WINAPI IPersistStream_fnGetClassID(
466 IPersistStream* iface,
467 CLSID* pClassID)
469 IShellLinkImpl *This = impl_from_IPersistStream(iface);
470 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
473 /************************************************************************
474 * IPersistStream_IsDirty (IPersistStream)
476 static HRESULT WINAPI IPersistStream_fnIsDirty(
477 IPersistStream* iface)
479 IShellLinkImpl *This = impl_from_IPersistStream(iface);
481 TRACE("(%p)\n", This);
483 return S_OK;
487 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
489 DWORD count;
490 USHORT len;
491 LPVOID temp;
492 LPWSTR str;
493 HRESULT r;
495 TRACE("%p\n", stm);
497 count = 0;
498 r = IStream_Read(stm, &len, sizeof(len), &count);
499 if ( FAILED (r) || ( count != sizeof(len) ) )
500 return E_FAIL;
502 if( unicode )
503 len *= sizeof (WCHAR);
505 TRACE("reading %d\n", len);
506 temp = malloc(len + sizeof(WCHAR));
507 if( !temp )
508 return E_OUTOFMEMORY;
509 count = 0;
510 r = IStream_Read(stm, temp, len, &count);
511 if( FAILED (r) || ( count != len ) )
513 free( temp );
514 return E_FAIL;
517 TRACE("read %s\n", debugstr_an(temp,len));
519 /* convert to unicode if necessary */
520 if( !unicode )
522 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
523 str = malloc( (count + 1) * sizeof(WCHAR) );
524 if( !str )
526 free( temp );
527 return E_OUTOFMEMORY;
529 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
530 free( temp );
532 else
534 count /= 2;
535 str = temp;
537 str[count] = 0;
539 *pstr = str;
541 return S_OK;
544 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
546 DWORD size;
547 ULONG count;
548 HRESULT r;
549 struct sized_chunk {
550 DWORD size;
551 unsigned char data[1];
552 } *chunk;
554 TRACE("%p\n",stm);
556 r = IStream_Read( stm, &size, sizeof(size), &count );
557 if( FAILED( r ) || count != sizeof(size) )
558 return E_FAIL;
560 chunk = malloc( size );
561 if( !chunk )
562 return E_OUTOFMEMORY;
564 chunk->size = size;
565 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
566 if( FAILED( r ) || count != (size - sizeof(size)) )
568 free( chunk );
569 return E_FAIL;
572 TRACE("Read %ld bytes\n",chunk->size);
574 *data = chunk;
576 return S_OK;
579 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
581 const int label_sz = ARRAY_SIZE(volume->label);
582 LPSTR label;
583 int len;
585 volume->serial = vol->dwVolSerial;
586 volume->type = vol->dwType;
588 if( !vol->dwVolLabelOfs )
589 return FALSE;
590 if( vol->dwSize <= vol->dwVolLabelOfs )
591 return FALSE;
592 len = vol->dwSize - vol->dwVolLabelOfs;
594 label = (LPSTR) vol;
595 label += vol->dwVolLabelOfs;
596 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
598 return TRUE;
601 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
603 int len = 0, wlen;
604 LPWSTR path;
606 while( (len < maxlen) && p[len] )
607 len++;
609 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
610 path = malloc((wlen + 1) * sizeof(WCHAR));
611 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
612 path[wlen] = 0;
614 return path;
617 static HRESULT Stream_LoadLocation( IStream *stm,
618 volume_info *volume, LPWSTR *path )
620 char *p = NULL;
621 LOCATION_INFO *loc;
622 HRESULT r;
623 DWORD n;
625 r = Stream_ReadChunk( stm, (LPVOID*) &p );
626 if( FAILED(r) )
627 return r;
629 loc = (LOCATION_INFO*) p;
630 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
632 free( p );
633 return E_FAIL;
636 /* if there's valid local volume information, load it */
637 if( loc->dwVolTableOfs &&
638 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
640 LOCAL_VOLUME_INFO *volume_info;
642 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
643 Stream_LoadVolume( volume_info, volume );
646 /* if there's a local path, load it */
647 n = loc->dwLocalPathOfs;
648 if( n && (n < loc->dwTotalSize) )
649 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
651 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
652 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
654 free( p );
655 return S_OK;
659 * The format of the advertised shortcut info seems to be:
661 * Offset Description
662 * ------ -----------
664 * 0 Length of the block (4 bytes, usually 0x314)
665 * 4 tag (dword)
666 * 8 string data in ANSI
667 * 8+0x104 string data in UNICODE
669 * In the original Win32 implementation the buffers are not initialized
670 * to zero, so data trailing the string is random garbage.
672 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
674 DWORD size;
675 ULONG count;
676 HRESULT r;
677 EXP_DARWIN_LINK buffer;
679 TRACE("%p\n",stm);
681 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
682 if( FAILED( r ) )
683 return r;
685 /* make sure that we read the size of the structure even on error */
686 size = sizeof buffer - sizeof (DWORD);
687 if( buffer.dbh.cbSize != sizeof buffer )
689 ERR("Ooops. This structure is not as expected...\n");
690 return E_FAIL;
693 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
694 if( FAILED( r ) )
695 return r;
697 if( count != size )
698 return E_FAIL;
700 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
702 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
704 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
705 return E_FAIL;
708 *str = malloc( (lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
709 lstrcpyW( *str, buffer.szwDarwinID );
711 return S_OK;
714 /************************************************************************
715 * IPersistStream_Load (IPersistStream)
717 static HRESULT WINAPI IPersistStream_fnLoad(
718 IPersistStream* iface,
719 IStream* stm)
721 LINK_HEADER hdr;
722 ULONG dwBytesRead;
723 BOOL unicode;
724 HRESULT r;
725 DWORD zero;
727 IShellLinkImpl *This = impl_from_IPersistStream(iface);
729 TRACE("%p %p\n", This, stm);
731 if( !stm )
732 return STG_E_INVALIDPOINTER;
734 dwBytesRead = 0;
735 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
736 if( FAILED( r ) )
737 return r;
739 if( dwBytesRead != sizeof(hdr))
740 return E_FAIL;
741 if( hdr.dwSize != sizeof(hdr))
742 return E_FAIL;
743 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
744 return E_FAIL;
746 /* free all the old stuff */
747 ILFree(This->pPidl);
748 This->pPidl = NULL;
749 memset( &This->volume, 0, sizeof This->volume );
750 free(This->sPath);
751 This->sPath = NULL;
752 free(This->sDescription);
753 This->sDescription = NULL;
754 free(This->sPathRel);
755 This->sPathRel = NULL;
756 free(This->sWorkDir);
757 This->sWorkDir = NULL;
758 free(This->sArgs);
759 This->sArgs = NULL;
760 free(This->sIcoPath);
761 This->sIcoPath = NULL;
762 free(This->sProduct);
763 This->sProduct = NULL;
764 free(This->sComponent);
765 This->sComponent = NULL;
767 This->wHotKey = hdr.wHotKey;
768 This->iIcoNdx = hdr.nIcon;
769 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
770 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
771 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
772 if (TRACE_ON(shell))
774 WCHAR sTemp[MAX_PATH];
775 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
776 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
777 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
778 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
779 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
780 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
783 /* load all the new stuff */
784 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
786 r = ILLoadFromStream( stm, &This->pPidl );
787 if( FAILED( r ) )
788 return r;
790 pdump(This->pPidl);
792 /* load the location information */
793 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
794 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
795 if( FAILED( r ) )
796 goto end;
798 unicode = hdr.dwFlags & SLDF_UNICODE;
799 if( hdr.dwFlags & SLDF_HAS_NAME )
801 r = Stream_LoadString( stm, unicode, &This->sDescription );
802 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
804 if( FAILED( r ) )
805 goto end;
807 if( hdr.dwFlags & SLDF_HAS_RELPATH )
809 r = Stream_LoadString( stm, unicode, &This->sPathRel );
810 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
812 if( FAILED( r ) )
813 goto end;
815 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
817 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
818 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
820 if( FAILED( r ) )
821 goto end;
823 if( hdr.dwFlags & SLDF_HAS_ARGS )
825 r = Stream_LoadString( stm, unicode, &This->sArgs );
826 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
828 if( FAILED( r ) )
829 goto end;
831 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
833 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
834 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
836 if( FAILED( r ) )
837 goto end;
839 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
841 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
842 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
844 if( FAILED( r ) )
845 goto end;
847 if( hdr.dwFlags & SLDF_HAS_DARWINID )
849 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
850 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
852 if( FAILED( r ) )
853 goto end;
855 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
856 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
858 /* Some lnk files have extra data blocks starting with a
859 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
860 * one with a 0xa0000003 signature. However these don't seem to matter
861 * too much.
863 WARN("Last word was not zero\n");
866 TRACE("OK\n");
868 pdump (This->pPidl);
870 return S_OK;
871 end:
872 return r;
875 /************************************************************************
876 * Stream_WriteString
878 * Helper function for IPersistStream_Save. Writes a unicode string
879 * with terminating nul byte to a stream, preceded by the its length.
881 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
883 USHORT len = lstrlenW( str ) + 1;
884 DWORD count;
885 HRESULT r;
887 r = IStream_Write( stm, &len, sizeof(len), &count );
888 if( FAILED( r ) )
889 return r;
891 len *= sizeof(WCHAR);
893 r = IStream_Write( stm, str, len, &count );
894 if( FAILED( r ) )
895 return r;
897 return S_OK;
900 /************************************************************************
901 * Stream_WriteLocationInfo
903 * Writes the location info to a stream
905 * FIXME: One day we might want to write the network volume information
906 * and the final path.
907 * Figure out how Windows deals with unicode paths here.
909 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
910 volume_info *volume )
912 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
913 LOCAL_VOLUME_INFO *vol;
914 LOCATION_INFO *loc;
915 LPSTR szLabel, szPath, szFinalPath;
916 ULONG count = 0;
917 HRESULT hr;
919 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
921 /* figure out the size of everything */
922 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
923 NULL, 0, NULL, NULL );
924 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
925 NULL, 0, NULL, NULL );
926 volume_info_size = sizeof *vol + label_size;
927 final_path_size = 1;
928 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
930 /* create pointers to everything */
931 loc = calloc(1, total_size);
932 vol = (LOCAL_VOLUME_INFO*) &loc[1];
933 szLabel = (LPSTR) &vol[1];
934 szPath = &szLabel[label_size];
935 szFinalPath = &szPath[path_size];
937 /* fill in the location information header */
938 loc->dwTotalSize = total_size;
939 loc->dwHeaderSize = sizeof (*loc);
940 loc->dwFlags = 1;
941 loc->dwVolTableOfs = sizeof (*loc);
942 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
943 loc->dwNetworkVolTableOfs = 0;
944 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
946 /* fill in the volume information */
947 vol->dwSize = volume_info_size;
948 vol->dwType = volume->type;
949 vol->dwVolSerial = volume->serial;
950 vol->dwVolLabelOfs = sizeof (*vol);
952 /* copy in the strings */
953 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
954 szLabel, label_size, NULL, NULL );
955 WideCharToMultiByte( CP_ACP, 0, path, -1,
956 szPath, path_size, NULL, NULL );
957 szFinalPath[0] = 0;
959 hr = IStream_Write( stm, loc, total_size, &count );
960 free(loc);
962 return hr;
965 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
967 EXP_DARWIN_LINK *buffer;
969 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
970 buffer->dbh.cbSize = sizeof *buffer;
971 buffer->dbh.dwSignature = magic;
972 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
973 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
975 return buffer;
978 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
980 EXP_DARWIN_LINK *buffer;
981 ULONG count;
983 TRACE("%p\n",stm);
985 buffer = shelllink_build_darwinid( string, magic );
987 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
990 /************************************************************************
991 * IPersistStream_Save (IPersistStream)
993 * FIXME: makes assumptions about byte order
995 static HRESULT WINAPI IPersistStream_fnSave(
996 IPersistStream* iface,
997 IStream* stm,
998 BOOL fClearDirty)
1000 LINK_HEADER header;
1001 ULONG count;
1002 DWORD zero;
1003 HRESULT r;
1005 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1007 TRACE("%p %p %x\n", This, stm, fClearDirty);
1009 memset(&header, 0, sizeof(header));
1010 header.dwSize = sizeof(header);
1011 header.fStartup = This->iShowCmd;
1012 header.MagicGuid = CLSID_ShellLink;
1014 header.wHotKey = This->wHotKey;
1015 header.nIcon = This->iIcoNdx;
1016 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1017 if( This->pPidl )
1018 header.dwFlags |= SLDF_HAS_ID_LIST;
1019 if( This->sPath )
1020 header.dwFlags |= SLDF_HAS_LINK_INFO;
1021 if( This->sDescription )
1022 header.dwFlags |= SLDF_HAS_NAME;
1023 if( This->sWorkDir )
1024 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1025 if( This->sArgs )
1026 header.dwFlags |= SLDF_HAS_ARGS;
1027 if( This->sIcoPath )
1028 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1029 if( This->sProduct )
1030 header.dwFlags |= SLDF_HAS_LOGO3ID;
1031 if( This->sComponent )
1032 header.dwFlags |= SLDF_HAS_DARWINID;
1034 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1035 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1036 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1038 /* write the Shortcut header */
1039 r = IStream_Write( stm, &header, sizeof(header), &count );
1040 if( FAILED( r ) )
1042 ERR("Write failed at %d\n",__LINE__);
1043 return r;
1046 TRACE("Writing pidl\n");
1048 /* write the PIDL to the shortcut */
1049 if( This->pPidl )
1051 r = ILSaveToStream( stm, This->pPidl );
1052 if( FAILED( r ) )
1054 ERR("Failed to write PIDL at %d\n",__LINE__);
1055 return r;
1059 if( This->sPath )
1060 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1062 if( This->sDescription )
1063 r = Stream_WriteString( stm, This->sDescription );
1065 if( This->sPathRel )
1066 r = Stream_WriteString( stm, This->sPathRel );
1068 if( This->sWorkDir )
1069 r = Stream_WriteString( stm, This->sWorkDir );
1071 if( This->sArgs )
1072 r = Stream_WriteString( stm, This->sArgs );
1074 if( This->sIcoPath )
1075 r = Stream_WriteString( stm, This->sIcoPath );
1077 if( This->sProduct )
1078 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1080 if( This->sComponent )
1081 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1083 /* the last field is a single zero dword */
1084 zero = 0;
1085 r = IStream_Write( stm, &zero, sizeof zero, &count );
1087 return S_OK;
1090 /************************************************************************
1091 * IPersistStream_GetSizeMax (IPersistStream)
1093 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1094 IPersistStream* iface,
1095 ULARGE_INTEGER* pcbSize)
1097 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1099 TRACE("(%p)\n", This);
1101 return E_NOTIMPL;
1104 static const IPersistStreamVtbl psvt =
1106 IPersistStream_fnQueryInterface,
1107 IPersistStream_fnAddRef,
1108 IPersistStream_fnRelease,
1109 IPersistStream_fnGetClassID,
1110 IPersistStream_fnIsDirty,
1111 IPersistStream_fnLoad,
1112 IPersistStream_fnSave,
1113 IPersistStream_fnGetSizeMax
1116 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1118 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1119 return FALSE;
1120 return TRUE;
1123 /**************************************************************************
1124 * ShellLink_UpdatePath
1125 * update absolute path in sPath using relative path in sPathRel
1127 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1129 if (!path || !psPath)
1130 return E_INVALIDARG;
1132 if (!*psPath && sPathRel) {
1133 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1134 LPWSTR final = NULL;
1136 /* first try if [directory of link file] + [relative path] finds an existing file */
1138 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1139 if( !final )
1140 final = buffer;
1141 lstrcpyW(final, sPathRel);
1143 *abs_path = '\0';
1145 if (SHELL_ExistsFileW(buffer)) {
1146 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1147 lstrcpyW(abs_path, buffer);
1148 } else {
1149 /* try if [working directory] + [relative path] finds an existing file */
1150 if (sWorkDir) {
1151 lstrcpyW(buffer, sWorkDir);
1152 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1154 if (SHELL_ExistsFileW(buffer))
1155 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1156 lstrcpyW(abs_path, buffer);
1160 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1161 if (!*abs_path)
1162 lstrcpyW(abs_path, sPathRel);
1164 *psPath = malloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1165 if (!*psPath)
1166 return E_OUTOFMEMORY;
1168 lstrcpyW(*psPath, abs_path);
1171 return S_OK;
1174 /**************************************************************************
1175 * IShellLink_ConstructFromFile
1177 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1178 LPCITEMIDLIST pidl, IUnknown **ppv)
1180 IShellLinkW* psl;
1182 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1184 if (SUCCEEDED(hr)) {
1185 IPersistFile* ppf;
1187 *ppv = NULL;
1189 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1191 if (SUCCEEDED(hr)) {
1192 WCHAR path[MAX_PATH];
1194 if (SHGetPathFromIDListW(pidl, path))
1195 hr = IPersistFile_Load(ppf, path, 0);
1196 else
1197 hr = E_FAIL;
1199 if (SUCCEEDED(hr))
1200 *ppv = (IUnknown*)psl;
1202 IPersistFile_Release(ppf);
1205 if (!*ppv)
1206 IShellLinkW_Release(psl);
1209 return hr;
1212 /**************************************************************************
1213 * IShellLinkA_QueryInterface
1215 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1217 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1218 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1221 /******************************************************************************
1222 * IShellLinkA_AddRef
1224 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1226 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1227 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1230 /******************************************************************************
1231 * IShellLinkA_Release
1233 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1235 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1236 return IShellLinkW_Release(&This->IShellLinkW_iface);
1239 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1240 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1242 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1243 HRESULT res = S_OK;
1245 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1246 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1248 if (This->sComponent || This->sProduct)
1249 return S_FALSE;
1251 if (cchMaxPath)
1252 pszFile[0] = 0;
1253 if (This->sPath && This->sPath[0])
1254 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1255 pszFile, cchMaxPath, NULL, NULL);
1256 else
1257 res = S_FALSE;
1259 if (pfd)
1261 memset(pfd, 0, sizeof(*pfd));
1263 if (res == S_OK)
1265 char path[MAX_PATH];
1266 WIN32_FILE_ATTRIBUTE_DATA fad;
1268 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1270 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1272 pfd->dwFileAttributes = fad.dwFileAttributes;
1273 pfd->ftCreationTime = fad.ftCreationTime;
1274 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1275 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1276 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1277 pfd->nFileSizeLow = fad.nFileSizeLow;
1280 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1282 if (GetShortPathNameA(path, path, MAX_PATH))
1284 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1288 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1289 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1290 wine_dbgstr_a(pfd->cAlternateFileName));
1293 return res;
1296 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1298 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1299 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1302 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1304 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1305 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1308 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1309 INT cchMaxName)
1311 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1313 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1315 if( cchMaxName )
1316 pszName[0] = 0;
1317 if( This->sDescription )
1318 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1319 pszName, cchMaxName, NULL, NULL);
1321 return S_OK;
1324 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1326 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1327 WCHAR *descrW;
1328 HRESULT hr;
1330 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1332 if (pszName)
1334 descrW = strdupAtoW(pszName);
1335 if (!descrW) return E_OUTOFMEMORY;
1337 else
1338 descrW = NULL;
1340 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1341 free(descrW);
1343 return hr;
1346 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1347 INT cchMaxPath)
1349 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1351 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1353 if( cchMaxPath )
1354 pszDir[0] = 0;
1355 if( This->sWorkDir )
1356 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1357 pszDir, cchMaxPath, NULL, NULL);
1359 return S_OK;
1362 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1364 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1365 WCHAR *dirW;
1366 HRESULT hr;
1368 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1370 dirW = strdupAtoW(pszDir);
1371 if (!dirW) return E_OUTOFMEMORY;
1373 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1374 free(dirW);
1376 return hr;
1379 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1381 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1383 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1385 if( cchMaxPath )
1386 pszArgs[0] = 0;
1387 if( This->sArgs )
1388 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1389 pszArgs, cchMaxPath, NULL, NULL);
1391 return S_OK;
1394 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1396 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1397 WCHAR *argsW;
1398 HRESULT hr;
1400 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1402 if (pszArgs)
1404 argsW = strdupAtoW(pszArgs);
1405 if (!argsW) return E_OUTOFMEMORY;
1407 else
1408 argsW = NULL;
1410 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1411 free(argsW);
1413 return hr;
1416 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1418 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1419 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1422 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1424 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1425 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1428 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1430 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1431 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1434 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1436 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1437 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1440 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1441 INT cchIconPath, INT *piIcon)
1443 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1445 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1447 *piIcon = This->iIcoNdx;
1449 if (This->sIcoPath)
1450 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1451 else
1452 pszIconPath[0] = 0;
1454 return S_OK;
1457 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1459 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1460 WCHAR *pathW = NULL;
1461 HRESULT hr;
1463 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1465 if (path)
1467 pathW = strdupAtoW(path);
1468 if (!pathW)
1469 return E_OUTOFMEMORY;
1472 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1473 free(pathW);
1475 return hr;
1478 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1479 DWORD dwReserved)
1481 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1482 WCHAR *pathW;
1483 HRESULT hr;
1485 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1487 pathW = strdupAtoW(pszPathRel);
1488 if (!pathW) return E_OUTOFMEMORY;
1490 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1491 free(pathW);
1493 return hr;
1496 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1498 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1500 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1502 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1505 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1507 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1508 HRESULT r;
1509 LPWSTR str;
1511 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1513 if (!pszFile) return E_INVALIDARG;
1515 str = strdupAtoW(pszFile);
1516 if (!str) return E_OUTOFMEMORY;
1518 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1519 free(str);
1521 return r;
1524 /**************************************************************************
1525 * IShellLink Implementation
1528 static const IShellLinkAVtbl slvt =
1530 IShellLinkA_fnQueryInterface,
1531 IShellLinkA_fnAddRef,
1532 IShellLinkA_fnRelease,
1533 IShellLinkA_fnGetPath,
1534 IShellLinkA_fnGetIDList,
1535 IShellLinkA_fnSetIDList,
1536 IShellLinkA_fnGetDescription,
1537 IShellLinkA_fnSetDescription,
1538 IShellLinkA_fnGetWorkingDirectory,
1539 IShellLinkA_fnSetWorkingDirectory,
1540 IShellLinkA_fnGetArguments,
1541 IShellLinkA_fnSetArguments,
1542 IShellLinkA_fnGetHotkey,
1543 IShellLinkA_fnSetHotkey,
1544 IShellLinkA_fnGetShowCmd,
1545 IShellLinkA_fnSetShowCmd,
1546 IShellLinkA_fnGetIconLocation,
1547 IShellLinkA_fnSetIconLocation,
1548 IShellLinkA_fnSetRelativePath,
1549 IShellLinkA_fnResolve,
1550 IShellLinkA_fnSetPath
1554 /**************************************************************************
1555 * IShellLinkW_fnQueryInterface
1557 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1558 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1560 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1562 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1564 *ppvObj = NULL;
1566 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1568 *ppvObj = &This->IShellLinkA_iface;
1570 else if(IsEqualIID(riid, &IID_IShellLinkW))
1572 *ppvObj = &This->IShellLinkW_iface;
1574 else if(IsEqualIID(riid, &IID_IPersistFile))
1576 *ppvObj = &This->IPersistFile_iface;
1578 else if(IsEqualIID(riid, &IID_IPersistStream))
1580 *ppvObj = &This->IPersistStream_iface;
1582 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1584 *ppvObj = &This->IShellLinkDataList_iface;
1586 else if(IsEqualIID(riid, &IID_IShellExtInit))
1588 *ppvObj = &This->IShellExtInit_iface;
1590 else if(IsEqualIID(riid, &IID_IContextMenu))
1592 *ppvObj = &This->IContextMenu_iface;
1594 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1596 *ppvObj = &This->IObjectWithSite_iface;
1598 else if(IsEqualIID(riid, &IID_IPropertyStore))
1600 *ppvObj = &This->IPropertyStore_iface;
1603 if(*ppvObj)
1605 IUnknown_AddRef((IUnknown*)*ppvObj);
1606 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1607 return S_OK;
1609 ERR("-- Interface: E_NOINTERFACE\n");
1610 return E_NOINTERFACE;
1613 /******************************************************************************
1614 * IShellLinkW_fnAddRef
1616 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1618 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1619 ULONG ref = InterlockedIncrement(&This->ref);
1621 TRACE("(%p)->(count=%lu)\n", This, ref - 1);
1623 return ref;
1626 /******************************************************************************
1627 * IShellLinkW_fnRelease
1629 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1631 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1632 ULONG refCount = InterlockedDecrement(&This->ref);
1634 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1636 if (refCount)
1637 return refCount;
1639 TRACE("-- destroying IShellLink(%p)\n",This);
1641 free(This->sIcoPath);
1642 free(This->sArgs);
1643 free(This->sWorkDir);
1644 free(This->sDescription);
1645 free(This->sPath);
1646 free(This->sPathRel);
1647 free(This->sProduct);
1648 free(This->sComponent);
1649 free(This->filepath);
1651 if (This->site)
1652 IUnknown_Release( This->site );
1654 if (This->pPidl)
1655 ILFree(This->pPidl);
1657 LocalFree(This);
1659 return 0;
1662 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1664 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1665 HRESULT res = S_OK;
1667 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1668 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1670 if (This->sComponent || This->sProduct)
1671 return S_FALSE;
1673 if (cchMaxPath)
1674 pszFile[0] = 0;
1675 if (This->sPath)
1676 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1677 else
1678 res = S_FALSE;
1680 if (pfd)
1682 memset(pfd, 0, sizeof(*pfd));
1684 if (res == S_OK)
1686 WCHAR path[MAX_PATH];
1687 WIN32_FILE_ATTRIBUTE_DATA fad;
1689 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1691 pfd->dwFileAttributes = fad.dwFileAttributes;
1692 pfd->ftCreationTime = fad.ftCreationTime;
1693 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1694 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1695 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1696 pfd->nFileSizeLow = fad.nFileSizeLow;
1699 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1701 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1703 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1707 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1708 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1709 wine_dbgstr_w(pfd->cAlternateFileName));
1712 return res;
1715 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1717 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1719 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1721 if (!This->pPidl)
1723 *ppidl = NULL;
1724 return S_FALSE;
1726 *ppidl = ILClone(This->pPidl);
1727 return S_OK;
1730 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1732 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1733 WCHAR path[MAX_PATH];
1735 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1737 if( This->pPidl )
1738 ILFree( This->pPidl );
1739 This->pPidl = ILClone( pidl );
1740 if( !This->pPidl )
1741 return E_FAIL;
1743 free( This->sPath );
1744 This->sPath = NULL;
1746 if ( SHGetPathFromIDListW( pidl, path ) )
1748 This->sPath = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1749 if (!This->sPath)
1750 return E_OUTOFMEMORY;
1752 lstrcpyW(This->sPath, path);
1755 This->bDirty = TRUE;
1757 return S_OK;
1760 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1762 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1764 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1766 pszName[0] = 0;
1767 if( This->sDescription )
1768 lstrcpynW( pszName, This->sDescription, cchMaxName );
1770 return S_OK;
1773 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1775 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1777 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1779 free(This->sDescription);
1780 if (pszName)
1782 This->sDescription = malloc( (lstrlenW( pszName ) + 1) * sizeof(WCHAR) );
1783 if ( !This->sDescription )
1784 return E_OUTOFMEMORY;
1786 lstrcpyW( This->sDescription, pszName );
1788 else
1789 This->sDescription = NULL;
1790 This->bDirty = TRUE;
1792 return S_OK;
1795 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1797 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1799 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1801 if( cchMaxPath )
1802 pszDir[0] = 0;
1803 if( This->sWorkDir )
1804 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1806 return S_OK;
1809 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1811 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1813 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1815 free(This->sWorkDir);
1816 This->sWorkDir = malloc( (lstrlenW( pszDir ) + 1) * sizeof(WCHAR) );
1817 if ( !This->sWorkDir )
1818 return E_OUTOFMEMORY;
1819 lstrcpyW( This->sWorkDir, pszDir );
1820 This->bDirty = TRUE;
1822 return S_OK;
1825 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1827 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1829 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1831 if( cchMaxPath )
1832 pszArgs[0] = 0;
1833 if( This->sArgs )
1834 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1836 return S_OK;
1839 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1841 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1843 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1845 free(This->sArgs);
1846 if (pszArgs)
1848 This->sArgs = malloc( (lstrlenW( pszArgs ) + 1) * sizeof(WCHAR) );
1849 if ( !This->sArgs )
1850 return E_OUTOFMEMORY;
1851 lstrcpyW( This->sArgs, pszArgs );
1853 else This->sArgs = NULL;
1855 This->bDirty = TRUE;
1857 return S_OK;
1860 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1862 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1864 TRACE("(%p)->(%p)\n",This, pwHotkey);
1866 *pwHotkey=This->wHotKey;
1868 return S_OK;
1871 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1873 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1875 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1877 This->wHotKey = wHotkey;
1878 This->bDirty = TRUE;
1880 return S_OK;
1883 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1885 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1887 TRACE("(%p)->(%p)\n",This, piShowCmd);
1889 *piShowCmd = This->iShowCmd;
1891 return S_OK;
1894 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1896 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1898 TRACE("(%p)->(%d)\n", This, iShowCmd);
1900 This->iShowCmd = iShowCmd;
1901 This->bDirty = TRUE;
1903 return S_OK;
1906 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1908 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1910 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1912 *piIcon = This->iIcoNdx;
1914 if (This->sIcoPath)
1915 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1916 else
1917 pszIconPath[0] = 0;
1919 return S_OK;
1922 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1924 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1926 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1928 free(This->sIcoPath);
1929 if (path)
1931 size_t len = (lstrlenW(path) + 1) * sizeof(WCHAR);
1932 This->sIcoPath = malloc(len);
1933 if (!This->sIcoPath)
1934 return E_OUTOFMEMORY;
1935 memcpy(This->sIcoPath, path, len);
1937 else
1938 This->sIcoPath = NULL;
1939 This->iIcoNdx = icon;
1940 This->bDirty = TRUE;
1942 return S_OK;
1945 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1947 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1949 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1951 free(This->sPathRel);
1952 This->sPathRel = malloc( (lstrlenW( pszPathRel ) + 1) * sizeof(WCHAR) );
1953 if ( !This->sPathRel )
1954 return E_OUTOFMEMORY;
1955 lstrcpyW( This->sPathRel, pszPathRel );
1956 This->bDirty = TRUE;
1958 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1961 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1963 HRESULT hr = S_OK;
1964 BOOL bSuccess;
1966 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1968 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1970 /*FIXME: use IResolveShellLink interface */
1972 if (!This->sPath && This->pPidl) {
1973 WCHAR buffer[MAX_PATH];
1975 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1977 if (bSuccess && *buffer) {
1978 This->sPath = malloc((lstrlenW(buffer) + 1) * sizeof(WCHAR));
1979 if (!This->sPath)
1980 return E_OUTOFMEMORY;
1982 lstrcpyW(This->sPath, buffer);
1984 This->bDirty = TRUE;
1985 } else
1986 hr = S_OK; /* don't report an error occurred while just caching information */
1989 if (!This->sIcoPath && This->sPath) {
1990 This->sIcoPath = malloc((lstrlenW(This->sPath) + 1) * sizeof(WCHAR));
1991 if (!This->sIcoPath)
1992 return E_OUTOFMEMORY;
1994 lstrcpyW(This->sIcoPath, This->sPath);
1995 This->iIcoNdx = 0;
1997 This->bDirty = TRUE;
2000 return hr;
2003 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2005 LPWSTR ret;
2006 LPCWSTR p;
2007 DWORD len;
2009 if( !str )
2010 return NULL;
2012 p = wcschr( str, ':' );
2013 if( !p )
2014 return NULL;
2015 len = p - str;
2016 ret = malloc(sizeof(WCHAR) * (len + 1));
2017 if( !ret )
2018 return ret;
2019 memcpy( ret, str, sizeof(WCHAR)*len );
2020 ret[len] = 0;
2021 return ret;
2024 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2026 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2027 WCHAR szGuid[39];
2028 HRESULT r;
2029 GUID guid;
2030 int len;
2032 while( str[0] )
2034 /* each segment must start with two colons */
2035 if( str[0] != ':' || str[1] != ':' )
2036 return E_FAIL;
2038 /* the last segment is just two colons */
2039 if( !str[2] )
2040 break;
2041 str += 2;
2043 /* there must be a colon straight after a guid */
2044 p = wcschr( str, ':' );
2045 if( !p )
2046 return E_FAIL;
2047 len = p - str;
2048 if( len != 38 )
2049 return E_FAIL;
2051 /* get the guid, and check it's validly formatted */
2052 memcpy( szGuid, str, sizeof(WCHAR)*len );
2053 szGuid[len] = 0;
2054 r = CLSIDFromString( szGuid, &guid );
2055 if( r != S_OK )
2056 return r;
2057 str = p + 1;
2059 /* match it up to a guid that we care about */
2060 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2061 szComponent = str;
2062 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2063 szProduct = str;
2064 else
2065 return E_FAIL;
2067 /* skip to the next field */
2068 str = wcschr( str, ':' );
2069 if( !str )
2070 return E_FAIL;
2073 /* we have to have a component for an advertised shortcut */
2074 if( !szComponent )
2075 return E_FAIL;
2077 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2078 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2080 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2081 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2083 return S_OK;
2086 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2088 const int label_sz = ARRAY_SIZE(volume->label);
2089 WCHAR drive[] = { path[0], ':', '\\', 0 };
2090 BOOL r;
2092 volume->type = GetDriveTypeW(drive);
2093 r = GetVolumeInformationW(drive, volume->label, label_sz,
2094 &volume->serial, NULL, NULL, NULL, 0);
2095 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2096 volume->type, volume->serial, debugstr_w(volume->label));
2097 return r;
2100 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2102 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2103 WCHAR buffer[MAX_PATH];
2104 LPWSTR fname, unquoted = NULL;
2105 HRESULT hr = S_OK;
2106 UINT len;
2108 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2110 if (!pszFile) return E_INVALIDARG;
2112 /* quotes at the ends of the string are stripped */
2113 len = lstrlenW(pszFile);
2114 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2116 unquoted = wcsdup(pszFile);
2117 PathUnquoteSpacesW(unquoted);
2118 pszFile = unquoted;
2121 /* any other quote marks are invalid */
2122 if (wcschr(pszFile, '"'))
2124 free(unquoted);
2125 return S_FALSE;
2128 free(This->sPath);
2129 This->sPath = NULL;
2131 free(This->sComponent);
2132 This->sComponent = NULL;
2134 if (This->pPidl)
2135 ILFree(This->pPidl);
2136 This->pPidl = NULL;
2138 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2140 if (*pszFile == '\0')
2141 *buffer = '\0';
2142 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2144 free(unquoted);
2145 return E_FAIL;
2147 else if(!PathFileExistsW(buffer) &&
2148 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2149 hr = S_FALSE;
2151 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2152 ShellLink_GetVolumeInfo(buffer, &This->volume);
2154 This->sPath = malloc( (lstrlenW( buffer ) + 1) * sizeof(WCHAR) );
2155 if (!This->sPath)
2157 free(unquoted);
2158 return E_OUTOFMEMORY;
2161 lstrcpyW(This->sPath, buffer);
2163 This->bDirty = TRUE;
2164 free(unquoted);
2166 return hr;
2169 /**************************************************************************
2170 * IShellLinkW Implementation
2173 static const IShellLinkWVtbl slvtw =
2175 IShellLinkW_fnQueryInterface,
2176 IShellLinkW_fnAddRef,
2177 IShellLinkW_fnRelease,
2178 IShellLinkW_fnGetPath,
2179 IShellLinkW_fnGetIDList,
2180 IShellLinkW_fnSetIDList,
2181 IShellLinkW_fnGetDescription,
2182 IShellLinkW_fnSetDescription,
2183 IShellLinkW_fnGetWorkingDirectory,
2184 IShellLinkW_fnSetWorkingDirectory,
2185 IShellLinkW_fnGetArguments,
2186 IShellLinkW_fnSetArguments,
2187 IShellLinkW_fnGetHotkey,
2188 IShellLinkW_fnSetHotkey,
2189 IShellLinkW_fnGetShowCmd,
2190 IShellLinkW_fnSetShowCmd,
2191 IShellLinkW_fnGetIconLocation,
2192 IShellLinkW_fnSetIconLocation,
2193 IShellLinkW_fnSetRelativePath,
2194 IShellLinkW_fnResolve,
2195 IShellLinkW_fnSetPath
2198 static HRESULT WINAPI
2199 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2201 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2202 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2205 static ULONG WINAPI
2206 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2208 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2209 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2212 static ULONG WINAPI
2213 ShellLink_DataList_Release( IShellLinkDataList* iface )
2215 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2216 return IShellLinkW_Release(&This->IShellLinkW_iface);
2219 static HRESULT WINAPI
2220 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2222 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2223 return E_NOTIMPL;
2226 static HRESULT WINAPI
2227 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2229 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2230 LPVOID block = NULL;
2231 HRESULT r = E_FAIL;
2233 TRACE("%p %08lx %p\n", iface, dwSig, ppDataBlock );
2235 switch (dwSig)
2237 case EXP_DARWIN_ID_SIG:
2238 if (!This->sComponent)
2239 break;
2240 block = shelllink_build_darwinid( This->sComponent, dwSig );
2241 r = S_OK;
2242 break;
2243 case EXP_SZ_LINK_SIG:
2244 case NT_CONSOLE_PROPS_SIG:
2245 case NT_FE_CONSOLE_PROPS_SIG:
2246 case EXP_SPECIAL_FOLDER_SIG:
2247 case EXP_SZ_ICON_SIG:
2248 FIXME("valid but unhandled datablock %08lx\n", dwSig);
2249 break;
2250 default:
2251 ERR("unknown datablock %08lx\n", dwSig);
2253 *ppDataBlock = block;
2254 return r;
2257 static HRESULT WINAPI
2258 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2260 FIXME("(%p)->(%lu): stub\n", iface, dwSig);
2261 return E_NOTIMPL;
2264 static HRESULT WINAPI
2265 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2267 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2268 DWORD flags = 0;
2270 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2272 /* FIXME: add more */
2273 if (This->sArgs)
2274 flags |= SLDF_HAS_ARGS;
2275 if (This->sComponent)
2276 flags |= SLDF_HAS_DARWINID;
2277 if (This->sIcoPath)
2278 flags |= SLDF_HAS_ICONLOCATION;
2279 if (This->sProduct)
2280 flags |= SLDF_HAS_LOGO3ID;
2281 if (This->pPidl)
2282 flags |= SLDF_HAS_ID_LIST;
2284 *pdwFlags = flags;
2286 return S_OK;
2289 static HRESULT WINAPI
2290 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2292 FIXME("(%p)->(%lu): stub\n", iface, dwFlags);
2293 return S_OK;
2296 static const IShellLinkDataListVtbl dlvt =
2298 ShellLink_DataList_QueryInterface,
2299 ShellLink_DataList_AddRef,
2300 ShellLink_DataList_Release,
2301 ShellLink_AddDataBlock,
2302 ShellLink_CopyDataBlock,
2303 ShellLink_RemoveDataBlock,
2304 ShellLink_GetFlags,
2305 ShellLink_SetFlags
2308 static HRESULT WINAPI
2309 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2311 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2312 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2315 static ULONG WINAPI
2316 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2318 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2319 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2322 static ULONG WINAPI
2323 ShellLink_ExtInit_Release( IShellExtInit* iface )
2325 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2326 return IShellLinkW_Release(&This->IShellLinkW_iface);
2329 /**************************************************************************
2330 * ShellLink implementation of IShellExtInit::Initialize()
2332 * Loads the shelllink from the dataobject the shell is pointing to.
2334 static HRESULT WINAPI
2335 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2336 IDataObject *pdtobj, HKEY hkeyProgID )
2338 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2339 FORMATETC format;
2340 STGMEDIUM stgm;
2341 UINT count;
2342 HRESULT r = E_FAIL;
2344 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2346 if( !pdtobj )
2347 return r;
2349 format.cfFormat = CF_HDROP;
2350 format.ptd = NULL;
2351 format.dwAspect = DVASPECT_CONTENT;
2352 format.lindex = -1;
2353 format.tymed = TYMED_HGLOBAL;
2355 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2356 return r;
2358 count = DragQueryFileW( stgm.hGlobal, -1, NULL, 0 );
2359 if( count == 1 )
2361 LPWSTR path;
2363 count = DragQueryFileW( stgm.hGlobal, 0, NULL, 0 );
2364 count++;
2365 path = malloc( count * sizeof(WCHAR) );
2366 if( path )
2368 IPersistFile *pf = &This->IPersistFile_iface;
2370 count = DragQueryFileW( stgm.hGlobal, 0, path, count );
2371 r = IPersistFile_Load( pf, path, 0 );
2372 free( path );
2375 ReleaseStgMedium( &stgm );
2377 return r;
2380 static const IShellExtInitVtbl eivt =
2382 ShellLink_ExtInit_QueryInterface,
2383 ShellLink_ExtInit_AddRef,
2384 ShellLink_ExtInit_Release,
2385 ShellLink_ExtInit_Initialize
2388 static HRESULT WINAPI
2389 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2391 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2392 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2395 static ULONG WINAPI
2396 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2398 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2399 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2402 static ULONG WINAPI
2403 ShellLink_ContextMenu_Release( IContextMenu* iface )
2405 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2406 return IShellLinkW_Release(&This->IShellLinkW_iface);
2409 static HRESULT WINAPI
2410 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2411 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2413 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2414 MENUITEMINFOW mii;
2415 int id = 1;
2417 TRACE("%p %p %u %u %u %u\n", This,
2418 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2420 if ( !hmenu )
2421 return E_INVALIDARG;
2423 memset( &mii, 0, sizeof mii );
2424 mii.cbSize = sizeof mii;
2425 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2426 mii.dwTypeData = (LPWSTR)L"Open";
2427 mii.cch = lstrlenW( mii.dwTypeData );
2428 mii.wID = idCmdFirst + id++;
2429 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2430 mii.fType = MFT_STRING;
2431 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2432 return E_FAIL;
2433 This->iIdOpen = 0;
2435 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2438 static LPWSTR
2439 shelllink_get_msi_component_path( LPWSTR component )
2441 LPWSTR path;
2442 DWORD r, sz = 0;
2444 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2445 if (r != ERROR_SUCCESS)
2446 return NULL;
2448 sz++;
2449 path = malloc( sz * sizeof(WCHAR) );
2450 r = CommandLineFromMsiDescriptor( component, path, &sz );
2451 if (r != ERROR_SUCCESS)
2453 free( path );
2454 path = NULL;
2457 TRACE("returning %s\n", debugstr_w( path ) );
2459 return path;
2462 static HRESULT WINAPI
2463 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2465 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2466 SHELLEXECUTEINFOW sei;
2467 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2468 LPWSTR args = NULL;
2469 LPWSTR path = NULL;
2470 HRESULT r;
2472 TRACE("%p %p\n", This, lpici );
2474 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2475 return E_INVALIDARG;
2477 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2479 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2480 return E_INVALIDARG;
2483 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2484 if ( FAILED( r ) )
2485 return r;
2487 if ( This->sComponent )
2489 path = shelllink_get_msi_component_path( This->sComponent );
2490 if (!path)
2491 return E_FAIL;
2493 else
2494 path = wcsdup( This->sPath );
2496 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2497 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2499 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2500 DWORD len = 2;
2502 if ( This->sArgs )
2503 len += lstrlenW( This->sArgs );
2504 if ( iciex->lpParametersW )
2505 len += lstrlenW( iciex->lpParametersW );
2507 args = malloc( len * sizeof(WCHAR) );
2508 args[0] = 0;
2509 if ( This->sArgs )
2510 lstrcatW( args, This->sArgs );
2511 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2513 lstrcatW( args, L" " );
2514 lstrcatW( args, iciex->lpParametersW );
2518 memset( &sei, 0, sizeof sei );
2519 sei.cbSize = sizeof sei;
2520 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2521 sei.lpFile = path;
2522 sei.nShow = This->iShowCmd;
2523 sei.lpIDList = This->pPidl;
2524 sei.lpDirectory = This->sWorkDir;
2525 sei.lpParameters = args;
2526 sei.lpVerb = L"Open";
2528 if( ShellExecuteExW( &sei ) )
2529 r = S_OK;
2530 else
2531 r = E_FAIL;
2533 free( args );
2534 free( path );
2536 return r;
2539 static HRESULT WINAPI
2540 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2541 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2543 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2545 FIXME("(%p)->(%Iu %u %p %p %u): stub\n", This,
2546 idCmd, uType, pwReserved, pszName, cchMax );
2548 return E_NOTIMPL;
2551 static const IContextMenuVtbl cmvt =
2553 ShellLink_ContextMenu_QueryInterface,
2554 ShellLink_ContextMenu_AddRef,
2555 ShellLink_ContextMenu_Release,
2556 ShellLink_QueryContextMenu,
2557 ShellLink_InvokeCommand,
2558 ShellLink_GetCommandString
2561 static HRESULT WINAPI
2562 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2564 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2565 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2568 static ULONG WINAPI
2569 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2571 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2572 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2575 static ULONG WINAPI
2576 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2578 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2579 return IShellLinkW_Release(&This->IShellLinkW_iface);
2582 static HRESULT WINAPI
2583 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2585 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2587 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2589 if ( !This->site )
2590 return E_FAIL;
2591 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2594 static HRESULT WINAPI
2595 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2597 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2599 TRACE("%p %p\n", iface, punk);
2601 if ( punk )
2602 IUnknown_AddRef( punk );
2604 if( This->site )
2605 IUnknown_Release( This->site );
2607 This->site = punk;
2609 return S_OK;
2612 static const IObjectWithSiteVtbl owsvt =
2614 ShellLink_ObjectWithSite_QueryInterface,
2615 ShellLink_ObjectWithSite_AddRef,
2616 ShellLink_ObjectWithSite_Release,
2617 ShellLink_SetSite,
2618 ShellLink_GetSite,
2621 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2623 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2624 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2627 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2629 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2630 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2633 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2635 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2636 return IShellLinkW_Release(&This->IShellLinkW_iface);
2639 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2641 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2642 FIXME("(%p)->(%p): stub\n", This, props);
2643 return E_NOTIMPL;
2646 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2648 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2649 FIXME("(%p)->(%ld %p): stub\n", This, propid, key);
2650 return E_NOTIMPL;
2653 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2655 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2656 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2657 return E_NOTIMPL;
2660 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2662 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2663 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2664 return S_OK;
2667 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2669 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2670 FIXME("(%p): stub\n", This);
2671 return S_OK;
2674 static const IPropertyStoreVtbl propertystorevtbl = {
2675 propertystore_QueryInterface,
2676 propertystore_AddRef,
2677 propertystore_Release,
2678 propertystore_GetCount,
2679 propertystore_GetAt,
2680 propertystore_GetValue,
2681 propertystore_SetValue,
2682 propertystore_Commit
2685 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2687 IShellLinkImpl * sl;
2688 HRESULT r;
2690 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2692 *obj = NULL;
2694 if (outer)
2695 return CLASS_E_NOAGGREGATION;
2697 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2698 if (!sl)
2699 return E_OUTOFMEMORY;
2701 sl->ref = 1;
2702 sl->IShellLinkA_iface.lpVtbl = &slvt;
2703 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2704 sl->IPersistFile_iface.lpVtbl = &pfvt;
2705 sl->IPersistStream_iface.lpVtbl = &psvt;
2706 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2707 sl->IShellExtInit_iface.lpVtbl = &eivt;
2708 sl->IContextMenu_iface.lpVtbl = &cmvt;
2709 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2710 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2711 sl->iShowCmd = SW_SHOWNORMAL;
2712 sl->bDirty = FALSE;
2713 sl->iIdOpen = -1;
2714 sl->site = NULL;
2715 sl->filepath = NULL;
2717 TRACE("(%p)\n", sl);
2719 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2720 IShellLinkW_Release( &sl->IShellLinkW_iface );
2721 return r;