Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / shell32 / shelllink.c
blob8b4964ba3a093a03afee3b4b20e5b62b9214a9b2
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 const IShellLinkAVtbl *lpVtbl;
132 const IShellLinkWVtbl *lpvtblw;
133 const IPersistFileVtbl *lpvtblPersistFile;
134 const IPersistStreamVtbl *lpvtblPersistStream;
135 const IShellLinkDataListVtbl *lpvtblShellLinkDataList;
136 const IShellExtInitVtbl *lpvtblShellExtInit;
137 const IContextMenuVtbl *lpvtblContextMenu;
138 const IObjectWithSiteVtbl *lpvtblObjectWithSite;
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;
164 } IShellLinkImpl;
166 static inline IShellLinkImpl *impl_from_IShellLinkW( IShellLinkW *iface )
168 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
171 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
173 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
176 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
178 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
181 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
183 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
186 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
188 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
191 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
193 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
196 static inline IShellLinkImpl *impl_from_IObjectWithSite( IObjectWithSite *iface )
198 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblObjectWithSite));
201 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
203 /* strdup on the process heap */
204 static inline LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
206 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
207 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
208 if( !p )
209 return p;
210 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
211 return p;
214 static inline LPWSTR strdupW( LPCWSTR src )
216 LPWSTR dest;
217 if (!src) return NULL;
218 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
219 if (dest)
220 lstrcpyW(dest, src);
221 return dest;
224 /**************************************************************************
225 * ShellLink::QueryInterface implementation
227 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
229 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
231 *ppvObj = NULL;
233 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
235 *ppvObj = This;
237 else if(IsEqualIID(riid, &IID_IShellLinkW))
239 *ppvObj = &(This->lpvtblw);
241 else if(IsEqualIID(riid, &IID_IPersistFile))
243 *ppvObj = &(This->lpvtblPersistFile);
245 else if(IsEqualIID(riid, &IID_IPersistStream))
247 *ppvObj = &(This->lpvtblPersistStream);
249 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
251 *ppvObj = &(This->lpvtblShellLinkDataList);
253 else if(IsEqualIID(riid, &IID_IShellExtInit))
255 *ppvObj = &(This->lpvtblShellExtInit);
257 else if(IsEqualIID(riid, &IID_IContextMenu))
259 *ppvObj = &(This->lpvtblContextMenu);
261 else if(IsEqualIID(riid, &IID_IObjectWithSite))
263 *ppvObj = &(This->lpvtblObjectWithSite);
266 if(*ppvObj)
268 IUnknown_AddRef((IUnknown*)(*ppvObj));
269 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
270 return S_OK;
272 ERR("-- Interface: E_NOINTERFACE\n");
273 return E_NOINTERFACE;
276 /**************************************************************************
277 * ShellLink::AddRef implementation
279 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
281 ULONG refCount = InterlockedIncrement(&This->ref);
283 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
285 return refCount;
288 /**************************************************************************
289 * ShellLink::Release implementation
291 static ULONG ShellLink_Release( IShellLinkImpl *This )
293 ULONG refCount = InterlockedDecrement(&This->ref);
295 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
297 if (refCount)
298 return refCount;
300 TRACE("-- destroying IShellLink(%p)\n",This);
302 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
303 HeapFree(GetProcessHeap(), 0, This->sArgs);
304 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
305 HeapFree(GetProcessHeap(), 0, This->sDescription);
306 HeapFree(GetProcessHeap(),0,This->sPath);
308 if (This->site)
309 IUnknown_Release( This->site );
311 if (This->pPidl)
312 ILFree(This->pPidl);
314 LocalFree((HANDLE)This);
316 return 0;
319 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
321 TRACE("%p %p\n", This, pclsid);
323 memcpy( pclsid, &CLSID_ShellLink, sizeof (CLSID) );
324 return S_OK;
327 /**************************************************************************
328 * IPersistFile_QueryInterface
330 static HRESULT WINAPI IPersistFile_fnQueryInterface(
331 IPersistFile* iface,
332 REFIID riid,
333 LPVOID *ppvObj)
335 IShellLinkImpl *This = impl_from_IPersistFile(iface);
336 return ShellLink_QueryInterface( This, riid, ppvObj );
339 /******************************************************************************
340 * IPersistFile_AddRef
342 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
344 IShellLinkImpl *This = impl_from_IPersistFile(iface);
345 return ShellLink_AddRef( This );
348 /******************************************************************************
349 * IPersistFile_Release
351 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
353 IShellLinkImpl *This = impl_from_IPersistFile(iface);
354 return IShellLinkA_Release((IShellLinkA*)This);
357 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
359 IShellLinkImpl *This = impl_from_IPersistFile(iface);
360 return ShellLink_GetClassID( This, pClassID );
363 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
365 IShellLinkImpl *This = impl_from_IPersistFile(iface);
367 TRACE("(%p)\n",This);
369 if (This->bDirty)
370 return S_OK;
372 return S_FALSE;
375 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
377 IShellLinkImpl *This = impl_from_IPersistFile(iface);
378 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
379 HRESULT r;
380 IStream *stm;
382 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
384 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
385 if( SUCCEEDED( r ) )
387 r = IPersistStream_Load(StreamThis, stm);
388 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
389 IStream_Release( stm );
390 This->bDirty = FALSE;
392 TRACE("-- returning hr %08x\n", r);
393 return r;
396 static BOOL StartLinkProcessor( LPCOLESTR szLink )
398 static const WCHAR szFormat[] = {
399 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
400 ' ','-','r',' ','"','%','s','"',0 };
401 LONG len;
402 LPWSTR buffer;
403 STARTUPINFOW si;
404 PROCESS_INFORMATION pi;
406 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
407 buffer = HeapAlloc( GetProcessHeap(), 0, len );
408 if( !buffer )
409 return FALSE;
411 wsprintfW( buffer, szFormat, szLink );
413 TRACE("starting %s\n",debugstr_w(buffer));
415 memset(&si, 0, sizeof(si));
416 si.cb = sizeof(si);
417 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
419 /* wait for a while to throttle the creation of linker processes */
420 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
421 WARN("Timed out waiting for shell linker\n");
423 CloseHandle( pi.hProcess );
424 CloseHandle( pi.hThread );
426 return TRUE;
429 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
431 IShellLinkImpl *This = impl_from_IPersistFile(iface);
432 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
433 HRESULT r;
434 IStream *stm;
435 static const WCHAR wszIEStartMenuHardCoded[] = {'C',':','\\','W','i','n','d','o','w','s','\\','S','t','a','r','t',' ','M','e','n','u','\\','P','r','o','g','r','a','m','s','\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','.','l','n','k',0};
436 static const WCHAR wszIEDesktopHardCoded[] = {'C',':','\\','W','i','n','d','o','w','s','\\','D','e','s','k','t','o','p','\\','I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','.','l','n','k',0};
437 WCHAR buffer[MAX_PATH];
439 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
441 if (!pszFileName)
442 return E_FAIL;
444 /* CrossOver HACK! The IE 6 installer hardcodes these paths (which work on
445 * Win9x), but don't in Windows 2000+ and Wine. So fix them up to use the
446 * proper functions to get the directory to save the link into */
447 if (!strcmpW(pszFileName, wszIEStartMenuHardCoded))
449 r = SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, SHGFP_TYPE_CURRENT, buffer);
450 strcatW(buffer, strchrW(strchrW(strchrW(wszIEStartMenuHardCoded, '\\') + 1, '\\') + 1, '\\'));
451 TRACE("changing %s to %s\n", debugstr_w(pszFileName), debugstr_w(buffer));
452 pszFileName = buffer;
454 else if (!strcmpW(pszFileName, wszIEDesktopHardCoded))
456 r = SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, buffer);
457 strcatW(buffer, strchrW(strchrW(strchrW(wszIEDesktopHardCoded, '\\') + 1, '\\') + 1, '\\'));
458 TRACE("changing %s to %s\n", debugstr_w(pszFileName), debugstr_w(buffer));
459 pszFileName = buffer;
462 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
463 if( SUCCEEDED( r ) )
465 r = IPersistStream_Save(StreamThis, stm, FALSE);
466 IStream_Release( stm );
468 if( SUCCEEDED( r ) )
470 StartLinkProcessor( pszFileName );
472 This->bDirty = FALSE;
474 else
476 DeleteFileW( pszFileName );
477 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
481 return r;
484 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
486 IShellLinkImpl *This = impl_from_IPersistFile(iface);
487 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
488 return NOERROR;
491 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
493 IShellLinkImpl *This = impl_from_IPersistFile(iface);
494 FIXME("(%p)\n",This);
495 return NOERROR;
498 static const IPersistFileVtbl pfvt =
500 IPersistFile_fnQueryInterface,
501 IPersistFile_fnAddRef,
502 IPersistFile_fnRelease,
503 IPersistFile_fnGetClassID,
504 IPersistFile_fnIsDirty,
505 IPersistFile_fnLoad,
506 IPersistFile_fnSave,
507 IPersistFile_fnSaveCompleted,
508 IPersistFile_fnGetCurFile
511 /************************************************************************
512 * IPersistStream_QueryInterface
514 static HRESULT WINAPI IPersistStream_fnQueryInterface(
515 IPersistStream* iface,
516 REFIID riid,
517 VOID** ppvObj)
519 IShellLinkImpl *This = impl_from_IPersistStream(iface);
520 return ShellLink_QueryInterface( This, riid, ppvObj );
523 /************************************************************************
524 * IPersistStream_Release
526 static ULONG WINAPI IPersistStream_fnRelease(
527 IPersistStream* iface)
529 IShellLinkImpl *This = impl_from_IPersistStream(iface);
530 return IShellLinkA_Release((IShellLinkA*)This);
533 /************************************************************************
534 * IPersistStream_AddRef
536 static ULONG WINAPI IPersistStream_fnAddRef(
537 IPersistStream* iface)
539 IShellLinkImpl *This = impl_from_IPersistStream(iface);
540 return ShellLink_AddRef( This );
543 /************************************************************************
544 * IPersistStream_GetClassID
547 static HRESULT WINAPI IPersistStream_fnGetClassID(
548 IPersistStream* iface,
549 CLSID* pClassID)
551 IShellLinkImpl *This = impl_from_IPersistStream(iface);
552 return ShellLink_GetClassID( This, pClassID );
555 /************************************************************************
556 * IPersistStream_IsDirty (IPersistStream)
558 static HRESULT WINAPI IPersistStream_fnIsDirty(
559 IPersistStream* iface)
561 IShellLinkImpl *This = impl_from_IPersistStream(iface);
563 TRACE("(%p)\n", This);
565 return S_OK;
569 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
571 DWORD count;
572 USHORT len;
573 LPVOID temp;
574 LPWSTR str;
575 HRESULT r;
577 TRACE("%p\n", stm);
579 count = 0;
580 r = IStream_Read(stm, &len, sizeof(len), &count);
581 if ( FAILED (r) || ( count != sizeof(len) ) )
582 return E_FAIL;
584 if( unicode )
585 len *= sizeof (WCHAR);
587 TRACE("reading %d\n", len);
588 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
589 if( !temp )
590 return E_OUTOFMEMORY;
591 count = 0;
592 r = IStream_Read(stm, temp, len, &count);
593 if( FAILED (r) || ( count != len ) )
595 HeapFree( GetProcessHeap(), 0, temp );
596 return E_FAIL;
599 TRACE("read %s\n", debugstr_an(temp,len));
601 /* convert to unicode if necessary */
602 if( !unicode )
604 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
605 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
606 if( str )
607 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
608 HeapFree( GetProcessHeap(), 0, temp );
610 else
612 count /= 2;
613 str = (LPWSTR) temp;
615 str[count] = 0;
617 *pstr = str;
619 return S_OK;
622 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
624 DWORD size;
625 ULONG count;
626 HRESULT r;
627 struct sized_chunk {
628 DWORD size;
629 unsigned char data[1];
630 } *chunk;
632 TRACE("%p\n",stm);
634 r = IStream_Read( stm, &size, sizeof(size), &count );
635 if( FAILED( r ) || count != sizeof(size) )
636 return E_FAIL;
638 chunk = HeapAlloc( GetProcessHeap(), 0, size );
639 if( !chunk )
640 return E_OUTOFMEMORY;
642 chunk->size = size;
643 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
644 if( FAILED( r ) || count != (size - sizeof(size)) )
646 HeapFree( GetProcessHeap(), 0, chunk );
647 return E_FAIL;
650 TRACE("Read %d bytes\n",chunk->size);
652 *data = (LPVOID) chunk;
654 return S_OK;
657 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
659 const int label_sz = sizeof volume->label/sizeof volume->label[0];
660 LPSTR label;
661 int len;
663 volume->serial = vol->dwVolSerial;
664 volume->type = vol->dwType;
666 if( !vol->dwVolLabelOfs )
667 return FALSE;
668 if( vol->dwSize <= vol->dwVolLabelOfs )
669 return FALSE;
670 len = vol->dwSize - vol->dwVolLabelOfs;
672 label = (LPSTR) vol;
673 label += vol->dwVolLabelOfs;
674 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
676 return TRUE;
679 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
681 int len = 0, wlen;
682 LPWSTR path;
684 while( p[len] && (len < maxlen) )
685 len++;
687 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
688 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
689 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
690 path[wlen] = 0;
692 return path;
695 static HRESULT Stream_LoadLocation( IStream *stm,
696 volume_info *volume, LPWSTR *path )
698 char *p = NULL;
699 LOCATION_INFO *loc;
700 HRESULT r;
701 int n;
703 r = Stream_ReadChunk( stm, (LPVOID*) &p );
704 if( FAILED(r) )
705 return r;
707 loc = (LOCATION_INFO*) p;
709 /* hack around .lnk files generated by older broken shelllink code */
710 if( loc->dwTotalSize == 0x18 )
712 ERR("link was generated by an older shelllink version\n");
713 memset( volume, 0, sizeof *volume );
714 *path = NULL;
715 return S_OK;
718 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
720 HeapFree( GetProcessHeap(), 0, p );
721 return E_FAIL;
724 /* if there's valid local volume information, load it */
725 if( loc->dwVolTableOfs &&
726 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
728 LOCAL_VOLUME_INFO *volume_info;
730 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
731 Stream_LoadVolume( volume_info, volume );
734 /* if there's a local path, load it */
735 n = loc->dwLocalPathOfs;
736 if( n && (n < loc->dwTotalSize) )
737 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
739 TRACE("type %d serial %08x name %s path %s\n", volume->type,
740 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
742 HeapFree( GetProcessHeap(), 0, p );
743 return S_OK;
747 * The format of the advertised shortcut info seems to be:
749 * Offset Description
750 * ------ -----------
752 * 0 Length of the block (4 bytes, usually 0x314)
753 * 4 tag (dword)
754 * 8 string data in ASCII
755 * 8+0x104 string data in UNICODE
757 * In the original Win32 implementation the buffers are not initialized
758 * to zero, so data trailing the string is random garbage.
760 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
762 DWORD size;
763 ULONG count;
764 HRESULT r;
765 EXP_DARWIN_LINK buffer;
767 TRACE("%p\n",stm);
769 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
770 if( FAILED( r ) )
771 return r;
773 /* make sure that we read the size of the structure even on error */
774 size = sizeof buffer - sizeof (DWORD);
775 if( buffer.dbh.cbSize != sizeof buffer )
777 ERR("Ooops. This structure is not as expected...\n");
778 return E_FAIL;
781 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
782 if( FAILED( r ) )
783 return r;
785 if( count != size )
786 return E_FAIL;
788 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
790 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
792 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
793 return E_FAIL;
796 *str = HeapAlloc( GetProcessHeap(), 0,
797 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
798 lstrcpyW( *str, buffer.szwDarwinID );
800 return S_OK;
803 /************************************************************************
804 * IPersistStream_Load (IPersistStream)
806 static HRESULT WINAPI IPersistStream_fnLoad(
807 IPersistStream* iface,
808 IStream* stm)
810 LINK_HEADER hdr;
811 ULONG dwBytesRead;
812 BOOL unicode;
813 HRESULT r;
814 DWORD zero;
816 IShellLinkImpl *This = impl_from_IPersistStream(iface);
818 TRACE("%p %p\n", This, stm);
820 if( !stm )
821 return STG_E_INVALIDPOINTER;
823 dwBytesRead = 0;
824 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
825 if( FAILED( r ) )
826 return r;
828 if( dwBytesRead != sizeof(hdr))
829 return E_FAIL;
830 if( hdr.dwSize != sizeof(hdr))
831 return E_FAIL;
832 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
833 return E_FAIL;
835 /* free all the old stuff */
836 ILFree(This->pPidl);
837 This->pPidl = NULL;
838 memset( &This->volume, 0, sizeof This->volume );
839 HeapFree(GetProcessHeap(), 0, This->sPath);
840 This->sPath = NULL;
841 HeapFree(GetProcessHeap(), 0, This->sDescription);
842 This->sDescription = NULL;
843 HeapFree(GetProcessHeap(), 0, This->sPathRel);
844 This->sPathRel = NULL;
845 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
846 This->sWorkDir = NULL;
847 HeapFree(GetProcessHeap(), 0, This->sArgs);
848 This->sArgs = NULL;
849 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
850 This->sIcoPath = NULL;
851 HeapFree(GetProcessHeap(), 0, This->sProduct);
852 This->sProduct = NULL;
853 HeapFree(GetProcessHeap(), 0, This->sComponent);
854 This->sComponent = NULL;
856 This->wHotKey = (WORD)hdr.wHotKey;
857 This->iIcoNdx = hdr.nIcon;
858 FileTimeToSystemTime (&hdr.Time1, &This->time1);
859 FileTimeToSystemTime (&hdr.Time2, &This->time2);
860 FileTimeToSystemTime (&hdr.Time3, &This->time3);
861 if (TRACE_ON(shell))
863 WCHAR sTemp[MAX_PATH];
864 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
865 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
866 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
867 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
868 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
869 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
870 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
871 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
872 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
875 /* load all the new stuff */
876 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
878 r = ILLoadFromStream( stm, &This->pPidl );
879 if( FAILED( r ) )
880 return r;
882 pdump(This->pPidl);
884 /* load the location information */
885 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
886 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
887 if( FAILED( r ) )
888 goto end;
890 unicode = hdr.dwFlags & SLDF_UNICODE;
891 if( hdr.dwFlags & SLDF_HAS_NAME )
893 r = Stream_LoadString( stm, unicode, &This->sDescription );
894 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
896 if( FAILED( r ) )
897 goto end;
899 if( hdr.dwFlags & SLDF_HAS_RELPATH )
901 r = Stream_LoadString( stm, unicode, &This->sPathRel );
902 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
904 if( FAILED( r ) )
905 goto end;
907 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
909 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
910 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
912 if( FAILED( r ) )
913 goto end;
915 if( hdr.dwFlags & SLDF_HAS_ARGS )
917 r = Stream_LoadString( stm, unicode, &This->sArgs );
918 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
920 if( FAILED( r ) )
921 goto end;
923 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
925 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
926 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
928 if( FAILED( r ) )
929 goto end;
931 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
933 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
934 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
936 if( FAILED( r ) )
937 goto end;
939 if( hdr.dwFlags & SLDF_HAS_DARWINID )
941 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
942 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
944 if( FAILED( r ) )
945 goto end;
947 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
948 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
949 ERR("Last word was not zero\n");
951 TRACE("OK\n");
953 pdump (This->pPidl);
955 return S_OK;
956 end:
957 return r;
960 /************************************************************************
961 * Stream_WriteString
963 * Helper function for IPersistStream_Save. Writes a unicode string
964 * with terminating nul byte to a stream, preceded by the its length.
966 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
968 USHORT len = lstrlenW( str ) + 1;
969 DWORD count;
970 HRESULT r;
972 r = IStream_Write( stm, &len, sizeof(len), &count );
973 if( FAILED( r ) )
974 return r;
976 len *= sizeof(WCHAR);
978 r = IStream_Write( stm, str, len, &count );
979 if( FAILED( r ) )
980 return r;
982 return S_OK;
985 /************************************************************************
986 * Stream_WriteLocationInfo
988 * Writes the location info to a stream
990 * FIXME: One day we might want to write the network volume information
991 * and the final path.
992 * Figure out how Windows deals with unicode paths here.
994 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
995 volume_info *volume )
997 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
998 LOCAL_VOLUME_INFO *vol;
999 LOCATION_INFO *loc;
1000 LPSTR szLabel, szPath, szFinalPath;
1001 ULONG count = 0;
1003 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
1005 /* figure out the size of everything */
1006 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1007 NULL, 0, NULL, NULL );
1008 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
1009 NULL, 0, NULL, NULL );
1010 volume_info_size = sizeof *vol + label_size;
1011 final_path_size = 1;
1012 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
1014 /* create pointers to everything */
1015 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
1016 vol = (LOCAL_VOLUME_INFO*) &loc[1];
1017 szLabel = (LPSTR) &vol[1];
1018 szPath = &szLabel[label_size];
1019 szFinalPath = &szPath[path_size];
1021 /* fill in the location information header */
1022 loc->dwTotalSize = total_size;
1023 loc->dwHeaderSize = sizeof (*loc);
1024 loc->dwFlags = 1;
1025 loc->dwVolTableOfs = sizeof (*loc);
1026 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
1027 loc->dwNetworkVolTableOfs = 0;
1028 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
1030 /* fill in the volume information */
1031 vol->dwSize = volume_info_size;
1032 vol->dwType = volume->type;
1033 vol->dwVolSerial = volume->serial;
1034 vol->dwVolLabelOfs = sizeof (*vol);
1036 /* copy in the strings */
1037 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
1038 szLabel, label_size, NULL, NULL );
1039 WideCharToMultiByte( CP_ACP, 0, path, -1,
1040 szPath, path_size, NULL, NULL );
1041 szFinalPath[0] = 0;
1043 return IStream_Write( stm, loc, total_size, &count );
1046 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
1048 EXP_DARWIN_LINK *buffer;
1050 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
1051 buffer->dbh.cbSize = sizeof *buffer;
1052 buffer->dbh.dwSignature = magic;
1053 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
1054 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
1056 return buffer;
1059 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1061 EXP_DARWIN_LINK *buffer;
1062 ULONG count;
1064 TRACE("%p\n",stm);
1066 buffer = shelllink_build_darwinid( string, magic );
1068 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1071 /************************************************************************
1072 * IPersistStream_Save (IPersistStream)
1074 * FIXME: makes assumptions about byte order
1076 static HRESULT WINAPI IPersistStream_fnSave(
1077 IPersistStream* iface,
1078 IStream* stm,
1079 BOOL fClearDirty)
1081 static const WCHAR wOpen[] = {'o','p','e','n',0};
1083 LINK_HEADER header;
1084 WCHAR exePath[MAX_PATH];
1085 ULONG count;
1086 DWORD zero;
1087 HRESULT r;
1089 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1091 TRACE("%p %p %x\n", This, stm, fClearDirty);
1093 *exePath = '\0';
1095 if (This->sPath)
1097 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
1098 NULL, NULL, NULL, NULL);
1100 * windows can create lnk files to executables that do not exist yet
1101 * so if the executable does not exist the just trust the path they
1102 * gave us
1104 if (!*exePath) lstrcpyW(exePath,This->sPath);
1107 memset(&header, 0, sizeof(header));
1108 header.dwSize = sizeof(header);
1109 header.fStartup = This->iShowCmd;
1110 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
1112 header.wHotKey = This->wHotKey;
1113 header.nIcon = This->iIcoNdx;
1114 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1115 if( This->pPidl )
1116 header.dwFlags |= SLDF_HAS_ID_LIST;
1117 if( This->sPath )
1118 header.dwFlags |= SLDF_HAS_LINK_INFO;
1119 if( This->sDescription )
1120 header.dwFlags |= SLDF_HAS_NAME;
1121 if( This->sWorkDir )
1122 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1123 if( This->sArgs )
1124 header.dwFlags |= SLDF_HAS_ARGS;
1125 if( This->sIcoPath )
1126 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1127 if( This->sProduct )
1128 header.dwFlags |= SLDF_HAS_LOGO3ID;
1129 if( This->sComponent )
1130 header.dwFlags |= SLDF_HAS_DARWINID;
1132 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1133 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1134 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1136 /* write the Shortcut header */
1137 r = IStream_Write( stm, &header, sizeof(header), &count );
1138 if( FAILED( r ) )
1140 ERR("Write failed at %d\n",__LINE__);
1141 return r;
1144 TRACE("Writing pidl\n");
1146 /* write the PIDL to the shortcut */
1147 if( This->pPidl )
1149 r = ILSaveToStream( stm, This->pPidl );
1150 if( FAILED( r ) )
1152 ERR("Failed to write PIDL at %d\n",__LINE__);
1153 return r;
1157 if( This->sPath )
1158 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1160 if( This->sDescription )
1161 r = Stream_WriteString( stm, This->sDescription );
1163 if( This->sPathRel )
1164 r = Stream_WriteString( stm, This->sPathRel );
1166 if( This->sWorkDir )
1167 r = Stream_WriteString( stm, This->sWorkDir );
1169 if( This->sArgs )
1170 r = Stream_WriteString( stm, This->sArgs );
1172 if( This->sIcoPath )
1173 r = Stream_WriteString( stm, This->sIcoPath );
1175 if( This->sProduct )
1176 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1178 if( This->sComponent )
1179 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1181 /* the last field is a single zero dword */
1182 zero = 0;
1183 r = IStream_Write( stm, &zero, sizeof zero, &count );
1185 return S_OK;
1188 /************************************************************************
1189 * IPersistStream_GetSizeMax (IPersistStream)
1191 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1192 IPersistStream* iface,
1193 ULARGE_INTEGER* pcbSize)
1195 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1197 TRACE("(%p)\n", This);
1199 return E_NOTIMPL;
1202 static const IPersistStreamVtbl psvt =
1204 IPersistStream_fnQueryInterface,
1205 IPersistStream_fnAddRef,
1206 IPersistStream_fnRelease,
1207 IPersistStream_fnGetClassID,
1208 IPersistStream_fnIsDirty,
1209 IPersistStream_fnLoad,
1210 IPersistStream_fnSave,
1211 IPersistStream_fnGetSizeMax
1214 /**************************************************************************
1215 * IShellLink_Constructor
1217 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1218 REFIID riid, LPVOID *ppv )
1220 IShellLinkImpl * sl;
1221 HRESULT r;
1223 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1225 *ppv = NULL;
1227 if (pUnkOuter)
1228 return CLASS_E_NOAGGREGATION;
1229 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1230 if (!sl)
1231 return E_OUTOFMEMORY;
1233 sl->ref = 1;
1234 sl->lpVtbl = &slvt;
1235 sl->lpvtblw = &slvtw;
1236 sl->lpvtblPersistFile = &pfvt;
1237 sl->lpvtblPersistStream = &psvt;
1238 sl->lpvtblShellLinkDataList = &dlvt;
1239 sl->lpvtblShellExtInit = &eivt;
1240 sl->lpvtblContextMenu = &cmvt;
1241 sl->lpvtblObjectWithSite = &owsvt;
1242 sl->iShowCmd = SW_SHOWNORMAL;
1243 sl->bDirty = FALSE;
1244 sl->iIdOpen = -1;
1245 sl->site = NULL;
1247 TRACE("(%p)->()\n",sl);
1249 r = ShellLink_QueryInterface( sl, riid, ppv );
1250 ShellLink_Release( sl );
1251 return r;
1255 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1257 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1258 return FALSE;
1259 return TRUE;
1262 /**************************************************************************
1263 * ShellLink_UpdatePath
1264 * update absolute path in sPath using relative path in sPathRel
1266 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1268 if (!path || !psPath)
1269 return E_INVALIDARG;
1271 if (!*psPath && sPathRel) {
1272 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1273 LPWSTR final = NULL;
1275 /* first try if [directory of link file] + [relative path] finds an existing file */
1277 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1278 if( !final )
1279 final = buffer;
1280 lstrcpyW(final, sPathRel);
1282 *abs_path = '\0';
1284 if (SHELL_ExistsFileW(buffer)) {
1285 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1286 lstrcpyW(abs_path, buffer);
1287 } else {
1288 /* try if [working directory] + [relative path] finds an existing file */
1289 if (sWorkDir) {
1290 lstrcpyW(buffer, sWorkDir);
1291 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1293 if (SHELL_ExistsFileW(buffer))
1294 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1295 lstrcpyW(abs_path, buffer);
1299 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1300 if (!*abs_path)
1301 lstrcpyW(abs_path, sPathRel);
1303 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1304 if (!*psPath)
1305 return E_OUTOFMEMORY;
1307 lstrcpyW(*psPath, abs_path);
1310 return S_OK;
1313 /**************************************************************************
1314 * IShellLink_ConstructFromFile
1316 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1317 LPCITEMIDLIST pidl, LPVOID* ppv)
1319 IShellLinkW* psl;
1321 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1323 if (SUCCEEDED(hr)) {
1324 IPersistFile* ppf;
1326 *ppv = NULL;
1328 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1330 if (SUCCEEDED(hr)) {
1331 WCHAR path[MAX_PATH];
1333 if (SHGetPathFromIDListW(pidl, path))
1334 hr = IPersistFile_Load(ppf, path, 0);
1335 else
1336 hr = E_FAIL;
1338 if (SUCCEEDED(hr))
1339 *ppv = (IUnknown*) psl;
1341 IPersistFile_Release(ppf);
1344 if (!*ppv)
1345 IShellLinkW_Release(psl);
1348 return hr;
1351 /**************************************************************************
1352 * IShellLinkA_QueryInterface
1354 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1356 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1357 return ShellLink_QueryInterface( This, riid, ppvObj );
1360 /******************************************************************************
1361 * IShellLinkA_AddRef
1363 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1365 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1366 return ShellLink_AddRef( This );
1369 /******************************************************************************
1370 * IShellLinkA_Release
1372 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1374 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1375 return ShellLink_Release( This );
1378 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1379 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1381 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1383 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1384 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1386 if (This->sComponent || This->sProduct)
1387 return S_FALSE;
1389 if (cchMaxPath)
1390 pszFile[0] = 0;
1391 if (This->sPath)
1392 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1393 pszFile, cchMaxPath, NULL, NULL);
1395 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1397 return S_OK;
1400 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1402 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1404 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1406 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1409 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1411 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1413 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1415 if (This->pPidl)
1416 ILFree(This->pPidl);
1417 This->pPidl = ILClone (pidl);
1418 This->bDirty = TRUE;
1420 return S_OK;
1423 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1425 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1427 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1429 if( cchMaxName )
1430 pszName[0] = 0;
1431 if( This->sDescription )
1432 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1433 pszName, cchMaxName, NULL, NULL);
1435 return S_OK;
1438 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1440 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1442 TRACE("(%p)->(pName=%s)\n", This, pszName);
1444 HeapFree(GetProcessHeap(), 0, This->sDescription);
1445 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1446 if ( !This->sDescription )
1447 return E_OUTOFMEMORY;
1449 This->bDirty = TRUE;
1451 return S_OK;
1454 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1456 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1458 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1460 if( cchMaxPath )
1461 pszDir[0] = 0;
1462 if( This->sWorkDir )
1463 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1464 pszDir, cchMaxPath, NULL, NULL);
1466 return S_OK;
1469 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1471 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1473 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1475 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1476 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1477 if ( !This->sWorkDir )
1478 return E_OUTOFMEMORY;
1480 This->bDirty = TRUE;
1482 return S_OK;
1485 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1487 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1489 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1491 if( cchMaxPath )
1492 pszArgs[0] = 0;
1493 if( This->sArgs )
1494 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1495 pszArgs, cchMaxPath, NULL, NULL);
1497 return S_OK;
1500 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1502 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1504 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1506 HeapFree(GetProcessHeap(), 0, This->sArgs);
1507 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1508 if( !This->sArgs )
1509 return E_OUTOFMEMORY;
1511 This->bDirty = TRUE;
1513 return S_OK;
1516 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1518 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1520 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1522 *pwHotkey = This->wHotKey;
1524 return S_OK;
1527 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1529 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1531 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1533 This->wHotKey = wHotkey;
1534 This->bDirty = TRUE;
1536 return S_OK;
1539 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1541 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1543 TRACE("(%p)->(%p)\n",This, piShowCmd);
1544 *piShowCmd = This->iShowCmd;
1545 return S_OK;
1548 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1550 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1552 TRACE("(%p) %d\n",This, iShowCmd);
1554 This->iShowCmd = iShowCmd;
1555 This->bDirty = TRUE;
1557 return NOERROR;
1560 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1562 LPCITEMIDLIST pidlLast;
1564 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1566 if (SUCCEEDED(hr)) {
1567 IExtractIconA* pei;
1569 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1571 if (SUCCEEDED(hr)) {
1572 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1574 IExtractIconA_Release(pei);
1577 IShellFolder_Release(psf);
1580 return hr;
1583 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1585 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1587 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1589 pszIconPath[0] = 0;
1590 *piIcon = This->iIcoNdx;
1592 if (This->sIcoPath)
1594 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1595 return S_OK;
1598 if (This->pPidl || This->sPath)
1600 IShellFolder* pdsk;
1602 HRESULT hr = SHGetDesktopFolder(&pdsk);
1604 if (SUCCEEDED(hr))
1606 /* first look for an icon using the PIDL (if present) */
1607 if (This->pPidl)
1608 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1609 else
1610 hr = E_FAIL;
1612 /* if we couldn't find an icon yet, look for it using the file system path */
1613 if (FAILED(hr) && This->sPath)
1615 LPITEMIDLIST pidl;
1617 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1619 if (SUCCEEDED(hr)) {
1620 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1622 SHFree(pidl);
1626 IShellFolder_Release(pdsk);
1629 return hr;
1631 return S_OK;
1634 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1636 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1638 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1640 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1641 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1642 if ( !This->sIcoPath )
1643 return E_OUTOFMEMORY;
1645 This->iIcoNdx = iIcon;
1646 This->bDirty = TRUE;
1648 return S_OK;
1651 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1653 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1655 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1657 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1658 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1659 This->bDirty = TRUE;
1661 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1664 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1666 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1668 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1670 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1673 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1675 HRESULT r;
1676 LPWSTR str;
1677 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1679 TRACE("(%p)->(path=%s)\n",This, pszFile);
1681 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1682 if( !str )
1683 return E_OUTOFMEMORY;
1685 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1686 HeapFree( GetProcessHeap(), 0, str );
1688 return r;
1691 /**************************************************************************
1692 * IShellLink Implementation
1695 static const IShellLinkAVtbl slvt =
1697 IShellLinkA_fnQueryInterface,
1698 IShellLinkA_fnAddRef,
1699 IShellLinkA_fnRelease,
1700 IShellLinkA_fnGetPath,
1701 IShellLinkA_fnGetIDList,
1702 IShellLinkA_fnSetIDList,
1703 IShellLinkA_fnGetDescription,
1704 IShellLinkA_fnSetDescription,
1705 IShellLinkA_fnGetWorkingDirectory,
1706 IShellLinkA_fnSetWorkingDirectory,
1707 IShellLinkA_fnGetArguments,
1708 IShellLinkA_fnSetArguments,
1709 IShellLinkA_fnGetHotkey,
1710 IShellLinkA_fnSetHotkey,
1711 IShellLinkA_fnGetShowCmd,
1712 IShellLinkA_fnSetShowCmd,
1713 IShellLinkA_fnGetIconLocation,
1714 IShellLinkA_fnSetIconLocation,
1715 IShellLinkA_fnSetRelativePath,
1716 IShellLinkA_fnResolve,
1717 IShellLinkA_fnSetPath
1721 /**************************************************************************
1722 * IShellLinkW_fnQueryInterface
1724 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1725 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1727 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1728 return ShellLink_QueryInterface( This, riid, ppvObj );
1731 /******************************************************************************
1732 * IShellLinkW_fnAddRef
1734 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1736 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1737 return ShellLink_AddRef( This );
1740 /******************************************************************************
1741 * IShellLinkW_fnRelease
1743 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1745 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1746 return ShellLink_Release( This );
1749 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1751 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1753 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1754 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1756 if (This->sComponent || This->sProduct)
1757 return S_FALSE;
1759 if (cchMaxPath)
1760 pszFile[0] = 0;
1761 if (This->sPath)
1762 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1764 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1766 return S_OK;
1769 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1771 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1773 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1775 if (!This->pPidl)
1777 *ppidl = NULL;
1778 return S_FALSE;
1780 *ppidl = ILClone(This->pPidl);
1781 return S_OK;
1784 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1786 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1788 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1790 if( This->pPidl )
1791 ILFree( This->pPidl );
1792 This->pPidl = ILClone( pidl );
1793 if( !This->pPidl )
1794 return E_FAIL;
1796 This->bDirty = TRUE;
1798 return S_OK;
1801 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1803 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1805 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1807 pszName[0] = 0;
1808 if( This->sDescription )
1809 lstrcpynW( pszName, This->sDescription, cchMaxName );
1811 return S_OK;
1814 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1816 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1818 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1820 HeapFree(GetProcessHeap(), 0, This->sDescription);
1821 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1822 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1823 if ( !This->sDescription )
1824 return E_OUTOFMEMORY;
1826 lstrcpyW( This->sDescription, pszName );
1827 This->bDirty = TRUE;
1829 return S_OK;
1832 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1834 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1836 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1838 if( cchMaxPath )
1839 pszDir[0] = 0;
1840 if( This->sWorkDir )
1841 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1843 return S_OK;
1846 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1848 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1850 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1852 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1853 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1854 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1855 if ( !This->sWorkDir )
1856 return E_OUTOFMEMORY;
1857 lstrcpyW( This->sWorkDir, pszDir );
1858 This->bDirty = TRUE;
1860 return S_OK;
1863 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1865 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1867 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1869 if( cchMaxPath )
1870 pszArgs[0] = 0;
1871 if( This->sArgs )
1872 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1874 return NOERROR;
1877 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1879 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1881 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1883 HeapFree(GetProcessHeap(), 0, This->sArgs);
1884 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1885 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1886 if ( !This->sArgs )
1887 return E_OUTOFMEMORY;
1888 lstrcpyW( This->sArgs, pszArgs );
1889 This->bDirty = TRUE;
1891 return S_OK;
1894 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1896 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1898 TRACE("(%p)->(%p)\n",This, pwHotkey);
1900 *pwHotkey=This->wHotKey;
1902 return S_OK;
1905 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1907 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1909 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1911 This->wHotKey = wHotkey;
1912 This->bDirty = TRUE;
1914 return S_OK;
1917 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1919 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1921 TRACE("(%p)->(%p)\n",This, piShowCmd);
1923 *piShowCmd = This->iShowCmd;
1925 return S_OK;
1928 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1930 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1932 This->iShowCmd = iShowCmd;
1933 This->bDirty = TRUE;
1935 return S_OK;
1938 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1940 LPCITEMIDLIST pidlLast;
1942 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1944 if (SUCCEEDED(hr)) {
1945 IExtractIconW* pei;
1947 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1949 if (SUCCEEDED(hr)) {
1950 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1952 IExtractIconW_Release(pei);
1955 IShellFolder_Release(psf);
1958 return hr;
1961 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1963 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1965 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1967 pszIconPath[0] = 0;
1968 *piIcon = This->iIcoNdx;
1970 if (This->sIcoPath)
1972 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1973 return S_OK;
1976 if (This->pPidl || This->sPath)
1978 IShellFolder* pdsk;
1980 HRESULT hr = SHGetDesktopFolder(&pdsk);
1982 if (SUCCEEDED(hr))
1984 /* first look for an icon using the PIDL (if present) */
1985 if (This->pPidl)
1986 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1987 else
1988 hr = E_FAIL;
1990 /* if we couldn't find an icon yet, look for it using the file system path */
1991 if (FAILED(hr) && This->sPath)
1993 LPITEMIDLIST pidl;
1995 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1997 if (SUCCEEDED(hr))
1999 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
2001 SHFree(pidl);
2005 IShellFolder_Release(pdsk);
2007 return hr;
2009 return S_OK;
2012 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
2014 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2016 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
2018 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
2019 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
2020 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
2021 if ( !This->sIcoPath )
2022 return E_OUTOFMEMORY;
2023 lstrcpyW( This->sIcoPath, pszIconPath );
2025 This->iIcoNdx = iIcon;
2026 This->bDirty = TRUE;
2028 return S_OK;
2031 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
2033 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2035 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
2037 HeapFree(GetProcessHeap(), 0, This->sPathRel);
2038 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
2039 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
2040 if ( !This->sPathRel )
2041 return E_OUTOFMEMORY;
2042 lstrcpyW( This->sPathRel, pszPathRel );
2043 This->bDirty = TRUE;
2045 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
2048 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
2050 HRESULT hr = S_OK;
2051 BOOL bSuccess;
2053 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2055 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
2057 /*FIXME: use IResolveShellLink interface */
2059 if (!This->sPath && This->pPidl) {
2060 WCHAR buffer[MAX_PATH];
2062 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2064 if (bSuccess && *buffer) {
2065 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2066 if (!This->sPath)
2067 return E_OUTOFMEMORY;
2069 lstrcpyW(This->sPath, buffer);
2071 This->bDirty = TRUE;
2072 } else
2073 hr = S_OK; /* don't report an error occurred while just caching information */
2076 if (!This->sIcoPath && This->sPath) {
2077 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2078 if (!This->sIcoPath)
2079 return E_OUTOFMEMORY;
2081 lstrcpyW(This->sIcoPath, This->sPath);
2082 This->iIcoNdx = 0;
2084 This->bDirty = TRUE;
2087 return hr;
2090 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2092 LPWSTR ret;
2093 LPCWSTR p;
2094 DWORD len;
2096 if( !str )
2097 return NULL;
2099 p = strchrW( str, ':' );
2100 if( !p )
2101 return NULL;
2102 len = p - str;
2103 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2104 if( !ret )
2105 return ret;
2106 memcpy( ret, str, sizeof(WCHAR)*len );
2107 ret[len] = 0;
2108 return ret;
2111 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2113 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2114 WCHAR szGuid[39];
2115 HRESULT r;
2116 GUID guid;
2117 int len;
2119 while( str[0] )
2121 /* each segment must start with two colons */
2122 if( str[0] != ':' || str[1] != ':' )
2123 return E_FAIL;
2125 /* the last segment is just two colons */
2126 if( !str[2] )
2127 break;
2128 str += 2;
2130 /* there must be a colon straight after a guid */
2131 p = strchrW( str, ':' );
2132 if( !p )
2133 return E_FAIL;
2134 len = p - str;
2135 if( len != 38 )
2136 return E_FAIL;
2138 /* get the guid, and check it's validly formatted */
2139 memcpy( szGuid, str, sizeof(WCHAR)*len );
2140 szGuid[len] = 0;
2141 r = CLSIDFromString( szGuid, &guid );
2142 if( r != S_OK )
2143 return r;
2144 str = p + 1;
2146 /* match it up to a guid that we care about */
2147 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2148 szComponent = str;
2149 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2150 szProduct = str;
2151 else
2152 return E_FAIL;
2154 /* skip to the next field */
2155 str = strchrW( str, ':' );
2156 if( !str )
2157 return E_FAIL;
2160 /* we have to have a component for an advertised shortcut */
2161 if( !szComponent )
2162 return E_FAIL;
2164 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2165 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2167 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2168 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2170 return S_OK;
2173 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2175 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2176 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2177 BOOL r;
2179 volume->type = GetDriveTypeW(drive);
2180 r = GetVolumeInformationW(drive, volume->label, label_sz,
2181 &volume->serial, NULL, NULL, NULL, 0);
2182 TRACE("r = %d type %d serial %08x name %s\n", r,
2183 volume->type, volume->serial, debugstr_w(volume->label));
2184 return r;
2187 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2189 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2190 WCHAR buffer[MAX_PATH];
2191 LPWSTR fname, unquoted = NULL;
2192 HRESULT hr = S_OK;
2193 UINT len;
2195 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2197 /* quotes at the ends of the string are stripped */
2198 len = lstrlenW(pszFile);
2199 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2201 unquoted = strdupW(pszFile);
2202 PathUnquoteSpacesW(unquoted);
2203 pszFile = unquoted;
2206 /* any other quote marks are invalid */
2207 if (strchrW(pszFile, '"'))
2208 return S_FALSE;
2210 HeapFree(GetProcessHeap(), 0, This->sPath);
2211 This->sPath = NULL;
2213 HeapFree(GetProcessHeap(), 0, This->sComponent);
2214 This->sComponent = NULL;
2216 if (This->pPidl)
2217 ILFree(This->pPidl);
2218 This->pPidl = NULL;
2220 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2222 if (*pszFile == '\0')
2223 *buffer = '\0';
2224 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2225 return E_FAIL;
2226 else if(!PathFileExistsW(buffer) &&
2227 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2228 hr = S_FALSE;
2230 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2231 ShellLink_GetVolumeInfo(buffer, &This->volume);
2233 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2234 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2235 if (!This->sPath)
2236 return E_OUTOFMEMORY;
2238 lstrcpyW(This->sPath, buffer);
2240 This->bDirty = TRUE;
2241 HeapFree(GetProcessHeap(), 0, unquoted);
2243 return hr;
2246 /**************************************************************************
2247 * IShellLinkW Implementation
2250 static const IShellLinkWVtbl slvtw =
2252 IShellLinkW_fnQueryInterface,
2253 IShellLinkW_fnAddRef,
2254 IShellLinkW_fnRelease,
2255 IShellLinkW_fnGetPath,
2256 IShellLinkW_fnGetIDList,
2257 IShellLinkW_fnSetIDList,
2258 IShellLinkW_fnGetDescription,
2259 IShellLinkW_fnSetDescription,
2260 IShellLinkW_fnGetWorkingDirectory,
2261 IShellLinkW_fnSetWorkingDirectory,
2262 IShellLinkW_fnGetArguments,
2263 IShellLinkW_fnSetArguments,
2264 IShellLinkW_fnGetHotkey,
2265 IShellLinkW_fnSetHotkey,
2266 IShellLinkW_fnGetShowCmd,
2267 IShellLinkW_fnSetShowCmd,
2268 IShellLinkW_fnGetIconLocation,
2269 IShellLinkW_fnSetIconLocation,
2270 IShellLinkW_fnSetRelativePath,
2271 IShellLinkW_fnResolve,
2272 IShellLinkW_fnSetPath
2275 static HRESULT WINAPI
2276 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2278 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2279 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2282 static ULONG WINAPI
2283 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2285 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2286 return IShellLinkA_AddRef((IShellLinkA*)This);
2289 static ULONG WINAPI
2290 ShellLink_DataList_Release( IShellLinkDataList* iface )
2292 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2293 return ShellLink_Release( This );
2296 static HRESULT WINAPI
2297 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2299 FIXME("\n");
2300 return E_NOTIMPL;
2303 static HRESULT WINAPI
2304 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2306 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2307 LPVOID block = NULL;
2308 HRESULT r = E_FAIL;
2310 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2312 switch (dwSig)
2314 case EXP_DARWIN_ID_SIG:
2315 if (!This->sComponent)
2316 break;
2317 block = shelllink_build_darwinid( This->sComponent, dwSig );
2318 r = S_OK;
2319 break;
2320 case EXP_SZ_LINK_SIG:
2321 case NT_CONSOLE_PROPS_SIG:
2322 case NT_FE_CONSOLE_PROPS_SIG:
2323 case EXP_SPECIAL_FOLDER_SIG:
2324 case EXP_SZ_ICON_SIG:
2325 FIXME("valid but unhandled datablock %08x\n", dwSig);
2326 break;
2327 default:
2328 ERR("unknown datablock %08x\n", dwSig);
2330 *ppDataBlock = block;
2331 return r;
2334 static HRESULT WINAPI
2335 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2337 FIXME("\n");
2338 return E_NOTIMPL;
2341 static HRESULT WINAPI
2342 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2344 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2345 DWORD flags = 0;
2347 FIXME("%p %p\n", This, pdwFlags );
2349 /* FIXME: add more */
2350 if (This->sArgs)
2351 flags |= SLDF_HAS_ARGS;
2352 if (This->sComponent)
2353 flags |= SLDF_HAS_DARWINID;
2354 if (This->sIcoPath)
2355 flags |= SLDF_HAS_ICONLOCATION;
2356 if (This->sProduct)
2357 flags |= SLDF_HAS_LOGO3ID;
2358 if (This->pPidl)
2359 flags |= SLDF_HAS_ID_LIST;
2361 *pdwFlags = flags;
2363 return S_OK;
2366 static HRESULT WINAPI
2367 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2369 FIXME("\n");
2370 return E_NOTIMPL;
2373 static const IShellLinkDataListVtbl dlvt =
2375 ShellLink_DataList_QueryInterface,
2376 ShellLink_DataList_AddRef,
2377 ShellLink_DataList_Release,
2378 ShellLink_AddDataBlock,
2379 ShellLink_CopyDataBlock,
2380 ShellLink_RemoveDataBlock,
2381 ShellLink_GetFlags,
2382 ShellLink_SetFlags
2385 static HRESULT WINAPI
2386 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2388 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2389 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2392 static ULONG WINAPI
2393 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2395 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2396 return IShellLinkA_AddRef((IShellLinkA*)This);
2399 static ULONG WINAPI
2400 ShellLink_ExtInit_Release( IShellExtInit* iface )
2402 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2403 return ShellLink_Release( This );
2406 /**************************************************************************
2407 * ShellLink implementation of IShellExtInit::Initialize()
2409 * Loads the shelllink from the dataobject the shell is pointing to.
2411 static HRESULT WINAPI
2412 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2413 IDataObject *pdtobj, HKEY hkeyProgID )
2415 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2416 FORMATETC format;
2417 STGMEDIUM stgm;
2418 UINT count;
2419 HRESULT r = E_FAIL;
2421 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2423 if( !pdtobj )
2424 return r;
2426 format.cfFormat = CF_HDROP;
2427 format.ptd = NULL;
2428 format.dwAspect = DVASPECT_CONTENT;
2429 format.lindex = -1;
2430 format.tymed = TYMED_HGLOBAL;
2432 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2433 return r;
2435 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2436 if( count == 1 )
2438 LPWSTR path;
2440 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2441 count++;
2442 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2443 if( path )
2445 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2447 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2448 r = IPersistFile_Load( pf, path, 0 );
2449 HeapFree( GetProcessHeap(), 0, path );
2452 ReleaseStgMedium( &stgm );
2454 return r;
2457 static const IShellExtInitVtbl eivt =
2459 ShellLink_ExtInit_QueryInterface,
2460 ShellLink_ExtInit_AddRef,
2461 ShellLink_ExtInit_Release,
2462 ShellLink_ExtInit_Initialize
2465 static HRESULT WINAPI
2466 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2468 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2469 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2472 static ULONG WINAPI
2473 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2475 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2476 return IShellLinkA_AddRef((IShellLinkA*)This);
2479 static ULONG WINAPI
2480 ShellLink_ContextMenu_Release( IContextMenu* iface )
2482 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2483 return ShellLink_Release( This );
2486 static HRESULT WINAPI
2487 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2488 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2490 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2491 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2492 MENUITEMINFOW mii;
2493 int id = 1;
2495 TRACE("%p %p %u %u %u %u\n", This,
2496 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2498 if ( !hmenu )
2499 return E_INVALIDARG;
2501 memset( &mii, 0, sizeof mii );
2502 mii.cbSize = sizeof mii;
2503 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2504 mii.dwTypeData = szOpen;
2505 mii.cch = strlenW( mii.dwTypeData );
2506 mii.wID = idCmdFirst + id++;
2507 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2508 mii.fType = MFT_STRING;
2509 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2510 return E_FAIL;
2511 This->iIdOpen = 0;
2513 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2516 static LPWSTR
2517 shelllink_get_msi_component_path( LPWSTR component )
2519 LPWSTR path;
2520 DWORD r, sz = 0;
2522 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2523 if (r != ERROR_SUCCESS)
2524 return NULL;
2526 sz++;
2527 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2528 r = CommandLineFromMsiDescriptor( component, path, &sz );
2529 if (r != ERROR_SUCCESS)
2531 HeapFree( GetProcessHeap(), 0, path );
2532 path = NULL;
2535 TRACE("returning %s\n", debugstr_w( path ) );
2537 return path;
2540 static HRESULT WINAPI
2541 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2543 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2544 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2545 SHELLEXECUTEINFOW sei;
2546 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2547 LPWSTR args = NULL;
2548 LPWSTR path = NULL;
2549 HRESULT r;
2551 TRACE("%p %p\n", This, lpici );
2553 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2554 return E_INVALIDARG;
2556 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2558 ERR("Unknown id %d != %d\n", (INT)lpici->lpVerb, This->iIdOpen );
2559 return E_INVALIDARG;
2562 r = IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, 0 );
2563 if ( FAILED( r ) )
2564 return r;
2566 if ( This->sComponent )
2568 path = shelllink_get_msi_component_path( This->sComponent );
2569 if (!path)
2570 return E_FAIL;
2572 else
2573 path = strdupW( This->sPath );
2575 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2576 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2578 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2579 DWORD len = 2;
2581 if ( This->sArgs )
2582 len += lstrlenW( This->sArgs );
2583 if ( iciex->lpParametersW )
2584 len += lstrlenW( iciex->lpParametersW );
2586 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2587 args[0] = 0;
2588 if ( This->sArgs )
2589 lstrcatW( args, This->sArgs );
2590 if ( iciex->lpParametersW )
2592 static const WCHAR space[] = { ' ', 0 };
2593 lstrcatW( args, space );
2594 lstrcatW( args, iciex->lpParametersW );
2598 memset( &sei, 0, sizeof sei );
2599 sei.cbSize = sizeof sei;
2600 sei.fMask = SEE_MASK_UNICODE | SEE_MASK_NOCLOSEPROCESS;
2601 sei.lpFile = path;
2602 sei.nShow = This->iShowCmd;
2603 sei.lpIDList = This->pPidl;
2604 sei.lpDirectory = This->sWorkDir;
2605 sei.lpParameters = args;
2606 sei.lpVerb = szOpen;
2608 if( ShellExecuteExW( &sei ) )
2610 if ( sei.hProcess )
2612 WaitForSingleObject( sei.hProcess, 10000 );
2613 CloseHandle( sei.hProcess );
2615 r = S_OK;
2617 else
2618 r = E_FAIL;
2620 HeapFree( GetProcessHeap(), 0, args );
2621 HeapFree( GetProcessHeap(), 0, path );
2623 return r;
2626 static HRESULT WINAPI
2627 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2628 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2630 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2632 FIXME("%p %u %u %p %p %u\n", This,
2633 idCmd, uType, pwReserved, pszName, cchMax );
2635 return E_NOTIMPL;
2638 static const IContextMenuVtbl cmvt =
2640 ShellLink_ContextMenu_QueryInterface,
2641 ShellLink_ContextMenu_AddRef,
2642 ShellLink_ContextMenu_Release,
2643 ShellLink_QueryContextMenu,
2644 ShellLink_InvokeCommand,
2645 ShellLink_GetCommandString
2648 static HRESULT WINAPI
2649 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2651 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2652 return ShellLink_QueryInterface( This, riid, ppvObject );
2655 static ULONG WINAPI
2656 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2658 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2659 return ShellLink_AddRef( This );
2662 static ULONG WINAPI
2663 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2665 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2666 return ShellLink_Release( This );
2669 static HRESULT WINAPI
2670 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2672 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2674 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2676 if ( !This->site )
2677 return E_FAIL;
2678 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2681 static HRESULT WINAPI
2682 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2684 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2686 TRACE("%p %p\n", iface, punk);
2688 if ( punk )
2689 IUnknown_AddRef( punk );
2690 This->site = punk;
2692 return S_OK;
2695 static const IObjectWithSiteVtbl owsvt =
2697 ShellLink_ObjectWithSite_QueryInterface,
2698 ShellLink_ObjectWithSite_AddRef,
2699 ShellLink_ObjectWithSite_Release,
2700 ShellLink_SetSite,
2701 ShellLink_GetSite,