shell32: Remove unnecessary WINAPI and some casts.
[wine/wine-gecko.git] / dlls / shell32 / shelllink.c
blob5fd48205bf68fbf10e155f7fbd9eebcf5a36a937
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 * ShellLink::QueryInterface implementation
234 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
236 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
238 *ppvObj = NULL;
240 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
242 *ppvObj = &This->IShellLinkA_iface;
244 else if(IsEqualIID(riid, &IID_IShellLinkW))
246 *ppvObj = &This->IShellLinkW_iface;
248 else if(IsEqualIID(riid, &IID_IPersistFile))
250 *ppvObj = &This->IPersistFile_iface;
252 else if(IsEqualIID(riid, &IID_IPersistStream))
254 *ppvObj = &This->IPersistStream_iface;
256 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
258 *ppvObj = &This->IShellLinkDataList_iface;
260 else if(IsEqualIID(riid, &IID_IShellExtInit))
262 *ppvObj = &This->IShellExtInit_iface;
264 else if(IsEqualIID(riid, &IID_IContextMenu))
266 *ppvObj = &This->IContextMenu_iface;
268 else if(IsEqualIID(riid, &IID_IObjectWithSite))
270 *ppvObj = &This->IObjectWithSite_iface;
273 if(*ppvObj)
275 IUnknown_AddRef((IUnknown*)(*ppvObj));
276 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
277 return S_OK;
279 ERR("-- Interface: E_NOINTERFACE\n");
280 return E_NOINTERFACE;
283 /**************************************************************************
284 * IPersistFile_QueryInterface
286 static HRESULT WINAPI IPersistFile_fnQueryInterface(
287 IPersistFile* iface,
288 REFIID riid,
289 LPVOID *ppvObj)
291 IShellLinkImpl *This = impl_from_IPersistFile(iface);
292 return ShellLink_QueryInterface( This, riid, ppvObj );
295 /******************************************************************************
296 * IPersistFile_AddRef
298 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
300 IShellLinkImpl *This = impl_from_IPersistFile(iface);
301 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
304 /******************************************************************************
305 * IPersistFile_Release
307 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
309 IShellLinkImpl *This = impl_from_IPersistFile(iface);
310 return IShellLinkA_Release(&This->IShellLinkA_iface);
313 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
315 IShellLinkImpl *This = impl_from_IPersistFile(iface);
317 TRACE("(%p)->(%p)\n", This, pClassID);
319 *pClassID = CLSID_ShellLink;
321 return S_OK;
324 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
326 IShellLinkImpl *This = impl_from_IPersistFile(iface);
328 TRACE("(%p)\n",This);
330 if (This->bDirty)
331 return S_OK;
333 return S_FALSE;
336 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
338 IShellLinkImpl *This = impl_from_IPersistFile(iface);
339 IPersistStream *StreamThis = &This->IPersistStream_iface;
340 HRESULT r;
341 IStream *stm;
343 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
345 if( dwMode == 0 )
346 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
347 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
348 if( SUCCEEDED( r ) )
350 r = IPersistStream_Load(StreamThis, stm);
351 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
352 IStream_Release( stm );
354 /* update file path */
355 HeapFree(GetProcessHeap(), 0, This->filepath);
356 This->filepath = strdupW(pszFileName);
358 This->bDirty = FALSE;
360 TRACE("-- returning hr %08x\n", r);
361 return r;
364 BOOL run_winemenubuilder( const WCHAR *args )
366 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
367 LONG len;
368 LPWSTR buffer;
369 STARTUPINFOW si;
370 PROCESS_INFORMATION pi;
371 BOOL ret;
372 WCHAR app[MAX_PATH];
373 void *redir;
375 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
376 strcatW( app, menubuilder );
378 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
379 buffer = HeapAlloc( GetProcessHeap(), 0, len );
380 if( !buffer )
381 return FALSE;
383 strcpyW( buffer, app );
384 strcatW( buffer, args );
386 TRACE("starting %s\n",debugstr_w(buffer));
388 memset(&si, 0, sizeof(si));
389 si.cb = sizeof(si);
391 Wow64DisableWow64FsRedirection( &redir );
392 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
393 Wow64RevertWow64FsRedirection( redir );
395 HeapFree( GetProcessHeap(), 0, buffer );
397 if (ret)
399 CloseHandle( pi.hProcess );
400 CloseHandle( pi.hThread );
403 return ret;
406 static BOOL StartLinkProcessor( LPCOLESTR szLink )
408 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
409 LONG len;
410 LPWSTR buffer;
411 BOOL ret;
413 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
414 buffer = HeapAlloc( GetProcessHeap(), 0, len );
415 if( !buffer )
416 return FALSE;
418 wsprintfW( buffer, szFormat, szLink );
419 ret = run_winemenubuilder( buffer );
420 HeapFree( GetProcessHeap(), 0, buffer );
421 return ret;
424 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
426 IShellLinkImpl *This = impl_from_IPersistFile(iface);
427 IPersistStream *StreamThis = &This->IPersistStream_iface;
428 HRESULT r;
429 IStream *stm;
431 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
433 if (!pszFileName)
434 return E_FAIL;
436 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
437 if( SUCCEEDED( r ) )
439 r = IPersistStream_Save(StreamThis, stm, FALSE);
440 IStream_Release( stm );
442 if( SUCCEEDED( r ) )
444 StartLinkProcessor( pszFileName );
446 /* update file path */
447 HeapFree(GetProcessHeap(), 0, This->filepath);
448 This->filepath = strdupW(pszFileName);
450 This->bDirty = FALSE;
452 else
454 DeleteFileW( pszFileName );
455 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
459 return r;
462 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
464 IShellLinkImpl *This = impl_from_IPersistFile(iface);
465 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
466 return S_OK;
469 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
471 IShellLinkImpl *This = impl_from_IPersistFile(iface);
472 IMalloc *pMalloc;
474 TRACE("(%p)->(%p)\n", This, filename);
476 if (!This->filepath)
478 *filename = NULL;
479 return S_FALSE;
482 SHGetMalloc(&pMalloc);
483 *filename = IMalloc_Alloc(pMalloc, (strlenW(This->filepath)+1)*sizeof(WCHAR));
484 if (!*filename) return E_OUTOFMEMORY;
486 strcpyW(*filename, This->filepath);
488 return S_OK;
491 static const IPersistFileVtbl pfvt =
493 IPersistFile_fnQueryInterface,
494 IPersistFile_fnAddRef,
495 IPersistFile_fnRelease,
496 IPersistFile_fnGetClassID,
497 IPersistFile_fnIsDirty,
498 IPersistFile_fnLoad,
499 IPersistFile_fnSave,
500 IPersistFile_fnSaveCompleted,
501 IPersistFile_fnGetCurFile
504 /************************************************************************
505 * IPersistStream_QueryInterface
507 static HRESULT WINAPI IPersistStream_fnQueryInterface(
508 IPersistStream* iface,
509 REFIID riid,
510 VOID** ppvObj)
512 IShellLinkImpl *This = impl_from_IPersistStream(iface);
513 return ShellLink_QueryInterface( This, riid, ppvObj );
516 /************************************************************************
517 * IPersistStream_Release
519 static ULONG WINAPI IPersistStream_fnRelease(
520 IPersistStream* iface)
522 IShellLinkImpl *This = impl_from_IPersistStream(iface);
523 return IShellLinkA_Release(&This->IShellLinkA_iface);
526 /************************************************************************
527 * IPersistStream_AddRef
529 static ULONG WINAPI IPersistStream_fnAddRef(
530 IPersistStream* iface)
532 IShellLinkImpl *This = impl_from_IPersistStream(iface);
533 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
536 /************************************************************************
537 * IPersistStream_GetClassID
540 static HRESULT WINAPI IPersistStream_fnGetClassID(
541 IPersistStream* iface,
542 CLSID* pClassID)
544 IShellLinkImpl *This = impl_from_IPersistStream(iface);
545 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
548 /************************************************************************
549 * IPersistStream_IsDirty (IPersistStream)
551 static HRESULT WINAPI IPersistStream_fnIsDirty(
552 IPersistStream* iface)
554 IShellLinkImpl *This = impl_from_IPersistStream(iface);
556 TRACE("(%p)\n", This);
558 return S_OK;
562 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
564 DWORD count;
565 USHORT len;
566 LPVOID temp;
567 LPWSTR str;
568 HRESULT r;
570 TRACE("%p\n", stm);
572 count = 0;
573 r = IStream_Read(stm, &len, sizeof(len), &count);
574 if ( FAILED (r) || ( count != sizeof(len) ) )
575 return E_FAIL;
577 if( unicode )
578 len *= sizeof (WCHAR);
580 TRACE("reading %d\n", len);
581 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
582 if( !temp )
583 return E_OUTOFMEMORY;
584 count = 0;
585 r = IStream_Read(stm, temp, len, &count);
586 if( FAILED (r) || ( count != len ) )
588 HeapFree( GetProcessHeap(), 0, temp );
589 return E_FAIL;
592 TRACE("read %s\n", debugstr_an(temp,len));
594 /* convert to unicode if necessary */
595 if( !unicode )
597 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
598 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
599 if( !str )
601 HeapFree( GetProcessHeap(), 0, temp );
602 return E_OUTOFMEMORY;
604 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
605 HeapFree( GetProcessHeap(), 0, temp );
607 else
609 count /= 2;
610 str = temp;
612 str[count] = 0;
614 *pstr = str;
616 return S_OK;
619 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
621 DWORD size;
622 ULONG count;
623 HRESULT r;
624 struct sized_chunk {
625 DWORD size;
626 unsigned char data[1];
627 } *chunk;
629 TRACE("%p\n",stm);
631 r = IStream_Read( stm, &size, sizeof(size), &count );
632 if( FAILED( r ) || count != sizeof(size) )
633 return E_FAIL;
635 chunk = HeapAlloc( GetProcessHeap(), 0, size );
636 if( !chunk )
637 return E_OUTOFMEMORY;
639 chunk->size = size;
640 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
641 if( FAILED( r ) || count != (size - sizeof(size)) )
643 HeapFree( GetProcessHeap(), 0, chunk );
644 return E_FAIL;
647 TRACE("Read %d bytes\n",chunk->size);
649 *data = chunk;
651 return S_OK;
654 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
656 const int label_sz = sizeof volume->label/sizeof volume->label[0];
657 LPSTR label;
658 int len;
660 volume->serial = vol->dwVolSerial;
661 volume->type = vol->dwType;
663 if( !vol->dwVolLabelOfs )
664 return FALSE;
665 if( vol->dwSize <= vol->dwVolLabelOfs )
666 return FALSE;
667 len = vol->dwSize - vol->dwVolLabelOfs;
669 label = (LPSTR) vol;
670 label += vol->dwVolLabelOfs;
671 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
673 return TRUE;
676 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
678 int len = 0, wlen;
679 LPWSTR path;
681 while( p[len] && (len < maxlen) )
682 len++;
684 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
685 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
686 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
687 path[wlen] = 0;
689 return path;
692 static HRESULT Stream_LoadLocation( IStream *stm,
693 volume_info *volume, LPWSTR *path )
695 char *p = NULL;
696 LOCATION_INFO *loc;
697 HRESULT r;
698 DWORD n;
700 r = Stream_ReadChunk( stm, (LPVOID*) &p );
701 if( FAILED(r) )
702 return r;
704 loc = (LOCATION_INFO*) p;
705 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
707 HeapFree( GetProcessHeap(), 0, p );
708 return E_FAIL;
711 /* if there's valid local volume information, load it */
712 if( loc->dwVolTableOfs &&
713 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
715 LOCAL_VOLUME_INFO *volume_info;
717 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
718 Stream_LoadVolume( volume_info, volume );
721 /* if there's a local path, load it */
722 n = loc->dwLocalPathOfs;
723 if( n && (n < loc->dwTotalSize) )
724 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
726 TRACE("type %d serial %08x name %s path %s\n", volume->type,
727 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
729 HeapFree( GetProcessHeap(), 0, p );
730 return S_OK;
734 * The format of the advertised shortcut info seems to be:
736 * Offset Description
737 * ------ -----------
739 * 0 Length of the block (4 bytes, usually 0x314)
740 * 4 tag (dword)
741 * 8 string data in ASCII
742 * 8+0x104 string data in UNICODE
744 * In the original Win32 implementation the buffers are not initialized
745 * to zero, so data trailing the string is random garbage.
747 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
749 DWORD size;
750 ULONG count;
751 HRESULT r;
752 EXP_DARWIN_LINK buffer;
754 TRACE("%p\n",stm);
756 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
757 if( FAILED( r ) )
758 return r;
760 /* make sure that we read the size of the structure even on error */
761 size = sizeof buffer - sizeof (DWORD);
762 if( buffer.dbh.cbSize != sizeof buffer )
764 ERR("Ooops. This structure is not as expected...\n");
765 return E_FAIL;
768 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
769 if( FAILED( r ) )
770 return r;
772 if( count != size )
773 return E_FAIL;
775 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
777 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
779 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
780 return E_FAIL;
783 *str = HeapAlloc( GetProcessHeap(), 0,
784 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
785 lstrcpyW( *str, buffer.szwDarwinID );
787 return S_OK;
790 /************************************************************************
791 * IPersistStream_Load (IPersistStream)
793 static HRESULT WINAPI IPersistStream_fnLoad(
794 IPersistStream* iface,
795 IStream* stm)
797 LINK_HEADER hdr;
798 ULONG dwBytesRead;
799 BOOL unicode;
800 HRESULT r;
801 DWORD zero;
803 IShellLinkImpl *This = impl_from_IPersistStream(iface);
805 TRACE("%p %p\n", This, stm);
807 if( !stm )
808 return STG_E_INVALIDPOINTER;
810 dwBytesRead = 0;
811 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
812 if( FAILED( r ) )
813 return r;
815 if( dwBytesRead != sizeof(hdr))
816 return E_FAIL;
817 if( hdr.dwSize != sizeof(hdr))
818 return E_FAIL;
819 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
820 return E_FAIL;
822 /* free all the old stuff */
823 ILFree(This->pPidl);
824 This->pPidl = NULL;
825 memset( &This->volume, 0, sizeof This->volume );
826 HeapFree(GetProcessHeap(), 0, This->sPath);
827 This->sPath = NULL;
828 HeapFree(GetProcessHeap(), 0, This->sDescription);
829 This->sDescription = NULL;
830 HeapFree(GetProcessHeap(), 0, This->sPathRel);
831 This->sPathRel = NULL;
832 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
833 This->sWorkDir = NULL;
834 HeapFree(GetProcessHeap(), 0, This->sArgs);
835 This->sArgs = NULL;
836 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
837 This->sIcoPath = NULL;
838 HeapFree(GetProcessHeap(), 0, This->sProduct);
839 This->sProduct = NULL;
840 HeapFree(GetProcessHeap(), 0, This->sComponent);
841 This->sComponent = NULL;
843 This->wHotKey = (WORD)hdr.wHotKey;
844 This->iIcoNdx = hdr.nIcon;
845 FileTimeToSystemTime (&hdr.Time1, &This->time1);
846 FileTimeToSystemTime (&hdr.Time2, &This->time2);
847 FileTimeToSystemTime (&hdr.Time3, &This->time3);
848 if (TRACE_ON(shell))
850 WCHAR sTemp[MAX_PATH];
851 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
852 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
853 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
854 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
855 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
856 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
857 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
858 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
859 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
862 /* load all the new stuff */
863 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
865 r = ILLoadFromStream( stm, &This->pPidl );
866 if( FAILED( r ) )
867 return r;
869 pdump(This->pPidl);
871 /* load the location information */
872 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
873 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
874 if( FAILED( r ) )
875 goto end;
877 unicode = hdr.dwFlags & SLDF_UNICODE;
878 if( hdr.dwFlags & SLDF_HAS_NAME )
880 r = Stream_LoadString( stm, unicode, &This->sDescription );
881 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
883 if( FAILED( r ) )
884 goto end;
886 if( hdr.dwFlags & SLDF_HAS_RELPATH )
888 r = Stream_LoadString( stm, unicode, &This->sPathRel );
889 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
891 if( FAILED( r ) )
892 goto end;
894 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
896 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
897 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
899 if( FAILED( r ) )
900 goto end;
902 if( hdr.dwFlags & SLDF_HAS_ARGS )
904 r = Stream_LoadString( stm, unicode, &This->sArgs );
905 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
907 if( FAILED( r ) )
908 goto end;
910 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
912 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
913 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
915 if( FAILED( r ) )
916 goto end;
918 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
920 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
921 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
923 if( FAILED( r ) )
924 goto end;
926 if( hdr.dwFlags & SLDF_HAS_DARWINID )
928 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
929 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
931 if( FAILED( r ) )
932 goto end;
934 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
935 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
937 /* Some lnk files have extra data blocks starting with a
938 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
939 * one with a 0xa0000003 signature. However these don't seem to matter
940 * too much.
942 WARN("Last word was not zero\n");
945 TRACE("OK\n");
947 pdump (This->pPidl);
949 return S_OK;
950 end:
951 return r;
954 /************************************************************************
955 * Stream_WriteString
957 * Helper function for IPersistStream_Save. Writes a unicode string
958 * with terminating nul byte to a stream, preceded by the its length.
960 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
962 USHORT len = lstrlenW( str ) + 1;
963 DWORD count;
964 HRESULT r;
966 r = IStream_Write( stm, &len, sizeof(len), &count );
967 if( FAILED( r ) )
968 return r;
970 len *= sizeof(WCHAR);
972 r = IStream_Write( stm, str, len, &count );
973 if( FAILED( r ) )
974 return r;
976 return S_OK;
979 /************************************************************************
980 * Stream_WriteLocationInfo
982 * Writes the location info to a stream
984 * FIXME: One day we might want to write the network volume information
985 * and the final path.
986 * Figure out how Windows deals with unicode paths here.
988 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
989 volume_info *volume )
991 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
992 LOCAL_VOLUME_INFO *vol;
993 LOCATION_INFO *loc;
994 LPSTR szLabel, szPath, szFinalPath;
995 ULONG count = 0;
996 HRESULT hr;
998 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
1000 /* figure out the size of everything */
1001 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1002 NULL, 0, NULL, NULL );
1003 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
1004 NULL, 0, NULL, NULL );
1005 volume_info_size = sizeof *vol + label_size;
1006 final_path_size = 1;
1007 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
1009 /* create pointers to everything */
1010 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
1011 vol = (LOCAL_VOLUME_INFO*) &loc[1];
1012 szLabel = (LPSTR) &vol[1];
1013 szPath = &szLabel[label_size];
1014 szFinalPath = &szPath[path_size];
1016 /* fill in the location information header */
1017 loc->dwTotalSize = total_size;
1018 loc->dwHeaderSize = sizeof (*loc);
1019 loc->dwFlags = 1;
1020 loc->dwVolTableOfs = sizeof (*loc);
1021 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1022 loc->dwNetworkVolTableOfs = 0;
1023 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1025 /* fill in the volume information */
1026 vol->dwSize = volume_info_size;
1027 vol->dwType = volume->type;
1028 vol->dwVolSerial = volume->serial;
1029 vol->dwVolLabelOfs = sizeof (*vol);
1031 /* copy in the strings */
1032 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1033 szLabel, label_size, NULL, NULL );
1034 WideCharToMultiByte( CP_ACP, 0, path, -1,
1035 szPath, path_size, NULL, NULL );
1036 szFinalPath[0] = 0;
1038 hr = IStream_Write( stm, loc, total_size, &count );
1039 HeapFree(GetProcessHeap(), 0, loc);
1041 return hr;
1044 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1046 EXP_DARWIN_LINK *buffer;
1048 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1049 buffer->dbh.cbSize = sizeof *buffer;
1050 buffer->dbh.dwSignature = magic;
1051 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1052 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1054 return buffer;
1057 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1059 EXP_DARWIN_LINK *buffer;
1060 ULONG count;
1062 TRACE("%p\n",stm);
1064 buffer = shelllink_build_darwinid( string, magic );
1066 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1069 /************************************************************************
1070 * IPersistStream_Save (IPersistStream)
1072 * FIXME: makes assumptions about byte order
1074 static HRESULT WINAPI IPersistStream_fnSave(
1075 IPersistStream* iface,
1076 IStream* stm,
1077 BOOL fClearDirty)
1079 LINK_HEADER header;
1080 ULONG count;
1081 DWORD zero;
1082 HRESULT r;
1084 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1086 TRACE("%p %p %x\n", This, stm, fClearDirty);
1088 memset(&header, 0, sizeof(header));
1089 header.dwSize = sizeof(header);
1090 header.fStartup = This->iShowCmd;
1091 header.MagicGuid = CLSID_ShellLink;
1093 header.wHotKey = This->wHotKey;
1094 header.nIcon = This->iIcoNdx;
1095 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1096 if( This->pPidl )
1097 header.dwFlags |= SLDF_HAS_ID_LIST;
1098 if( This->sPath )
1099 header.dwFlags |= SLDF_HAS_LINK_INFO;
1100 if( This->sDescription )
1101 header.dwFlags |= SLDF_HAS_NAME;
1102 if( This->sWorkDir )
1103 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1104 if( This->sArgs )
1105 header.dwFlags |= SLDF_HAS_ARGS;
1106 if( This->sIcoPath )
1107 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1108 if( This->sProduct )
1109 header.dwFlags |= SLDF_HAS_LOGO3ID;
1110 if( This->sComponent )
1111 header.dwFlags |= SLDF_HAS_DARWINID;
1113 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1114 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1115 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1117 /* write the Shortcut header */
1118 r = IStream_Write( stm, &header, sizeof(header), &count );
1119 if( FAILED( r ) )
1121 ERR("Write failed at %d\n",__LINE__);
1122 return r;
1125 TRACE("Writing pidl\n");
1127 /* write the PIDL to the shortcut */
1128 if( This->pPidl )
1130 r = ILSaveToStream( stm, This->pPidl );
1131 if( FAILED( r ) )
1133 ERR("Failed to write PIDL at %d\n",__LINE__);
1134 return r;
1138 if( This->sPath )
1139 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1141 if( This->sDescription )
1142 r = Stream_WriteString( stm, This->sDescription );
1144 if( This->sPathRel )
1145 r = Stream_WriteString( stm, This->sPathRel );
1147 if( This->sWorkDir )
1148 r = Stream_WriteString( stm, This->sWorkDir );
1150 if( This->sArgs )
1151 r = Stream_WriteString( stm, This->sArgs );
1153 if( This->sIcoPath )
1154 r = Stream_WriteString( stm, This->sIcoPath );
1156 if( This->sProduct )
1157 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1159 if( This->sComponent )
1160 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1162 /* the last field is a single zero dword */
1163 zero = 0;
1164 r = IStream_Write( stm, &zero, sizeof zero, &count );
1166 return S_OK;
1169 /************************************************************************
1170 * IPersistStream_GetSizeMax (IPersistStream)
1172 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1173 IPersistStream* iface,
1174 ULARGE_INTEGER* pcbSize)
1176 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1178 TRACE("(%p)\n", This);
1180 return E_NOTIMPL;
1183 static const IPersistStreamVtbl psvt =
1185 IPersistStream_fnQueryInterface,
1186 IPersistStream_fnAddRef,
1187 IPersistStream_fnRelease,
1188 IPersistStream_fnGetClassID,
1189 IPersistStream_fnIsDirty,
1190 IPersistStream_fnLoad,
1191 IPersistStream_fnSave,
1192 IPersistStream_fnGetSizeMax
1195 /**************************************************************************
1196 * IShellLink_Constructor
1198 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1199 REFIID riid, LPVOID *ppv )
1201 IShellLinkImpl * sl;
1202 HRESULT r;
1204 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1206 *ppv = NULL;
1208 if (pUnkOuter)
1209 return CLASS_E_NOAGGREGATION;
1210 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1211 if (!sl)
1212 return E_OUTOFMEMORY;
1214 sl->ref = 1;
1215 sl->IShellLinkA_iface.lpVtbl = &slvt;
1216 sl->IShellLinkW_iface.lpVtbl = &slvtw;
1217 sl->IPersistFile_iface.lpVtbl = &pfvt;
1218 sl->IPersistStream_iface.lpVtbl = &psvt;
1219 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
1220 sl->IShellExtInit_iface.lpVtbl = &eivt;
1221 sl->IContextMenu_iface.lpVtbl = &cmvt;
1222 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
1223 sl->iShowCmd = SW_SHOWNORMAL;
1224 sl->bDirty = FALSE;
1225 sl->iIdOpen = -1;
1226 sl->site = NULL;
1227 sl->filepath = NULL;
1229 TRACE("(%p)->()\n",sl);
1231 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, ppv );
1232 IShellLinkW_Release( &sl->IShellLinkW_iface );
1233 return r;
1237 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1239 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1240 return FALSE;
1241 return TRUE;
1244 /**************************************************************************
1245 * ShellLink_UpdatePath
1246 * update absolute path in sPath using relative path in sPathRel
1248 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1250 if (!path || !psPath)
1251 return E_INVALIDARG;
1253 if (!*psPath && sPathRel) {
1254 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1255 LPWSTR final = NULL;
1257 /* first try if [directory of link file] + [relative path] finds an existing file */
1259 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1260 if( !final )
1261 final = buffer;
1262 lstrcpyW(final, sPathRel);
1264 *abs_path = '\0';
1266 if (SHELL_ExistsFileW(buffer)) {
1267 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1268 lstrcpyW(abs_path, buffer);
1269 } else {
1270 /* try if [working directory] + [relative path] finds an existing file */
1271 if (sWorkDir) {
1272 lstrcpyW(buffer, sWorkDir);
1273 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1275 if (SHELL_ExistsFileW(buffer))
1276 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1277 lstrcpyW(abs_path, buffer);
1281 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1282 if (!*abs_path)
1283 lstrcpyW(abs_path, sPathRel);
1285 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1286 if (!*psPath)
1287 return E_OUTOFMEMORY;
1289 lstrcpyW(*psPath, abs_path);
1292 return S_OK;
1295 /**************************************************************************
1296 * IShellLink_ConstructFromFile
1298 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1299 LPCITEMIDLIST pidl, IUnknown **ppv)
1301 IShellLinkW* psl;
1303 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1305 if (SUCCEEDED(hr)) {
1306 IPersistFile* ppf;
1308 *ppv = NULL;
1310 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1312 if (SUCCEEDED(hr)) {
1313 WCHAR path[MAX_PATH];
1315 if (SHGetPathFromIDListW(pidl, path))
1316 hr = IPersistFile_Load(ppf, path, 0);
1317 else
1318 hr = E_FAIL;
1320 if (SUCCEEDED(hr))
1321 *ppv = (IUnknown*)psl;
1323 IPersistFile_Release(ppf);
1326 if (!*ppv)
1327 IShellLinkW_Release(psl);
1330 return hr;
1333 /**************************************************************************
1334 * IShellLinkA_QueryInterface
1336 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1338 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1339 return ShellLink_QueryInterface( This, riid, ppvObj );
1342 /******************************************************************************
1343 * IShellLinkA_AddRef
1345 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1347 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1348 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1351 /******************************************************************************
1352 * IShellLinkA_Release
1354 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1356 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1357 return IShellLinkW_Release(&This->IShellLinkW_iface);
1360 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1361 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1363 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1365 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1366 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1368 if (This->sComponent || This->sProduct)
1369 return S_FALSE;
1371 if (cchMaxPath)
1372 pszFile[0] = 0;
1373 if (This->sPath)
1374 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1375 pszFile, cchMaxPath, NULL, NULL);
1377 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1379 return S_OK;
1382 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1384 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1385 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1388 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1390 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1391 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1394 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1395 INT cchMaxName)
1397 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1399 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1401 if( cchMaxName )
1402 pszName[0] = 0;
1403 if( This->sDescription )
1404 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1405 pszName, cchMaxName, NULL, NULL);
1407 return S_OK;
1410 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1412 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1413 WCHAR *descrW;
1414 HRESULT hr;
1416 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1418 if (pszName)
1420 descrW = heap_strdupAtoW(pszName);
1421 if (!descrW) return E_OUTOFMEMORY;
1423 else
1424 descrW = NULL;
1426 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1427 HeapFree(GetProcessHeap(), 0, descrW);
1429 return hr;
1432 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1433 INT cchMaxPath)
1435 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1437 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1439 if( cchMaxPath )
1440 pszDir[0] = 0;
1441 if( This->sWorkDir )
1442 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1443 pszDir, cchMaxPath, NULL, NULL);
1445 return S_OK;
1448 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1450 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1451 WCHAR *dirW;
1452 HRESULT hr;
1454 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1456 dirW = heap_strdupAtoW(pszDir);
1457 if (!dirW) return E_OUTOFMEMORY;
1459 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1460 HeapFree(GetProcessHeap(), 0, dirW);
1462 return hr;
1465 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1467 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1469 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1471 if( cchMaxPath )
1472 pszArgs[0] = 0;
1473 if( This->sArgs )
1474 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1475 pszArgs, cchMaxPath, NULL, NULL);
1477 return S_OK;
1480 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1482 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1483 WCHAR *argsW;
1484 HRESULT hr;
1486 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1488 if (pszArgs)
1490 argsW = heap_strdupAtoW(pszArgs);
1491 if (!argsW) return E_OUTOFMEMORY;
1493 else
1494 argsW = NULL;
1496 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1497 HeapFree(GetProcessHeap(), 0, argsW);
1499 return hr;
1502 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1504 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1505 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1508 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1510 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1511 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1514 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1516 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1517 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1520 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1522 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1523 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1526 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1527 INT cchIconPath, INT *piIcon)
1529 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1531 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1533 *piIcon = This->iIcoNdx;
1535 if (This->sIcoPath)
1536 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1537 else
1538 pszIconPath[0] = 0;
1540 return S_OK;
1543 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR pszIconPath,
1544 INT iIcon)
1546 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1547 WCHAR *pathW;
1548 HRESULT hr;
1550 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1552 pathW = heap_strdupAtoW(pszIconPath);
1553 if (!pathW) return E_OUTOFMEMORY;
1555 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, pathW, iIcon);
1556 HeapFree(GetProcessHeap(), 0, pathW);
1558 return hr;
1561 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1562 DWORD dwReserved)
1564 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1565 WCHAR *pathW;
1566 HRESULT hr;
1568 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1570 pathW = heap_strdupAtoW(pszPathRel);
1571 if (!pathW) return E_OUTOFMEMORY;
1573 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1574 HeapFree(GetProcessHeap(), 0, pathW);
1576 return hr;
1579 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1581 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1583 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1585 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1588 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1590 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1591 HRESULT r;
1592 LPWSTR str;
1594 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1596 if (!pszFile) return E_INVALIDARG;
1598 str = heap_strdupAtoW(pszFile);
1599 if( !str )
1600 return E_OUTOFMEMORY;
1602 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1603 HeapFree( GetProcessHeap(), 0, str );
1605 return r;
1608 /**************************************************************************
1609 * IShellLink Implementation
1612 static const IShellLinkAVtbl slvt =
1614 IShellLinkA_fnQueryInterface,
1615 IShellLinkA_fnAddRef,
1616 IShellLinkA_fnRelease,
1617 IShellLinkA_fnGetPath,
1618 IShellLinkA_fnGetIDList,
1619 IShellLinkA_fnSetIDList,
1620 IShellLinkA_fnGetDescription,
1621 IShellLinkA_fnSetDescription,
1622 IShellLinkA_fnGetWorkingDirectory,
1623 IShellLinkA_fnSetWorkingDirectory,
1624 IShellLinkA_fnGetArguments,
1625 IShellLinkA_fnSetArguments,
1626 IShellLinkA_fnGetHotkey,
1627 IShellLinkA_fnSetHotkey,
1628 IShellLinkA_fnGetShowCmd,
1629 IShellLinkA_fnSetShowCmd,
1630 IShellLinkA_fnGetIconLocation,
1631 IShellLinkA_fnSetIconLocation,
1632 IShellLinkA_fnSetRelativePath,
1633 IShellLinkA_fnResolve,
1634 IShellLinkA_fnSetPath
1638 /**************************************************************************
1639 * IShellLinkW_fnQueryInterface
1641 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1642 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1644 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1645 return ShellLink_QueryInterface( This, riid, ppvObj );
1648 /******************************************************************************
1649 * IShellLinkW_fnAddRef
1651 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1653 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1654 ULONG ref = InterlockedIncrement(&This->ref);
1656 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1658 return ref;
1661 /******************************************************************************
1662 * IShellLinkW_fnRelease
1664 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1666 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1667 ULONG refCount = InterlockedDecrement(&This->ref);
1669 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1671 if (refCount)
1672 return refCount;
1674 TRACE("-- destroying IShellLink(%p)\n",This);
1676 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1677 HeapFree(GetProcessHeap(), 0, This->sArgs);
1678 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1679 HeapFree(GetProcessHeap(), 0, This->sDescription);
1680 HeapFree(GetProcessHeap(), 0, This->sPath);
1681 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1682 HeapFree(GetProcessHeap(), 0, This->sProduct);
1683 HeapFree(GetProcessHeap(), 0, This->sComponent);
1684 HeapFree(GetProcessHeap(), 0, This->filepath);
1686 if (This->site)
1687 IUnknown_Release( This->site );
1689 if (This->pPidl)
1690 ILFree(This->pPidl);
1692 LocalFree(This);
1694 return 0;
1697 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1699 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1701 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1702 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1704 if (This->sComponent || This->sProduct)
1705 return S_FALSE;
1707 if (cchMaxPath)
1708 pszFile[0] = 0;
1709 if (This->sPath)
1710 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1712 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1714 return S_OK;
1717 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1719 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1721 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1723 if (!This->pPidl)
1725 *ppidl = NULL;
1726 return S_FALSE;
1728 *ppidl = ILClone(This->pPidl);
1729 return S_OK;
1732 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1734 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1736 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1738 if( This->pPidl )
1739 ILFree( This->pPidl );
1740 This->pPidl = ILClone( pidl );
1741 if( !This->pPidl )
1742 return E_FAIL;
1744 This->bDirty = TRUE;
1746 return S_OK;
1749 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1751 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1753 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1755 pszName[0] = 0;
1756 if( This->sDescription )
1757 lstrcpynW( pszName, This->sDescription, cchMaxName );
1759 return S_OK;
1762 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1764 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1766 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1768 HeapFree(GetProcessHeap(), 0, This->sDescription);
1769 if (pszName)
1771 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1772 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1773 if ( !This->sDescription )
1774 return E_OUTOFMEMORY;
1776 lstrcpyW( This->sDescription, pszName );
1778 else
1779 This->sDescription = NULL;
1780 This->bDirty = TRUE;
1782 return S_OK;
1785 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1787 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1789 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1791 if( cchMaxPath )
1792 pszDir[0] = 0;
1793 if( This->sWorkDir )
1794 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1796 return S_OK;
1799 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1801 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1803 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1805 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1806 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1807 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1808 if ( !This->sWorkDir )
1809 return E_OUTOFMEMORY;
1810 lstrcpyW( This->sWorkDir, pszDir );
1811 This->bDirty = TRUE;
1813 return S_OK;
1816 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1818 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1820 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1822 if( cchMaxPath )
1823 pszArgs[0] = 0;
1824 if( This->sArgs )
1825 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1827 return S_OK;
1830 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1832 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1834 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1836 HeapFree(GetProcessHeap(), 0, This->sArgs);
1837 if (pszArgs)
1839 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1840 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1841 if ( !This->sArgs )
1842 return E_OUTOFMEMORY;
1843 lstrcpyW( This->sArgs, pszArgs );
1845 else This->sArgs = NULL;
1847 This->bDirty = TRUE;
1849 return S_OK;
1852 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1854 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1856 TRACE("(%p)->(%p)\n",This, pwHotkey);
1858 *pwHotkey=This->wHotKey;
1860 return S_OK;
1863 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1865 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1867 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1869 This->wHotKey = wHotkey;
1870 This->bDirty = TRUE;
1872 return S_OK;
1875 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1877 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1879 TRACE("(%p)->(%p)\n",This, piShowCmd);
1881 *piShowCmd = This->iShowCmd;
1883 return S_OK;
1886 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1888 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1890 TRACE("(%p)->(%d)\n", This, iShowCmd);
1892 This->iShowCmd = iShowCmd;
1893 This->bDirty = TRUE;
1895 return S_OK;
1898 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1900 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1902 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1904 *piIcon = This->iIcoNdx;
1906 if (This->sIcoPath)
1907 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1908 else
1909 pszIconPath[0] = 0;
1911 return S_OK;
1914 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1916 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1918 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1920 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1921 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1922 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1923 if ( !This->sIcoPath )
1924 return E_OUTOFMEMORY;
1925 lstrcpyW( This->sIcoPath, pszIconPath );
1927 This->iIcoNdx = iIcon;
1928 This->bDirty = TRUE;
1930 return S_OK;
1933 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1935 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1937 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1939 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1940 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1941 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1942 if ( !This->sPathRel )
1943 return E_OUTOFMEMORY;
1944 lstrcpyW( This->sPathRel, pszPathRel );
1945 This->bDirty = TRUE;
1947 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1950 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1952 HRESULT hr = S_OK;
1953 BOOL bSuccess;
1955 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1957 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1959 /*FIXME: use IResolveShellLink interface */
1961 if (!This->sPath && This->pPidl) {
1962 WCHAR buffer[MAX_PATH];
1964 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1966 if (bSuccess && *buffer) {
1967 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1968 if (!This->sPath)
1969 return E_OUTOFMEMORY;
1971 lstrcpyW(This->sPath, buffer);
1973 This->bDirty = TRUE;
1974 } else
1975 hr = S_OK; /* don't report an error occurred while just caching information */
1978 if (!This->sIcoPath && This->sPath) {
1979 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1980 if (!This->sIcoPath)
1981 return E_OUTOFMEMORY;
1983 lstrcpyW(This->sIcoPath, This->sPath);
1984 This->iIcoNdx = 0;
1986 This->bDirty = TRUE;
1989 return hr;
1992 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
1994 LPWSTR ret;
1995 LPCWSTR p;
1996 DWORD len;
1998 if( !str )
1999 return NULL;
2001 p = strchrW( str, ':' );
2002 if( !p )
2003 return NULL;
2004 len = p - str;
2005 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2006 if( !ret )
2007 return ret;
2008 memcpy( ret, str, sizeof(WCHAR)*len );
2009 ret[len] = 0;
2010 return ret;
2013 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2015 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2016 WCHAR szGuid[39];
2017 HRESULT r;
2018 GUID guid;
2019 int len;
2021 while( str[0] )
2023 /* each segment must start with two colons */
2024 if( str[0] != ':' || str[1] != ':' )
2025 return E_FAIL;
2027 /* the last segment is just two colons */
2028 if( !str[2] )
2029 break;
2030 str += 2;
2032 /* there must be a colon straight after a guid */
2033 p = strchrW( str, ':' );
2034 if( !p )
2035 return E_FAIL;
2036 len = p - str;
2037 if( len != 38 )
2038 return E_FAIL;
2040 /* get the guid, and check it's validly formatted */
2041 memcpy( szGuid, str, sizeof(WCHAR)*len );
2042 szGuid[len] = 0;
2043 r = CLSIDFromString( szGuid, &guid );
2044 if( r != S_OK )
2045 return r;
2046 str = p + 1;
2048 /* match it up to a guid that we care about */
2049 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2050 szComponent = str;
2051 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2052 szProduct = str;
2053 else
2054 return E_FAIL;
2056 /* skip to the next field */
2057 str = strchrW( str, ':' );
2058 if( !str )
2059 return E_FAIL;
2062 /* we have to have a component for an advertised shortcut */
2063 if( !szComponent )
2064 return E_FAIL;
2066 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2067 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2069 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2070 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2072 return S_OK;
2075 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2077 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2078 WCHAR drive[] = { path[0], ':', '\\', 0 };
2079 BOOL r;
2081 volume->type = GetDriveTypeW(drive);
2082 r = GetVolumeInformationW(drive, volume->label, label_sz,
2083 &volume->serial, NULL, NULL, NULL, 0);
2084 TRACE("r = %d type %d serial %08x name %s\n", r,
2085 volume->type, volume->serial, debugstr_w(volume->label));
2086 return r;
2089 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2091 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2092 WCHAR buffer[MAX_PATH];
2093 LPWSTR fname, unquoted = NULL;
2094 HRESULT hr = S_OK;
2095 UINT len;
2097 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2099 if (!pszFile) return E_INVALIDARG;
2101 /* quotes at the ends of the string are stripped */
2102 len = lstrlenW(pszFile);
2103 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2105 unquoted = strdupW(pszFile);
2106 PathUnquoteSpacesW(unquoted);
2107 pszFile = unquoted;
2110 /* any other quote marks are invalid */
2111 if (strchrW(pszFile, '"'))
2113 HeapFree(GetProcessHeap(), 0, unquoted);
2114 return S_FALSE;
2117 HeapFree(GetProcessHeap(), 0, This->sPath);
2118 This->sPath = NULL;
2120 HeapFree(GetProcessHeap(), 0, This->sComponent);
2121 This->sComponent = NULL;
2123 if (This->pPidl)
2124 ILFree(This->pPidl);
2125 This->pPidl = NULL;
2127 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2129 if (*pszFile == '\0')
2130 *buffer = '\0';
2131 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2132 return E_FAIL;
2133 else if(!PathFileExistsW(buffer) &&
2134 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2135 hr = S_FALSE;
2137 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2138 ShellLink_GetVolumeInfo(buffer, &This->volume);
2140 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2141 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2142 if (!This->sPath)
2144 HeapFree(GetProcessHeap(), 0, unquoted);
2145 return E_OUTOFMEMORY;
2148 lstrcpyW(This->sPath, buffer);
2150 This->bDirty = TRUE;
2151 HeapFree(GetProcessHeap(), 0, unquoted);
2153 return hr;
2156 /**************************************************************************
2157 * IShellLinkW Implementation
2160 static const IShellLinkWVtbl slvtw =
2162 IShellLinkW_fnQueryInterface,
2163 IShellLinkW_fnAddRef,
2164 IShellLinkW_fnRelease,
2165 IShellLinkW_fnGetPath,
2166 IShellLinkW_fnGetIDList,
2167 IShellLinkW_fnSetIDList,
2168 IShellLinkW_fnGetDescription,
2169 IShellLinkW_fnSetDescription,
2170 IShellLinkW_fnGetWorkingDirectory,
2171 IShellLinkW_fnSetWorkingDirectory,
2172 IShellLinkW_fnGetArguments,
2173 IShellLinkW_fnSetArguments,
2174 IShellLinkW_fnGetHotkey,
2175 IShellLinkW_fnSetHotkey,
2176 IShellLinkW_fnGetShowCmd,
2177 IShellLinkW_fnSetShowCmd,
2178 IShellLinkW_fnGetIconLocation,
2179 IShellLinkW_fnSetIconLocation,
2180 IShellLinkW_fnSetRelativePath,
2181 IShellLinkW_fnResolve,
2182 IShellLinkW_fnSetPath
2185 static HRESULT WINAPI
2186 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2188 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2189 return IShellLinkA_QueryInterface(&This->IShellLinkA_iface, riid, ppvObject);
2192 static ULONG WINAPI
2193 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2195 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2196 return IShellLinkA_AddRef(&This->IShellLinkA_iface);
2199 static ULONG WINAPI
2200 ShellLink_DataList_Release( IShellLinkDataList* iface )
2202 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2203 return IShellLinkW_Release(&This->IShellLinkW_iface);
2206 static HRESULT WINAPI
2207 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2209 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2210 return E_NOTIMPL;
2213 static HRESULT WINAPI
2214 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2216 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2217 LPVOID block = NULL;
2218 HRESULT r = E_FAIL;
2220 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2222 switch (dwSig)
2224 case EXP_DARWIN_ID_SIG:
2225 if (!This->sComponent)
2226 break;
2227 block = shelllink_build_darwinid( This->sComponent, dwSig );
2228 r = S_OK;
2229 break;
2230 case EXP_SZ_LINK_SIG:
2231 case NT_CONSOLE_PROPS_SIG:
2232 case NT_FE_CONSOLE_PROPS_SIG:
2233 case EXP_SPECIAL_FOLDER_SIG:
2234 case EXP_SZ_ICON_SIG:
2235 FIXME("valid but unhandled datablock %08x\n", dwSig);
2236 break;
2237 default:
2238 ERR("unknown datablock %08x\n", dwSig);
2240 *ppDataBlock = block;
2241 return r;
2244 static HRESULT WINAPI
2245 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2247 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2248 return E_NOTIMPL;
2251 static HRESULT WINAPI
2252 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2254 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2255 DWORD flags = 0;
2257 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2259 /* FIXME: add more */
2260 if (This->sArgs)
2261 flags |= SLDF_HAS_ARGS;
2262 if (This->sComponent)
2263 flags |= SLDF_HAS_DARWINID;
2264 if (This->sIcoPath)
2265 flags |= SLDF_HAS_ICONLOCATION;
2266 if (This->sProduct)
2267 flags |= SLDF_HAS_LOGO3ID;
2268 if (This->pPidl)
2269 flags |= SLDF_HAS_ID_LIST;
2271 *pdwFlags = flags;
2273 return S_OK;
2276 static HRESULT WINAPI
2277 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2279 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2280 return E_NOTIMPL;
2283 static const IShellLinkDataListVtbl dlvt =
2285 ShellLink_DataList_QueryInterface,
2286 ShellLink_DataList_AddRef,
2287 ShellLink_DataList_Release,
2288 ShellLink_AddDataBlock,
2289 ShellLink_CopyDataBlock,
2290 ShellLink_RemoveDataBlock,
2291 ShellLink_GetFlags,
2292 ShellLink_SetFlags
2295 static HRESULT WINAPI
2296 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2298 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2299 return IShellLinkA_QueryInterface(&This->IShellLinkA_iface, riid, ppvObject);
2302 static ULONG WINAPI
2303 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2305 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2306 return IShellLinkA_AddRef(&This->IShellLinkA_iface);
2309 static ULONG WINAPI
2310 ShellLink_ExtInit_Release( IShellExtInit* iface )
2312 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2313 return IShellLinkW_Release(&This->IShellLinkW_iface);
2316 /**************************************************************************
2317 * ShellLink implementation of IShellExtInit::Initialize()
2319 * Loads the shelllink from the dataobject the shell is pointing to.
2321 static HRESULT WINAPI
2322 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2323 IDataObject *pdtobj, HKEY hkeyProgID )
2325 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2326 FORMATETC format;
2327 STGMEDIUM stgm;
2328 UINT count;
2329 HRESULT r = E_FAIL;
2331 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2333 if( !pdtobj )
2334 return r;
2336 format.cfFormat = CF_HDROP;
2337 format.ptd = NULL;
2338 format.dwAspect = DVASPECT_CONTENT;
2339 format.lindex = -1;
2340 format.tymed = TYMED_HGLOBAL;
2342 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2343 return r;
2345 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2346 if( count == 1 )
2348 LPWSTR path;
2350 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2351 count++;
2352 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2353 if( path )
2355 IPersistFile *pf = &This->IPersistFile_iface;
2357 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2358 r = IPersistFile_Load( pf, path, 0 );
2359 HeapFree( GetProcessHeap(), 0, path );
2362 ReleaseStgMedium( &stgm );
2364 return r;
2367 static const IShellExtInitVtbl eivt =
2369 ShellLink_ExtInit_QueryInterface,
2370 ShellLink_ExtInit_AddRef,
2371 ShellLink_ExtInit_Release,
2372 ShellLink_ExtInit_Initialize
2375 static HRESULT WINAPI
2376 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2378 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2379 return IShellLinkA_QueryInterface(&This->IShellLinkA_iface, riid, ppvObject);
2382 static ULONG WINAPI
2383 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2385 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2386 return IShellLinkA_AddRef(&This->IShellLinkA_iface);
2389 static ULONG WINAPI
2390 ShellLink_ContextMenu_Release( IContextMenu* iface )
2392 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2393 return IShellLinkW_Release(&This->IShellLinkW_iface);
2396 static HRESULT WINAPI
2397 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2398 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2400 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2401 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2402 MENUITEMINFOW mii;
2403 int id = 1;
2405 TRACE("%p %p %u %u %u %u\n", This,
2406 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2408 if ( !hmenu )
2409 return E_INVALIDARG;
2411 memset( &mii, 0, sizeof mii );
2412 mii.cbSize = sizeof mii;
2413 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2414 mii.dwTypeData = szOpen;
2415 mii.cch = strlenW( mii.dwTypeData );
2416 mii.wID = idCmdFirst + id++;
2417 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2418 mii.fType = MFT_STRING;
2419 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2420 return E_FAIL;
2421 This->iIdOpen = 0;
2423 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2426 static LPWSTR
2427 shelllink_get_msi_component_path( LPWSTR component )
2429 LPWSTR path;
2430 DWORD r, sz = 0;
2432 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2433 if (r != ERROR_SUCCESS)
2434 return NULL;
2436 sz++;
2437 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2438 r = CommandLineFromMsiDescriptor( component, path, &sz );
2439 if (r != ERROR_SUCCESS)
2441 HeapFree( GetProcessHeap(), 0, path );
2442 path = NULL;
2445 TRACE("returning %s\n", debugstr_w( path ) );
2447 return path;
2450 static HRESULT WINAPI
2451 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2453 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2454 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2455 SHELLEXECUTEINFOW sei;
2456 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2457 LPWSTR args = NULL;
2458 LPWSTR path = NULL;
2459 HRESULT r;
2461 TRACE("%p %p\n", This, lpici );
2463 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2464 return E_INVALIDARG;
2466 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2468 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2469 return E_INVALIDARG;
2472 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2473 if ( FAILED( r ) )
2474 return r;
2476 if ( This->sComponent )
2478 path = shelllink_get_msi_component_path( This->sComponent );
2479 if (!path)
2480 return E_FAIL;
2482 else
2483 path = strdupW( This->sPath );
2485 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2486 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2488 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2489 DWORD len = 2;
2491 if ( This->sArgs )
2492 len += lstrlenW( This->sArgs );
2493 if ( iciex->lpParametersW )
2494 len += lstrlenW( iciex->lpParametersW );
2496 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2497 args[0] = 0;
2498 if ( This->sArgs )
2499 lstrcatW( args, This->sArgs );
2500 if ( iciex->lpParametersW )
2502 static const WCHAR space[] = { ' ', 0 };
2503 lstrcatW( args, space );
2504 lstrcatW( args, iciex->lpParametersW );
2508 memset( &sei, 0, sizeof sei );
2509 sei.cbSize = sizeof sei;
2510 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2511 sei.lpFile = path;
2512 sei.nShow = This->iShowCmd;
2513 sei.lpIDList = This->pPidl;
2514 sei.lpDirectory = This->sWorkDir;
2515 sei.lpParameters = args;
2516 sei.lpVerb = szOpen;
2518 if( ShellExecuteExW( &sei ) )
2519 r = S_OK;
2520 else
2521 r = E_FAIL;
2523 HeapFree( GetProcessHeap(), 0, args );
2524 HeapFree( GetProcessHeap(), 0, path );
2526 return r;
2529 static HRESULT WINAPI
2530 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2531 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2533 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2535 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2536 idCmd, uType, pwReserved, pszName, cchMax );
2538 return E_NOTIMPL;
2541 static const IContextMenuVtbl cmvt =
2543 ShellLink_ContextMenu_QueryInterface,
2544 ShellLink_ContextMenu_AddRef,
2545 ShellLink_ContextMenu_Release,
2546 ShellLink_QueryContextMenu,
2547 ShellLink_InvokeCommand,
2548 ShellLink_GetCommandString
2551 static HRESULT WINAPI
2552 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2554 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2555 return ShellLink_QueryInterface( This, riid, ppvObject );
2558 static ULONG WINAPI
2559 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2561 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2562 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2565 static ULONG WINAPI
2566 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2568 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2569 return IShellLinkW_Release(&This->IShellLinkW_iface);
2572 static HRESULT WINAPI
2573 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2575 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2577 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2579 if ( !This->site )
2580 return E_FAIL;
2581 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2584 static HRESULT WINAPI
2585 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2587 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2589 TRACE("%p %p\n", iface, punk);
2591 if ( punk )
2592 IUnknown_AddRef( punk );
2594 if( This->site )
2595 IUnknown_Release( This->site );
2597 This->site = punk;
2599 return S_OK;
2602 static const IObjectWithSiteVtbl owsvt =
2604 ShellLink_ObjectWithSite_QueryInterface,
2605 ShellLink_ObjectWithSite_AddRef,
2606 ShellLink_ObjectWithSite_Release,
2607 ShellLink_SetSite,
2608 ShellLink_GetSite,