advapi32: Don't shadow keychain_item in mac_write_credential.
[wine/multimedia.git] / dlls / shell32 / shelllink.c
blob4000a1d90eac4ea5a50588eb14f9de9d373f7083
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 Time1; /* 0x1c */
77 FILETIME Time2; /* 0x24 */
78 FILETIME Time3; /* 0x2c */
79 DWORD dwFileLength; /* 0x34 File length */
80 DWORD nIcon; /* 0x38 icon number */
81 DWORD fStartup; /* 0x3c startup type */
82 DWORD wHotKey; /* 0x40 hotkey */
83 DWORD Unknown5; /* 0x44 */
84 DWORD Unknown6; /* 0x48 */
85 } LINK_HEADER, * PLINK_HEADER;
87 #define SHLINK_LOCAL 0
88 #define SHLINK_REMOTE 1
90 typedef struct _LOCATION_INFO
92 DWORD dwTotalSize;
93 DWORD dwHeaderSize;
94 DWORD dwFlags;
95 DWORD dwVolTableOfs;
96 DWORD dwLocalPathOfs;
97 DWORD dwNetworkVolTableOfs;
98 DWORD dwFinalPathOfs;
99 } LOCATION_INFO;
101 typedef struct _LOCAL_VOLUME_INFO
103 DWORD dwSize;
104 DWORD dwType;
105 DWORD dwVolSerial;
106 DWORD dwVolLabelOfs;
107 } LOCAL_VOLUME_INFO;
109 typedef struct volume_info_t
111 DWORD type;
112 DWORD serial;
113 WCHAR label[12]; /* assume 8.3 */
114 } volume_info;
116 #include "poppack.h"
118 static const IShellLinkAVtbl slvt;
119 static const IShellLinkWVtbl slvtw;
120 static const IPersistFileVtbl pfvt;
121 static const IPersistStreamVtbl psvt;
122 static const IShellLinkDataListVtbl dlvt;
123 static const IShellExtInitVtbl eivt;
124 static const IContextMenuVtbl cmvt;
125 static const IObjectWithSiteVtbl owsvt;
127 /* IShellLink Implementation */
129 typedef struct
131 IShellLinkA IShellLinkA_iface;
132 IShellLinkW IShellLinkW_iface;
133 IPersistFile IPersistFile_iface;
134 IPersistStream IPersistStream_iface;
135 IShellLinkDataList IShellLinkDataList_iface;
136 IShellExtInit IShellExtInit_iface;
137 IContextMenu IContextMenu_iface;
138 IObjectWithSite IObjectWithSite_iface;
140 LONG ref;
142 /* data structures according to the information in the link */
143 LPITEMIDLIST pPidl;
144 WORD wHotKey;
145 SYSTEMTIME time1;
146 SYSTEMTIME time2;
147 SYSTEMTIME time3;
149 DWORD iShowCmd;
150 LPWSTR sIcoPath;
151 INT iIcoNdx;
152 LPWSTR sPath;
153 LPWSTR sArgs;
154 LPWSTR sWorkDir;
155 LPWSTR sDescription;
156 LPWSTR sPathRel;
157 LPWSTR sProduct;
158 LPWSTR sComponent;
159 volume_info volume;
161 BOOL bDirty;
162 INT iIdOpen; /* id of the "Open" entry in the context menu */
163 IUnknown *site;
165 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
166 } IShellLinkImpl;
168 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
170 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
173 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
175 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
178 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
180 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
183 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
185 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
188 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
190 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
193 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
195 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
198 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
200 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
203 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
205 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
208 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
210 /* strdup on the process heap */
211 static inline LPWSTR heap_strdupAtoW( LPCSTR str)
213 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
214 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
215 if( !p )
216 return p;
217 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
218 return p;
221 static inline LPWSTR strdupW( LPCWSTR src )
223 LPWSTR dest;
224 if (!src) return NULL;
225 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
226 if (dest)
227 lstrcpyW(dest, src);
228 return dest;
231 /**************************************************************************
232 * IPersistFile_QueryInterface
234 static HRESULT WINAPI IPersistFile_fnQueryInterface(
235 IPersistFile* iface,
236 REFIID riid,
237 LPVOID *ppvObj)
239 IShellLinkImpl *This = impl_from_IPersistFile(iface);
240 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
243 /******************************************************************************
244 * IPersistFile_AddRef
246 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
248 IShellLinkImpl *This = impl_from_IPersistFile(iface);
249 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
252 /******************************************************************************
253 * IPersistFile_Release
255 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
257 IShellLinkImpl *This = impl_from_IPersistFile(iface);
258 return IShellLinkW_Release(&This->IShellLinkW_iface);
261 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
263 IShellLinkImpl *This = impl_from_IPersistFile(iface);
265 TRACE("(%p)->(%p)\n", This, pClassID);
267 *pClassID = CLSID_ShellLink;
269 return S_OK;
272 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
274 IShellLinkImpl *This = impl_from_IPersistFile(iface);
276 TRACE("(%p)\n",This);
278 if (This->bDirty)
279 return S_OK;
281 return S_FALSE;
284 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
286 IShellLinkImpl *This = impl_from_IPersistFile(iface);
287 IPersistStream *StreamThis = &This->IPersistStream_iface;
288 HRESULT r;
289 IStream *stm;
291 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
293 if( dwMode == 0 )
294 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
295 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
296 if( SUCCEEDED( r ) )
298 r = IPersistStream_Load(StreamThis, stm);
299 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
300 IStream_Release( stm );
302 /* update file path */
303 HeapFree(GetProcessHeap(), 0, This->filepath);
304 This->filepath = strdupW(pszFileName);
306 This->bDirty = FALSE;
308 TRACE("-- returning hr %08x\n", r);
309 return r;
312 BOOL run_winemenubuilder( const WCHAR *args )
314 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
315 LONG len;
316 LPWSTR buffer;
317 STARTUPINFOW si;
318 PROCESS_INFORMATION pi;
319 BOOL ret;
320 WCHAR app[MAX_PATH];
321 void *redir;
323 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
324 strcatW( app, menubuilder );
326 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
327 buffer = HeapAlloc( GetProcessHeap(), 0, len );
328 if( !buffer )
329 return FALSE;
331 strcpyW( buffer, app );
332 strcatW( buffer, args );
334 TRACE("starting %s\n",debugstr_w(buffer));
336 memset(&si, 0, sizeof(si));
337 si.cb = sizeof(si);
339 Wow64DisableWow64FsRedirection( &redir );
340 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
341 Wow64RevertWow64FsRedirection( redir );
343 HeapFree( GetProcessHeap(), 0, buffer );
345 if (ret)
347 CloseHandle( pi.hProcess );
348 CloseHandle( pi.hThread );
351 return ret;
354 static BOOL StartLinkProcessor( LPCOLESTR szLink )
356 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
357 LONG len;
358 LPWSTR buffer;
359 BOOL ret;
361 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
362 buffer = HeapAlloc( GetProcessHeap(), 0, len );
363 if( !buffer )
364 return FALSE;
366 wsprintfW( buffer, szFormat, szLink );
367 ret = run_winemenubuilder( buffer );
368 HeapFree( GetProcessHeap(), 0, buffer );
369 return ret;
372 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
374 IShellLinkImpl *This = impl_from_IPersistFile(iface);
375 IPersistStream *StreamThis = &This->IPersistStream_iface;
376 HRESULT r;
377 IStream *stm;
379 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
381 if (!pszFileName)
382 return E_FAIL;
384 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
385 if( SUCCEEDED( r ) )
387 r = IPersistStream_Save(StreamThis, stm, FALSE);
388 IStream_Release( stm );
390 if( SUCCEEDED( r ) )
392 StartLinkProcessor( pszFileName );
394 /* update file path */
395 HeapFree(GetProcessHeap(), 0, This->filepath);
396 This->filepath = strdupW(pszFileName);
398 This->bDirty = FALSE;
400 else
402 DeleteFileW( pszFileName );
403 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
407 return r;
410 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
412 IShellLinkImpl *This = impl_from_IPersistFile(iface);
413 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
414 return S_OK;
417 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
419 IShellLinkImpl *This = impl_from_IPersistFile(iface);
420 IMalloc *pMalloc;
422 TRACE("(%p)->(%p)\n", This, filename);
424 if (!This->filepath)
426 *filename = NULL;
427 return S_FALSE;
430 SHGetMalloc(&pMalloc);
431 *filename = IMalloc_Alloc(pMalloc, (strlenW(This->filepath)+1)*sizeof(WCHAR));
432 if (!*filename) return E_OUTOFMEMORY;
434 strcpyW(*filename, This->filepath);
436 return S_OK;
439 static const IPersistFileVtbl pfvt =
441 IPersistFile_fnQueryInterface,
442 IPersistFile_fnAddRef,
443 IPersistFile_fnRelease,
444 IPersistFile_fnGetClassID,
445 IPersistFile_fnIsDirty,
446 IPersistFile_fnLoad,
447 IPersistFile_fnSave,
448 IPersistFile_fnSaveCompleted,
449 IPersistFile_fnGetCurFile
452 /************************************************************************
453 * IPersistStream_QueryInterface
455 static HRESULT WINAPI IPersistStream_fnQueryInterface(
456 IPersistStream* iface,
457 REFIID riid,
458 VOID** ppvObj)
460 IShellLinkImpl *This = impl_from_IPersistStream(iface);
461 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
464 /************************************************************************
465 * IPersistStream_Release
467 static ULONG WINAPI IPersistStream_fnRelease(
468 IPersistStream* iface)
470 IShellLinkImpl *This = impl_from_IPersistStream(iface);
471 return IShellLinkW_Release(&This->IShellLinkW_iface);
474 /************************************************************************
475 * IPersistStream_AddRef
477 static ULONG WINAPI IPersistStream_fnAddRef(
478 IPersistStream* iface)
480 IShellLinkImpl *This = impl_from_IPersistStream(iface);
481 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
484 /************************************************************************
485 * IPersistStream_GetClassID
488 static HRESULT WINAPI IPersistStream_fnGetClassID(
489 IPersistStream* iface,
490 CLSID* pClassID)
492 IShellLinkImpl *This = impl_from_IPersistStream(iface);
493 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
496 /************************************************************************
497 * IPersistStream_IsDirty (IPersistStream)
499 static HRESULT WINAPI IPersistStream_fnIsDirty(
500 IPersistStream* iface)
502 IShellLinkImpl *This = impl_from_IPersistStream(iface);
504 TRACE("(%p)\n", This);
506 return S_OK;
510 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
512 DWORD count;
513 USHORT len;
514 LPVOID temp;
515 LPWSTR str;
516 HRESULT r;
518 TRACE("%p\n", stm);
520 count = 0;
521 r = IStream_Read(stm, &len, sizeof(len), &count);
522 if ( FAILED (r) || ( count != sizeof(len) ) )
523 return E_FAIL;
525 if( unicode )
526 len *= sizeof (WCHAR);
528 TRACE("reading %d\n", len);
529 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
530 if( !temp )
531 return E_OUTOFMEMORY;
532 count = 0;
533 r = IStream_Read(stm, temp, len, &count);
534 if( FAILED (r) || ( count != len ) )
536 HeapFree( GetProcessHeap(), 0, temp );
537 return E_FAIL;
540 TRACE("read %s\n", debugstr_an(temp,len));
542 /* convert to unicode if necessary */
543 if( !unicode )
545 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
546 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
547 if( !str )
549 HeapFree( GetProcessHeap(), 0, temp );
550 return E_OUTOFMEMORY;
552 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
553 HeapFree( GetProcessHeap(), 0, temp );
555 else
557 count /= 2;
558 str = temp;
560 str[count] = 0;
562 *pstr = str;
564 return S_OK;
567 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
569 DWORD size;
570 ULONG count;
571 HRESULT r;
572 struct sized_chunk {
573 DWORD size;
574 unsigned char data[1];
575 } *chunk;
577 TRACE("%p\n",stm);
579 r = IStream_Read( stm, &size, sizeof(size), &count );
580 if( FAILED( r ) || count != sizeof(size) )
581 return E_FAIL;
583 chunk = HeapAlloc( GetProcessHeap(), 0, size );
584 if( !chunk )
585 return E_OUTOFMEMORY;
587 chunk->size = size;
588 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
589 if( FAILED( r ) || count != (size - sizeof(size)) )
591 HeapFree( GetProcessHeap(), 0, chunk );
592 return E_FAIL;
595 TRACE("Read %d bytes\n",chunk->size);
597 *data = chunk;
599 return S_OK;
602 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
604 const int label_sz = sizeof volume->label/sizeof volume->label[0];
605 LPSTR label;
606 int len;
608 volume->serial = vol->dwVolSerial;
609 volume->type = vol->dwType;
611 if( !vol->dwVolLabelOfs )
612 return FALSE;
613 if( vol->dwSize <= vol->dwVolLabelOfs )
614 return FALSE;
615 len = vol->dwSize - vol->dwVolLabelOfs;
617 label = (LPSTR) vol;
618 label += vol->dwVolLabelOfs;
619 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
621 return TRUE;
624 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
626 int len = 0, wlen;
627 LPWSTR path;
629 while( p[len] && (len < maxlen) )
630 len++;
632 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
633 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
634 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
635 path[wlen] = 0;
637 return path;
640 static HRESULT Stream_LoadLocation( IStream *stm,
641 volume_info *volume, LPWSTR *path )
643 char *p = NULL;
644 LOCATION_INFO *loc;
645 HRESULT r;
646 DWORD n;
648 r = Stream_ReadChunk( stm, (LPVOID*) &p );
649 if( FAILED(r) )
650 return r;
652 loc = (LOCATION_INFO*) p;
653 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
655 HeapFree( GetProcessHeap(), 0, p );
656 return E_FAIL;
659 /* if there's valid local volume information, load it */
660 if( loc->dwVolTableOfs &&
661 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
663 LOCAL_VOLUME_INFO *volume_info;
665 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
666 Stream_LoadVolume( volume_info, volume );
669 /* if there's a local path, load it */
670 n = loc->dwLocalPathOfs;
671 if( n && (n < loc->dwTotalSize) )
672 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
674 TRACE("type %d serial %08x name %s path %s\n", volume->type,
675 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
677 HeapFree( GetProcessHeap(), 0, p );
678 return S_OK;
682 * The format of the advertised shortcut info seems to be:
684 * Offset Description
685 * ------ -----------
687 * 0 Length of the block (4 bytes, usually 0x314)
688 * 4 tag (dword)
689 * 8 string data in ASCII
690 * 8+0x104 string data in UNICODE
692 * In the original Win32 implementation the buffers are not initialized
693 * to zero, so data trailing the string is random garbage.
695 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
697 DWORD size;
698 ULONG count;
699 HRESULT r;
700 EXP_DARWIN_LINK buffer;
702 TRACE("%p\n",stm);
704 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
705 if( FAILED( r ) )
706 return r;
708 /* make sure that we read the size of the structure even on error */
709 size = sizeof buffer - sizeof (DWORD);
710 if( buffer.dbh.cbSize != sizeof buffer )
712 ERR("Ooops. This structure is not as expected...\n");
713 return E_FAIL;
716 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
717 if( FAILED( r ) )
718 return r;
720 if( count != size )
721 return E_FAIL;
723 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
725 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
727 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
728 return E_FAIL;
731 *str = HeapAlloc( GetProcessHeap(), 0,
732 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
733 lstrcpyW( *str, buffer.szwDarwinID );
735 return S_OK;
738 /************************************************************************
739 * IPersistStream_Load (IPersistStream)
741 static HRESULT WINAPI IPersistStream_fnLoad(
742 IPersistStream* iface,
743 IStream* stm)
745 LINK_HEADER hdr;
746 ULONG dwBytesRead;
747 BOOL unicode;
748 HRESULT r;
749 DWORD zero;
751 IShellLinkImpl *This = impl_from_IPersistStream(iface);
753 TRACE("%p %p\n", This, stm);
755 if( !stm )
756 return STG_E_INVALIDPOINTER;
758 dwBytesRead = 0;
759 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
760 if( FAILED( r ) )
761 return r;
763 if( dwBytesRead != sizeof(hdr))
764 return E_FAIL;
765 if( hdr.dwSize != sizeof(hdr))
766 return E_FAIL;
767 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
768 return E_FAIL;
770 /* free all the old stuff */
771 ILFree(This->pPidl);
772 This->pPidl = NULL;
773 memset( &This->volume, 0, sizeof This->volume );
774 HeapFree(GetProcessHeap(), 0, This->sPath);
775 This->sPath = NULL;
776 HeapFree(GetProcessHeap(), 0, This->sDescription);
777 This->sDescription = NULL;
778 HeapFree(GetProcessHeap(), 0, This->sPathRel);
779 This->sPathRel = NULL;
780 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
781 This->sWorkDir = NULL;
782 HeapFree(GetProcessHeap(), 0, This->sArgs);
783 This->sArgs = NULL;
784 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
785 This->sIcoPath = NULL;
786 HeapFree(GetProcessHeap(), 0, This->sProduct);
787 This->sProduct = NULL;
788 HeapFree(GetProcessHeap(), 0, This->sComponent);
789 This->sComponent = NULL;
791 This->wHotKey = (WORD)hdr.wHotKey;
792 This->iIcoNdx = hdr.nIcon;
793 FileTimeToSystemTime (&hdr.Time1, &This->time1);
794 FileTimeToSystemTime (&hdr.Time2, &This->time2);
795 FileTimeToSystemTime (&hdr.Time3, &This->time3);
796 if (TRACE_ON(shell))
798 WCHAR sTemp[MAX_PATH];
799 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
800 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
801 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
802 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
803 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
804 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
805 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
806 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
807 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
810 /* load all the new stuff */
811 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
813 r = ILLoadFromStream( stm, &This->pPidl );
814 if( FAILED( r ) )
815 return r;
817 pdump(This->pPidl);
819 /* load the location information */
820 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
821 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
822 if( FAILED( r ) )
823 goto end;
825 unicode = hdr.dwFlags & SLDF_UNICODE;
826 if( hdr.dwFlags & SLDF_HAS_NAME )
828 r = Stream_LoadString( stm, unicode, &This->sDescription );
829 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
831 if( FAILED( r ) )
832 goto end;
834 if( hdr.dwFlags & SLDF_HAS_RELPATH )
836 r = Stream_LoadString( stm, unicode, &This->sPathRel );
837 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
839 if( FAILED( r ) )
840 goto end;
842 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
844 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
845 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
847 if( FAILED( r ) )
848 goto end;
850 if( hdr.dwFlags & SLDF_HAS_ARGS )
852 r = Stream_LoadString( stm, unicode, &This->sArgs );
853 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
855 if( FAILED( r ) )
856 goto end;
858 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
860 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
861 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
863 if( FAILED( r ) )
864 goto end;
866 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
868 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
869 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
871 if( FAILED( r ) )
872 goto end;
874 if( hdr.dwFlags & SLDF_HAS_DARWINID )
876 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
877 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
879 if( FAILED( r ) )
880 goto end;
882 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
883 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
885 /* Some lnk files have extra data blocks starting with a
886 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
887 * one with a 0xa0000003 signature. However these don't seem to matter
888 * too much.
890 WARN("Last word was not zero\n");
893 TRACE("OK\n");
895 pdump (This->pPidl);
897 return S_OK;
898 end:
899 return r;
902 /************************************************************************
903 * Stream_WriteString
905 * Helper function for IPersistStream_Save. Writes a unicode string
906 * with terminating nul byte to a stream, preceded by the its length.
908 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
910 USHORT len = lstrlenW( str ) + 1;
911 DWORD count;
912 HRESULT r;
914 r = IStream_Write( stm, &len, sizeof(len), &count );
915 if( FAILED( r ) )
916 return r;
918 len *= sizeof(WCHAR);
920 r = IStream_Write( stm, str, len, &count );
921 if( FAILED( r ) )
922 return r;
924 return S_OK;
927 /************************************************************************
928 * Stream_WriteLocationInfo
930 * Writes the location info to a stream
932 * FIXME: One day we might want to write the network volume information
933 * and the final path.
934 * Figure out how Windows deals with unicode paths here.
936 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
937 volume_info *volume )
939 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
940 LOCAL_VOLUME_INFO *vol;
941 LOCATION_INFO *loc;
942 LPSTR szLabel, szPath, szFinalPath;
943 ULONG count = 0;
944 HRESULT hr;
946 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
948 /* figure out the size of everything */
949 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
950 NULL, 0, NULL, NULL );
951 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
952 NULL, 0, NULL, NULL );
953 volume_info_size = sizeof *vol + label_size;
954 final_path_size = 1;
955 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
957 /* create pointers to everything */
958 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
959 vol = (LOCAL_VOLUME_INFO*) &loc[1];
960 szLabel = (LPSTR) &vol[1];
961 szPath = &szLabel[label_size];
962 szFinalPath = &szPath[path_size];
964 /* fill in the location information header */
965 loc->dwTotalSize = total_size;
966 loc->dwHeaderSize = sizeof (*loc);
967 loc->dwFlags = 1;
968 loc->dwVolTableOfs = sizeof (*loc);
969 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
970 loc->dwNetworkVolTableOfs = 0;
971 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
973 /* fill in the volume information */
974 vol->dwSize = volume_info_size;
975 vol->dwType = volume->type;
976 vol->dwVolSerial = volume->serial;
977 vol->dwVolLabelOfs = sizeof (*vol);
979 /* copy in the strings */
980 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
981 szLabel, label_size, NULL, NULL );
982 WideCharToMultiByte( CP_ACP, 0, path, -1,
983 szPath, path_size, NULL, NULL );
984 szFinalPath[0] = 0;
986 hr = IStream_Write( stm, loc, total_size, &count );
987 HeapFree(GetProcessHeap(), 0, loc);
989 return hr;
992 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
994 EXP_DARWIN_LINK *buffer;
996 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
997 buffer->dbh.cbSize = sizeof *buffer;
998 buffer->dbh.dwSignature = magic;
999 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1000 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1002 return buffer;
1005 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1007 EXP_DARWIN_LINK *buffer;
1008 ULONG count;
1010 TRACE("%p\n",stm);
1012 buffer = shelllink_build_darwinid( string, magic );
1014 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1017 /************************************************************************
1018 * IPersistStream_Save (IPersistStream)
1020 * FIXME: makes assumptions about byte order
1022 static HRESULT WINAPI IPersistStream_fnSave(
1023 IPersistStream* iface,
1024 IStream* stm,
1025 BOOL fClearDirty)
1027 LINK_HEADER header;
1028 ULONG count;
1029 DWORD zero;
1030 HRESULT r;
1032 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1034 TRACE("%p %p %x\n", This, stm, fClearDirty);
1036 memset(&header, 0, sizeof(header));
1037 header.dwSize = sizeof(header);
1038 header.fStartup = This->iShowCmd;
1039 header.MagicGuid = CLSID_ShellLink;
1041 header.wHotKey = This->wHotKey;
1042 header.nIcon = This->iIcoNdx;
1043 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1044 if( This->pPidl )
1045 header.dwFlags |= SLDF_HAS_ID_LIST;
1046 if( This->sPath )
1047 header.dwFlags |= SLDF_HAS_LINK_INFO;
1048 if( This->sDescription )
1049 header.dwFlags |= SLDF_HAS_NAME;
1050 if( This->sWorkDir )
1051 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1052 if( This->sArgs )
1053 header.dwFlags |= SLDF_HAS_ARGS;
1054 if( This->sIcoPath )
1055 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1056 if( This->sProduct )
1057 header.dwFlags |= SLDF_HAS_LOGO3ID;
1058 if( This->sComponent )
1059 header.dwFlags |= SLDF_HAS_DARWINID;
1061 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1062 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1063 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1065 /* write the Shortcut header */
1066 r = IStream_Write( stm, &header, sizeof(header), &count );
1067 if( FAILED( r ) )
1069 ERR("Write failed at %d\n",__LINE__);
1070 return r;
1073 TRACE("Writing pidl\n");
1075 /* write the PIDL to the shortcut */
1076 if( This->pPidl )
1078 r = ILSaveToStream( stm, This->pPidl );
1079 if( FAILED( r ) )
1081 ERR("Failed to write PIDL at %d\n",__LINE__);
1082 return r;
1086 if( This->sPath )
1087 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1089 if( This->sDescription )
1090 r = Stream_WriteString( stm, This->sDescription );
1092 if( This->sPathRel )
1093 r = Stream_WriteString( stm, This->sPathRel );
1095 if( This->sWorkDir )
1096 r = Stream_WriteString( stm, This->sWorkDir );
1098 if( This->sArgs )
1099 r = Stream_WriteString( stm, This->sArgs );
1101 if( This->sIcoPath )
1102 r = Stream_WriteString( stm, This->sIcoPath );
1104 if( This->sProduct )
1105 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1107 if( This->sComponent )
1108 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1110 /* the last field is a single zero dword */
1111 zero = 0;
1112 r = IStream_Write( stm, &zero, sizeof zero, &count );
1114 return S_OK;
1117 /************************************************************************
1118 * IPersistStream_GetSizeMax (IPersistStream)
1120 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1121 IPersistStream* iface,
1122 ULARGE_INTEGER* pcbSize)
1124 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1126 TRACE("(%p)\n", This);
1128 return E_NOTIMPL;
1131 static const IPersistStreamVtbl psvt =
1133 IPersistStream_fnQueryInterface,
1134 IPersistStream_fnAddRef,
1135 IPersistStream_fnRelease,
1136 IPersistStream_fnGetClassID,
1137 IPersistStream_fnIsDirty,
1138 IPersistStream_fnLoad,
1139 IPersistStream_fnSave,
1140 IPersistStream_fnGetSizeMax
1143 /**************************************************************************
1144 * IShellLink_Constructor
1146 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1147 REFIID riid, LPVOID *ppv )
1149 IShellLinkImpl * sl;
1150 HRESULT r;
1152 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1154 *ppv = NULL;
1156 if (pUnkOuter)
1157 return CLASS_E_NOAGGREGATION;
1158 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1159 if (!sl)
1160 return E_OUTOFMEMORY;
1162 sl->ref = 1;
1163 sl->IShellLinkA_iface.lpVtbl = &slvt;
1164 sl->IShellLinkW_iface.lpVtbl = &slvtw;
1165 sl->IPersistFile_iface.lpVtbl = &pfvt;
1166 sl->IPersistStream_iface.lpVtbl = &psvt;
1167 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
1168 sl->IShellExtInit_iface.lpVtbl = &eivt;
1169 sl->IContextMenu_iface.lpVtbl = &cmvt;
1170 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
1171 sl->iShowCmd = SW_SHOWNORMAL;
1172 sl->bDirty = FALSE;
1173 sl->iIdOpen = -1;
1174 sl->site = NULL;
1175 sl->filepath = NULL;
1177 TRACE("(%p)->()\n",sl);
1179 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, ppv );
1180 IShellLinkW_Release( &sl->IShellLinkW_iface );
1181 return r;
1185 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1187 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1188 return FALSE;
1189 return TRUE;
1192 /**************************************************************************
1193 * ShellLink_UpdatePath
1194 * update absolute path in sPath using relative path in sPathRel
1196 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1198 if (!path || !psPath)
1199 return E_INVALIDARG;
1201 if (!*psPath && sPathRel) {
1202 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1203 LPWSTR final = NULL;
1205 /* first try if [directory of link file] + [relative path] finds an existing file */
1207 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1208 if( !final )
1209 final = buffer;
1210 lstrcpyW(final, sPathRel);
1212 *abs_path = '\0';
1214 if (SHELL_ExistsFileW(buffer)) {
1215 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1216 lstrcpyW(abs_path, buffer);
1217 } else {
1218 /* try if [working directory] + [relative path] finds an existing file */
1219 if (sWorkDir) {
1220 lstrcpyW(buffer, sWorkDir);
1221 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1223 if (SHELL_ExistsFileW(buffer))
1224 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1225 lstrcpyW(abs_path, buffer);
1229 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1230 if (!*abs_path)
1231 lstrcpyW(abs_path, sPathRel);
1233 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1234 if (!*psPath)
1235 return E_OUTOFMEMORY;
1237 lstrcpyW(*psPath, abs_path);
1240 return S_OK;
1243 /**************************************************************************
1244 * IShellLink_ConstructFromFile
1246 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1247 LPCITEMIDLIST pidl, IUnknown **ppv)
1249 IShellLinkW* psl;
1251 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1253 if (SUCCEEDED(hr)) {
1254 IPersistFile* ppf;
1256 *ppv = NULL;
1258 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1260 if (SUCCEEDED(hr)) {
1261 WCHAR path[MAX_PATH];
1263 if (SHGetPathFromIDListW(pidl, path))
1264 hr = IPersistFile_Load(ppf, path, 0);
1265 else
1266 hr = E_FAIL;
1268 if (SUCCEEDED(hr))
1269 *ppv = (IUnknown*)psl;
1271 IPersistFile_Release(ppf);
1274 if (!*ppv)
1275 IShellLinkW_Release(psl);
1278 return hr;
1281 /**************************************************************************
1282 * IShellLinkA_QueryInterface
1284 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1286 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1287 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1290 /******************************************************************************
1291 * IShellLinkA_AddRef
1293 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1295 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1296 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1299 /******************************************************************************
1300 * IShellLinkA_Release
1302 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1304 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1305 return IShellLinkW_Release(&This->IShellLinkW_iface);
1308 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1309 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1311 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1313 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1314 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1316 if (This->sComponent || This->sProduct)
1317 return S_FALSE;
1319 if (cchMaxPath)
1320 pszFile[0] = 0;
1321 if (This->sPath)
1322 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1323 pszFile, cchMaxPath, NULL, NULL);
1325 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1327 return S_OK;
1330 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1332 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1333 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1336 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1338 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1339 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1342 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1343 INT cchMaxName)
1345 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1347 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1349 if( cchMaxName )
1350 pszName[0] = 0;
1351 if( This->sDescription )
1352 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1353 pszName, cchMaxName, NULL, NULL);
1355 return S_OK;
1358 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1360 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1361 WCHAR *descrW;
1362 HRESULT hr;
1364 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1366 if (pszName)
1368 descrW = heap_strdupAtoW(pszName);
1369 if (!descrW) return E_OUTOFMEMORY;
1371 else
1372 descrW = NULL;
1374 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1375 HeapFree(GetProcessHeap(), 0, descrW);
1377 return hr;
1380 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1381 INT cchMaxPath)
1383 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1385 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1387 if( cchMaxPath )
1388 pszDir[0] = 0;
1389 if( This->sWorkDir )
1390 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1391 pszDir, cchMaxPath, NULL, NULL);
1393 return S_OK;
1396 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1398 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1399 WCHAR *dirW;
1400 HRESULT hr;
1402 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1404 dirW = heap_strdupAtoW(pszDir);
1405 if (!dirW) return E_OUTOFMEMORY;
1407 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1408 HeapFree(GetProcessHeap(), 0, dirW);
1410 return hr;
1413 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1415 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1417 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1419 if( cchMaxPath )
1420 pszArgs[0] = 0;
1421 if( This->sArgs )
1422 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1423 pszArgs, cchMaxPath, NULL, NULL);
1425 return S_OK;
1428 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1430 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1431 WCHAR *argsW;
1432 HRESULT hr;
1434 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1436 if (pszArgs)
1438 argsW = heap_strdupAtoW(pszArgs);
1439 if (!argsW) return E_OUTOFMEMORY;
1441 else
1442 argsW = NULL;
1444 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1445 HeapFree(GetProcessHeap(), 0, argsW);
1447 return hr;
1450 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1452 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1453 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1456 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1458 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1459 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1462 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1464 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1465 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1468 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1470 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1471 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1474 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1475 INT cchIconPath, INT *piIcon)
1477 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1479 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1481 *piIcon = This->iIcoNdx;
1483 if (This->sIcoPath)
1484 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1485 else
1486 pszIconPath[0] = 0;
1488 return S_OK;
1491 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR pszIconPath,
1492 INT iIcon)
1494 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1495 WCHAR *pathW;
1496 HRESULT hr;
1498 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1500 pathW = heap_strdupAtoW(pszIconPath);
1501 if (!pathW) return E_OUTOFMEMORY;
1503 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, pathW, iIcon);
1504 HeapFree(GetProcessHeap(), 0, pathW);
1506 return hr;
1509 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1510 DWORD dwReserved)
1512 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1513 WCHAR *pathW;
1514 HRESULT hr;
1516 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1518 pathW = heap_strdupAtoW(pszPathRel);
1519 if (!pathW) return E_OUTOFMEMORY;
1521 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1522 HeapFree(GetProcessHeap(), 0, pathW);
1524 return hr;
1527 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1529 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1531 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1533 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1536 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1538 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1539 HRESULT r;
1540 LPWSTR str;
1542 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1544 if (!pszFile) return E_INVALIDARG;
1546 str = heap_strdupAtoW(pszFile);
1547 if( !str )
1548 return E_OUTOFMEMORY;
1550 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1551 HeapFree( GetProcessHeap(), 0, str );
1553 return r;
1556 /**************************************************************************
1557 * IShellLink Implementation
1560 static const IShellLinkAVtbl slvt =
1562 IShellLinkA_fnQueryInterface,
1563 IShellLinkA_fnAddRef,
1564 IShellLinkA_fnRelease,
1565 IShellLinkA_fnGetPath,
1566 IShellLinkA_fnGetIDList,
1567 IShellLinkA_fnSetIDList,
1568 IShellLinkA_fnGetDescription,
1569 IShellLinkA_fnSetDescription,
1570 IShellLinkA_fnGetWorkingDirectory,
1571 IShellLinkA_fnSetWorkingDirectory,
1572 IShellLinkA_fnGetArguments,
1573 IShellLinkA_fnSetArguments,
1574 IShellLinkA_fnGetHotkey,
1575 IShellLinkA_fnSetHotkey,
1576 IShellLinkA_fnGetShowCmd,
1577 IShellLinkA_fnSetShowCmd,
1578 IShellLinkA_fnGetIconLocation,
1579 IShellLinkA_fnSetIconLocation,
1580 IShellLinkA_fnSetRelativePath,
1581 IShellLinkA_fnResolve,
1582 IShellLinkA_fnSetPath
1586 /**************************************************************************
1587 * IShellLinkW_fnQueryInterface
1589 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1590 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1592 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1594 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1596 *ppvObj = NULL;
1598 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1600 *ppvObj = &This->IShellLinkA_iface;
1602 else if(IsEqualIID(riid, &IID_IShellLinkW))
1604 *ppvObj = &This->IShellLinkW_iface;
1606 else if(IsEqualIID(riid, &IID_IPersistFile))
1608 *ppvObj = &This->IPersistFile_iface;
1610 else if(IsEqualIID(riid, &IID_IPersistStream))
1612 *ppvObj = &This->IPersistStream_iface;
1614 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1616 *ppvObj = &This->IShellLinkDataList_iface;
1618 else if(IsEqualIID(riid, &IID_IShellExtInit))
1620 *ppvObj = &This->IShellExtInit_iface;
1622 else if(IsEqualIID(riid, &IID_IContextMenu))
1624 *ppvObj = &This->IContextMenu_iface;
1626 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1628 *ppvObj = &This->IObjectWithSite_iface;
1631 if(*ppvObj)
1633 IUnknown_AddRef((IUnknown*)*ppvObj);
1634 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1635 return S_OK;
1637 ERR("-- Interface: E_NOINTERFACE\n");
1638 return E_NOINTERFACE;
1641 /******************************************************************************
1642 * IShellLinkW_fnAddRef
1644 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1646 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1647 ULONG ref = InterlockedIncrement(&This->ref);
1649 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1651 return ref;
1654 /******************************************************************************
1655 * IShellLinkW_fnRelease
1657 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1659 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1660 ULONG refCount = InterlockedDecrement(&This->ref);
1662 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1664 if (refCount)
1665 return refCount;
1667 TRACE("-- destroying IShellLink(%p)\n",This);
1669 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1670 HeapFree(GetProcessHeap(), 0, This->sArgs);
1671 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1672 HeapFree(GetProcessHeap(), 0, This->sDescription);
1673 HeapFree(GetProcessHeap(), 0, This->sPath);
1674 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1675 HeapFree(GetProcessHeap(), 0, This->sProduct);
1676 HeapFree(GetProcessHeap(), 0, This->sComponent);
1677 HeapFree(GetProcessHeap(), 0, This->filepath);
1679 if (This->site)
1680 IUnknown_Release( This->site );
1682 if (This->pPidl)
1683 ILFree(This->pPidl);
1685 LocalFree(This);
1687 return 0;
1690 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1692 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1694 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1695 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1697 if (This->sComponent || This->sProduct)
1698 return S_FALSE;
1700 if (cchMaxPath)
1701 pszFile[0] = 0;
1702 if (This->sPath)
1703 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1705 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1707 return S_OK;
1710 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1712 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1714 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1716 if (!This->pPidl)
1718 *ppidl = NULL;
1719 return S_FALSE;
1721 *ppidl = ILClone(This->pPidl);
1722 return S_OK;
1725 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1727 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1729 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1731 if( This->pPidl )
1732 ILFree( This->pPidl );
1733 This->pPidl = ILClone( pidl );
1734 if( !This->pPidl )
1735 return E_FAIL;
1737 This->bDirty = TRUE;
1739 return S_OK;
1742 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1744 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1746 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1748 pszName[0] = 0;
1749 if( This->sDescription )
1750 lstrcpynW( pszName, This->sDescription, cchMaxName );
1752 return S_OK;
1755 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1757 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1759 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1761 HeapFree(GetProcessHeap(), 0, This->sDescription);
1762 if (pszName)
1764 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1765 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1766 if ( !This->sDescription )
1767 return E_OUTOFMEMORY;
1769 lstrcpyW( This->sDescription, pszName );
1771 else
1772 This->sDescription = NULL;
1773 This->bDirty = TRUE;
1775 return S_OK;
1778 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1780 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1782 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1784 if( cchMaxPath )
1785 pszDir[0] = 0;
1786 if( This->sWorkDir )
1787 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1789 return S_OK;
1792 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1794 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1796 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1798 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1799 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1800 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1801 if ( !This->sWorkDir )
1802 return E_OUTOFMEMORY;
1803 lstrcpyW( This->sWorkDir, pszDir );
1804 This->bDirty = TRUE;
1806 return S_OK;
1809 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1811 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1813 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1815 if( cchMaxPath )
1816 pszArgs[0] = 0;
1817 if( This->sArgs )
1818 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1820 return S_OK;
1823 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1825 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1827 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1829 HeapFree(GetProcessHeap(), 0, This->sArgs);
1830 if (pszArgs)
1832 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1833 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1834 if ( !This->sArgs )
1835 return E_OUTOFMEMORY;
1836 lstrcpyW( This->sArgs, pszArgs );
1838 else This->sArgs = NULL;
1840 This->bDirty = TRUE;
1842 return S_OK;
1845 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1847 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1849 TRACE("(%p)->(%p)\n",This, pwHotkey);
1851 *pwHotkey=This->wHotKey;
1853 return S_OK;
1856 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1858 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1860 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1862 This->wHotKey = wHotkey;
1863 This->bDirty = TRUE;
1865 return S_OK;
1868 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1870 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1872 TRACE("(%p)->(%p)\n",This, piShowCmd);
1874 *piShowCmd = This->iShowCmd;
1876 return S_OK;
1879 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1881 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1883 TRACE("(%p)->(%d)\n", This, iShowCmd);
1885 This->iShowCmd = iShowCmd;
1886 This->bDirty = TRUE;
1888 return S_OK;
1891 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1893 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1895 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1897 *piIcon = This->iIcoNdx;
1899 if (This->sIcoPath)
1900 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1901 else
1902 pszIconPath[0] = 0;
1904 return S_OK;
1907 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1909 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1911 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1913 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1914 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1915 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1916 if ( !This->sIcoPath )
1917 return E_OUTOFMEMORY;
1918 lstrcpyW( This->sIcoPath, pszIconPath );
1920 This->iIcoNdx = iIcon;
1921 This->bDirty = TRUE;
1923 return S_OK;
1926 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1928 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1930 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1932 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1933 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1934 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1935 if ( !This->sPathRel )
1936 return E_OUTOFMEMORY;
1937 lstrcpyW( This->sPathRel, pszPathRel );
1938 This->bDirty = TRUE;
1940 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1943 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1945 HRESULT hr = S_OK;
1946 BOOL bSuccess;
1948 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1950 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1952 /*FIXME: use IResolveShellLink interface */
1954 if (!This->sPath && This->pPidl) {
1955 WCHAR buffer[MAX_PATH];
1957 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1959 if (bSuccess && *buffer) {
1960 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1961 if (!This->sPath)
1962 return E_OUTOFMEMORY;
1964 lstrcpyW(This->sPath, buffer);
1966 This->bDirty = TRUE;
1967 } else
1968 hr = S_OK; /* don't report an error occurred while just caching information */
1971 if (!This->sIcoPath && This->sPath) {
1972 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1973 if (!This->sIcoPath)
1974 return E_OUTOFMEMORY;
1976 lstrcpyW(This->sIcoPath, This->sPath);
1977 This->iIcoNdx = 0;
1979 This->bDirty = TRUE;
1982 return hr;
1985 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
1987 LPWSTR ret;
1988 LPCWSTR p;
1989 DWORD len;
1991 if( !str )
1992 return NULL;
1994 p = strchrW( str, ':' );
1995 if( !p )
1996 return NULL;
1997 len = p - str;
1998 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
1999 if( !ret )
2000 return ret;
2001 memcpy( ret, str, sizeof(WCHAR)*len );
2002 ret[len] = 0;
2003 return ret;
2006 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2008 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2009 WCHAR szGuid[39];
2010 HRESULT r;
2011 GUID guid;
2012 int len;
2014 while( str[0] )
2016 /* each segment must start with two colons */
2017 if( str[0] != ':' || str[1] != ':' )
2018 return E_FAIL;
2020 /* the last segment is just two colons */
2021 if( !str[2] )
2022 break;
2023 str += 2;
2025 /* there must be a colon straight after a guid */
2026 p = strchrW( str, ':' );
2027 if( !p )
2028 return E_FAIL;
2029 len = p - str;
2030 if( len != 38 )
2031 return E_FAIL;
2033 /* get the guid, and check it's validly formatted */
2034 memcpy( szGuid, str, sizeof(WCHAR)*len );
2035 szGuid[len] = 0;
2036 r = CLSIDFromString( szGuid, &guid );
2037 if( r != S_OK )
2038 return r;
2039 str = p + 1;
2041 /* match it up to a guid that we care about */
2042 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2043 szComponent = str;
2044 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2045 szProduct = str;
2046 else
2047 return E_FAIL;
2049 /* skip to the next field */
2050 str = strchrW( str, ':' );
2051 if( !str )
2052 return E_FAIL;
2055 /* we have to have a component for an advertised shortcut */
2056 if( !szComponent )
2057 return E_FAIL;
2059 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2060 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2062 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2063 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2065 return S_OK;
2068 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2070 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2071 WCHAR drive[] = { path[0], ':', '\\', 0 };
2072 BOOL r;
2074 volume->type = GetDriveTypeW(drive);
2075 r = GetVolumeInformationW(drive, volume->label, label_sz,
2076 &volume->serial, NULL, NULL, NULL, 0);
2077 TRACE("r = %d type %d serial %08x name %s\n", r,
2078 volume->type, volume->serial, debugstr_w(volume->label));
2079 return r;
2082 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2084 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2085 WCHAR buffer[MAX_PATH];
2086 LPWSTR fname, unquoted = NULL;
2087 HRESULT hr = S_OK;
2088 UINT len;
2090 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2092 if (!pszFile) return E_INVALIDARG;
2094 /* quotes at the ends of the string are stripped */
2095 len = lstrlenW(pszFile);
2096 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2098 unquoted = strdupW(pszFile);
2099 PathUnquoteSpacesW(unquoted);
2100 pszFile = unquoted;
2103 /* any other quote marks are invalid */
2104 if (strchrW(pszFile, '"'))
2106 HeapFree(GetProcessHeap(), 0, unquoted);
2107 return S_FALSE;
2110 HeapFree(GetProcessHeap(), 0, This->sPath);
2111 This->sPath = NULL;
2113 HeapFree(GetProcessHeap(), 0, This->sComponent);
2114 This->sComponent = NULL;
2116 if (This->pPidl)
2117 ILFree(This->pPidl);
2118 This->pPidl = NULL;
2120 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2122 if (*pszFile == '\0')
2123 *buffer = '\0';
2124 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2125 return E_FAIL;
2126 else if(!PathFileExistsW(buffer) &&
2127 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2128 hr = S_FALSE;
2130 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2131 ShellLink_GetVolumeInfo(buffer, &This->volume);
2133 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2134 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2135 if (!This->sPath)
2137 HeapFree(GetProcessHeap(), 0, unquoted);
2138 return E_OUTOFMEMORY;
2141 lstrcpyW(This->sPath, buffer);
2143 This->bDirty = TRUE;
2144 HeapFree(GetProcessHeap(), 0, unquoted);
2146 return hr;
2149 /**************************************************************************
2150 * IShellLinkW Implementation
2153 static const IShellLinkWVtbl slvtw =
2155 IShellLinkW_fnQueryInterface,
2156 IShellLinkW_fnAddRef,
2157 IShellLinkW_fnRelease,
2158 IShellLinkW_fnGetPath,
2159 IShellLinkW_fnGetIDList,
2160 IShellLinkW_fnSetIDList,
2161 IShellLinkW_fnGetDescription,
2162 IShellLinkW_fnSetDescription,
2163 IShellLinkW_fnGetWorkingDirectory,
2164 IShellLinkW_fnSetWorkingDirectory,
2165 IShellLinkW_fnGetArguments,
2166 IShellLinkW_fnSetArguments,
2167 IShellLinkW_fnGetHotkey,
2168 IShellLinkW_fnSetHotkey,
2169 IShellLinkW_fnGetShowCmd,
2170 IShellLinkW_fnSetShowCmd,
2171 IShellLinkW_fnGetIconLocation,
2172 IShellLinkW_fnSetIconLocation,
2173 IShellLinkW_fnSetRelativePath,
2174 IShellLinkW_fnResolve,
2175 IShellLinkW_fnSetPath
2178 static HRESULT WINAPI
2179 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2181 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2182 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2185 static ULONG WINAPI
2186 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2188 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2189 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2192 static ULONG WINAPI
2193 ShellLink_DataList_Release( IShellLinkDataList* iface )
2195 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2196 return IShellLinkW_Release(&This->IShellLinkW_iface);
2199 static HRESULT WINAPI
2200 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2202 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2203 return E_NOTIMPL;
2206 static HRESULT WINAPI
2207 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2209 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2210 LPVOID block = NULL;
2211 HRESULT r = E_FAIL;
2213 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2215 switch (dwSig)
2217 case EXP_DARWIN_ID_SIG:
2218 if (!This->sComponent)
2219 break;
2220 block = shelllink_build_darwinid( This->sComponent, dwSig );
2221 r = S_OK;
2222 break;
2223 case EXP_SZ_LINK_SIG:
2224 case NT_CONSOLE_PROPS_SIG:
2225 case NT_FE_CONSOLE_PROPS_SIG:
2226 case EXP_SPECIAL_FOLDER_SIG:
2227 case EXP_SZ_ICON_SIG:
2228 FIXME("valid but unhandled datablock %08x\n", dwSig);
2229 break;
2230 default:
2231 ERR("unknown datablock %08x\n", dwSig);
2233 *ppDataBlock = block;
2234 return r;
2237 static HRESULT WINAPI
2238 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2240 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2241 return E_NOTIMPL;
2244 static HRESULT WINAPI
2245 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2247 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2248 DWORD flags = 0;
2250 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2252 /* FIXME: add more */
2253 if (This->sArgs)
2254 flags |= SLDF_HAS_ARGS;
2255 if (This->sComponent)
2256 flags |= SLDF_HAS_DARWINID;
2257 if (This->sIcoPath)
2258 flags |= SLDF_HAS_ICONLOCATION;
2259 if (This->sProduct)
2260 flags |= SLDF_HAS_LOGO3ID;
2261 if (This->pPidl)
2262 flags |= SLDF_HAS_ID_LIST;
2264 *pdwFlags = flags;
2266 return S_OK;
2269 static HRESULT WINAPI
2270 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2272 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2273 return E_NOTIMPL;
2276 static const IShellLinkDataListVtbl dlvt =
2278 ShellLink_DataList_QueryInterface,
2279 ShellLink_DataList_AddRef,
2280 ShellLink_DataList_Release,
2281 ShellLink_AddDataBlock,
2282 ShellLink_CopyDataBlock,
2283 ShellLink_RemoveDataBlock,
2284 ShellLink_GetFlags,
2285 ShellLink_SetFlags
2288 static HRESULT WINAPI
2289 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2291 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2292 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2295 static ULONG WINAPI
2296 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2298 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2299 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2302 static ULONG WINAPI
2303 ShellLink_ExtInit_Release( IShellExtInit* iface )
2305 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2306 return IShellLinkW_Release(&This->IShellLinkW_iface);
2309 /**************************************************************************
2310 * ShellLink implementation of IShellExtInit::Initialize()
2312 * Loads the shelllink from the dataobject the shell is pointing to.
2314 static HRESULT WINAPI
2315 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2316 IDataObject *pdtobj, HKEY hkeyProgID )
2318 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2319 FORMATETC format;
2320 STGMEDIUM stgm;
2321 UINT count;
2322 HRESULT r = E_FAIL;
2324 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2326 if( !pdtobj )
2327 return r;
2329 format.cfFormat = CF_HDROP;
2330 format.ptd = NULL;
2331 format.dwAspect = DVASPECT_CONTENT;
2332 format.lindex = -1;
2333 format.tymed = TYMED_HGLOBAL;
2335 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2336 return r;
2338 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2339 if( count == 1 )
2341 LPWSTR path;
2343 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2344 count++;
2345 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2346 if( path )
2348 IPersistFile *pf = &This->IPersistFile_iface;
2350 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2351 r = IPersistFile_Load( pf, path, 0 );
2352 HeapFree( GetProcessHeap(), 0, path );
2355 ReleaseStgMedium( &stgm );
2357 return r;
2360 static const IShellExtInitVtbl eivt =
2362 ShellLink_ExtInit_QueryInterface,
2363 ShellLink_ExtInit_AddRef,
2364 ShellLink_ExtInit_Release,
2365 ShellLink_ExtInit_Initialize
2368 static HRESULT WINAPI
2369 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2371 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2372 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2375 static ULONG WINAPI
2376 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2378 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2379 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2382 static ULONG WINAPI
2383 ShellLink_ContextMenu_Release( IContextMenu* iface )
2385 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2386 return IShellLinkW_Release(&This->IShellLinkW_iface);
2389 static HRESULT WINAPI
2390 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2391 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2393 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2394 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2395 MENUITEMINFOW mii;
2396 int id = 1;
2398 TRACE("%p %p %u %u %u %u\n", This,
2399 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2401 if ( !hmenu )
2402 return E_INVALIDARG;
2404 memset( &mii, 0, sizeof mii );
2405 mii.cbSize = sizeof mii;
2406 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2407 mii.dwTypeData = szOpen;
2408 mii.cch = strlenW( mii.dwTypeData );
2409 mii.wID = idCmdFirst + id++;
2410 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2411 mii.fType = MFT_STRING;
2412 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2413 return E_FAIL;
2414 This->iIdOpen = 0;
2416 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2419 static LPWSTR
2420 shelllink_get_msi_component_path( LPWSTR component )
2422 LPWSTR path;
2423 DWORD r, sz = 0;
2425 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2426 if (r != ERROR_SUCCESS)
2427 return NULL;
2429 sz++;
2430 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2431 r = CommandLineFromMsiDescriptor( component, path, &sz );
2432 if (r != ERROR_SUCCESS)
2434 HeapFree( GetProcessHeap(), 0, path );
2435 path = NULL;
2438 TRACE("returning %s\n", debugstr_w( path ) );
2440 return path;
2443 static HRESULT WINAPI
2444 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2446 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2447 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2448 SHELLEXECUTEINFOW sei;
2449 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2450 LPWSTR args = NULL;
2451 LPWSTR path = NULL;
2452 HRESULT r;
2454 TRACE("%p %p\n", This, lpici );
2456 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2457 return E_INVALIDARG;
2459 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2461 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2462 return E_INVALIDARG;
2465 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2466 if ( FAILED( r ) )
2467 return r;
2469 if ( This->sComponent )
2471 path = shelllink_get_msi_component_path( This->sComponent );
2472 if (!path)
2473 return E_FAIL;
2475 else
2476 path = strdupW( This->sPath );
2478 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2479 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2481 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2482 DWORD len = 2;
2484 if ( This->sArgs )
2485 len += lstrlenW( This->sArgs );
2486 if ( iciex->lpParametersW )
2487 len += lstrlenW( iciex->lpParametersW );
2489 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2490 args[0] = 0;
2491 if ( This->sArgs )
2492 lstrcatW( args, This->sArgs );
2493 if ( iciex->lpParametersW )
2495 static const WCHAR space[] = { ' ', 0 };
2496 lstrcatW( args, space );
2497 lstrcatW( args, iciex->lpParametersW );
2501 memset( &sei, 0, sizeof sei );
2502 sei.cbSize = sizeof sei;
2503 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2504 sei.lpFile = path;
2505 sei.nShow = This->iShowCmd;
2506 sei.lpIDList = This->pPidl;
2507 sei.lpDirectory = This->sWorkDir;
2508 sei.lpParameters = args;
2509 sei.lpVerb = szOpen;
2511 if( ShellExecuteExW( &sei ) )
2512 r = S_OK;
2513 else
2514 r = E_FAIL;
2516 HeapFree( GetProcessHeap(), 0, args );
2517 HeapFree( GetProcessHeap(), 0, path );
2519 return r;
2522 static HRESULT WINAPI
2523 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2524 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2526 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2528 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2529 idCmd, uType, pwReserved, pszName, cchMax );
2531 return E_NOTIMPL;
2534 static const IContextMenuVtbl cmvt =
2536 ShellLink_ContextMenu_QueryInterface,
2537 ShellLink_ContextMenu_AddRef,
2538 ShellLink_ContextMenu_Release,
2539 ShellLink_QueryContextMenu,
2540 ShellLink_InvokeCommand,
2541 ShellLink_GetCommandString
2544 static HRESULT WINAPI
2545 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2547 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2548 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2551 static ULONG WINAPI
2552 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2554 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2555 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2558 static ULONG WINAPI
2559 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2561 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2562 return IShellLinkW_Release(&This->IShellLinkW_iface);
2565 static HRESULT WINAPI
2566 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2568 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2570 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2572 if ( !This->site )
2573 return E_FAIL;
2574 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2577 static HRESULT WINAPI
2578 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2580 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2582 TRACE("%p %p\n", iface, punk);
2584 if ( punk )
2585 IUnknown_AddRef( punk );
2587 if( This->site )
2588 IUnknown_Release( This->site );
2590 This->site = punk;
2592 return S_OK;
2595 static const IObjectWithSiteVtbl owsvt =
2597 ShellLink_ObjectWithSite_QueryInterface,
2598 ShellLink_ObjectWithSite_AddRef,
2599 ShellLink_ObjectWithSite_Release,
2600 ShellLink_SetSite,
2601 ShellLink_GetSite,