winex11.drv: Refcount the vulkan surface window.
[wine.git] / dlls / shell32 / shelllink.c
blob0380ac9e7a4d6ba3de73b44424d1dd2f1926097f
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 /* IShellLink Implementation */
120 typedef struct
122 IShellLinkA IShellLinkA_iface;
123 IShellLinkW IShellLinkW_iface;
124 IPersistFile IPersistFile_iface;
125 IPersistStream IPersistStream_iface;
126 IShellLinkDataList IShellLinkDataList_iface;
127 IShellExtInit IShellExtInit_iface;
128 IContextMenu IContextMenu_iface;
129 IObjectWithSite IObjectWithSite_iface;
130 IPropertyStore IPropertyStore_iface;
132 LONG ref;
134 /* data structures according to the information in the link */
135 LPITEMIDLIST pPidl;
136 WORD wHotKey;
137 SYSTEMTIME time1;
138 SYSTEMTIME time2;
139 SYSTEMTIME time3;
141 DWORD iShowCmd;
142 LPWSTR sIcoPath;
143 INT iIcoNdx;
144 LPWSTR sPath;
145 LPWSTR sArgs;
146 LPWSTR sWorkDir;
147 LPWSTR sDescription;
148 LPWSTR sPathRel;
149 LPWSTR sProduct;
150 LPWSTR sComponent;
151 volume_info volume;
153 BOOL bDirty;
154 INT iIdOpen; /* id of the "Open" entry in the context menu */
155 IUnknown *site;
157 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
158 } IShellLinkImpl;
160 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
162 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
165 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
167 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
170 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
172 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
175 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
177 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
180 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
182 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
185 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
187 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
190 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
192 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
195 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
197 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
200 static inline IShellLinkImpl *impl_from_IPropertyStore(IPropertyStore *iface)
202 return CONTAINING_RECORD(iface, IShellLinkImpl, IPropertyStore_iface);
205 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
207 /* strdup on the process heap */
208 static inline LPWSTR heap_strdupAtoW( LPCSTR str)
210 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
211 LPWSTR p = heap_alloc( len*sizeof (WCHAR) );
212 if( !p )
213 return p;
214 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
215 return p;
218 /**************************************************************************
219 * IPersistFile_QueryInterface
221 static HRESULT WINAPI IPersistFile_fnQueryInterface(
222 IPersistFile* iface,
223 REFIID riid,
224 LPVOID *ppvObj)
226 IShellLinkImpl *This = impl_from_IPersistFile(iface);
227 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
230 /******************************************************************************
231 * IPersistFile_AddRef
233 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
235 IShellLinkImpl *This = impl_from_IPersistFile(iface);
236 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
239 /******************************************************************************
240 * IPersistFile_Release
242 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
244 IShellLinkImpl *This = impl_from_IPersistFile(iface);
245 return IShellLinkW_Release(&This->IShellLinkW_iface);
248 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
250 IShellLinkImpl *This = impl_from_IPersistFile(iface);
252 TRACE("(%p)->(%p)\n", This, pClassID);
254 *pClassID = CLSID_ShellLink;
256 return S_OK;
259 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
261 IShellLinkImpl *This = impl_from_IPersistFile(iface);
263 TRACE("(%p)\n",This);
265 if (This->bDirty)
266 return S_OK;
268 return S_FALSE;
271 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
273 IShellLinkImpl *This = impl_from_IPersistFile(iface);
274 IPersistStream *StreamThis = &This->IPersistStream_iface;
275 HRESULT r;
276 IStream *stm;
278 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
280 if( dwMode == 0 )
281 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
282 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
283 if( SUCCEEDED( r ) )
285 r = IPersistStream_Load(StreamThis, stm);
286 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
287 IStream_Release( stm );
289 /* update file path */
290 heap_free(This->filepath);
291 This->filepath = strdupW(pszFileName);
293 This->bDirty = FALSE;
295 TRACE("-- returning hr %08x\n", r);
296 return r;
299 BOOL run_winemenubuilder( const WCHAR *args )
301 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
302 LONG len;
303 LPWSTR buffer;
304 STARTUPINFOW si;
305 PROCESS_INFORMATION pi;
306 BOOL ret;
307 WCHAR app[MAX_PATH];
308 void *redir;
310 GetSystemDirectoryW( app, MAX_PATH - ARRAY_SIZE(menubuilder) );
311 strcatW( app, menubuilder );
313 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
314 buffer = heap_alloc( len );
315 if( !buffer )
316 return FALSE;
318 strcpyW( buffer, app );
319 strcatW( buffer, args );
321 TRACE("starting %s\n",debugstr_w(buffer));
323 memset(&si, 0, sizeof(si));
324 si.cb = sizeof(si);
326 Wow64DisableWow64FsRedirection( &redir );
327 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
328 Wow64RevertWow64FsRedirection( redir );
330 heap_free( buffer );
332 if (ret)
334 CloseHandle( pi.hProcess );
335 CloseHandle( pi.hThread );
338 return ret;
341 static BOOL StartLinkProcessor( LPCOLESTR szLink )
343 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
344 LONG len;
345 LPWSTR buffer;
346 BOOL ret;
348 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
349 buffer = heap_alloc( len );
350 if( !buffer )
351 return FALSE;
353 wsprintfW( buffer, szFormat, szLink );
354 ret = run_winemenubuilder( buffer );
355 heap_free( buffer );
356 return ret;
359 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
361 IShellLinkImpl *This = impl_from_IPersistFile(iface);
362 IPersistStream *StreamThis = &This->IPersistStream_iface;
363 HRESULT r;
364 IStream *stm;
366 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
368 if (!pszFileName)
369 return E_FAIL;
371 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
372 if( SUCCEEDED( r ) )
374 r = IPersistStream_Save(StreamThis, stm, FALSE);
375 IStream_Release( stm );
377 if( SUCCEEDED( r ) )
379 StartLinkProcessor( pszFileName );
381 /* update file path */
382 heap_free(This->filepath);
383 This->filepath = strdupW(pszFileName);
385 This->bDirty = FALSE;
387 else
389 DeleteFileW( pszFileName );
390 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
394 return r;
397 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
399 IShellLinkImpl *This = impl_from_IPersistFile(iface);
400 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
401 return S_OK;
404 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
406 IShellLinkImpl *This = impl_from_IPersistFile(iface);
407 IMalloc *pMalloc;
409 TRACE("(%p)->(%p)\n", This, filename);
411 if (!This->filepath)
413 *filename = NULL;
414 return S_FALSE;
417 SHGetMalloc(&pMalloc);
418 *filename = IMalloc_Alloc(pMalloc, (strlenW(This->filepath)+1)*sizeof(WCHAR));
419 if (!*filename) return E_OUTOFMEMORY;
421 strcpyW(*filename, This->filepath);
423 return S_OK;
426 static const IPersistFileVtbl pfvt =
428 IPersistFile_fnQueryInterface,
429 IPersistFile_fnAddRef,
430 IPersistFile_fnRelease,
431 IPersistFile_fnGetClassID,
432 IPersistFile_fnIsDirty,
433 IPersistFile_fnLoad,
434 IPersistFile_fnSave,
435 IPersistFile_fnSaveCompleted,
436 IPersistFile_fnGetCurFile
439 /************************************************************************
440 * IPersistStream_QueryInterface
442 static HRESULT WINAPI IPersistStream_fnQueryInterface(
443 IPersistStream* iface,
444 REFIID riid,
445 VOID** ppvObj)
447 IShellLinkImpl *This = impl_from_IPersistStream(iface);
448 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
451 /************************************************************************
452 * IPersistStream_Release
454 static ULONG WINAPI IPersistStream_fnRelease(
455 IPersistStream* iface)
457 IShellLinkImpl *This = impl_from_IPersistStream(iface);
458 return IShellLinkW_Release(&This->IShellLinkW_iface);
461 /************************************************************************
462 * IPersistStream_AddRef
464 static ULONG WINAPI IPersistStream_fnAddRef(
465 IPersistStream* iface)
467 IShellLinkImpl *This = impl_from_IPersistStream(iface);
468 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
471 /************************************************************************
472 * IPersistStream_GetClassID
475 static HRESULT WINAPI IPersistStream_fnGetClassID(
476 IPersistStream* iface,
477 CLSID* pClassID)
479 IShellLinkImpl *This = impl_from_IPersistStream(iface);
480 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
483 /************************************************************************
484 * IPersistStream_IsDirty (IPersistStream)
486 static HRESULT WINAPI IPersistStream_fnIsDirty(
487 IPersistStream* iface)
489 IShellLinkImpl *This = impl_from_IPersistStream(iface);
491 TRACE("(%p)\n", This);
493 return S_OK;
497 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
499 DWORD count;
500 USHORT len;
501 LPVOID temp;
502 LPWSTR str;
503 HRESULT r;
505 TRACE("%p\n", stm);
507 count = 0;
508 r = IStream_Read(stm, &len, sizeof(len), &count);
509 if ( FAILED (r) || ( count != sizeof(len) ) )
510 return E_FAIL;
512 if( unicode )
513 len *= sizeof (WCHAR);
515 TRACE("reading %d\n", len);
516 temp = heap_alloc(len + sizeof(WCHAR));
517 if( !temp )
518 return E_OUTOFMEMORY;
519 count = 0;
520 r = IStream_Read(stm, temp, len, &count);
521 if( FAILED (r) || ( count != len ) )
523 heap_free( temp );
524 return E_FAIL;
527 TRACE("read %s\n", debugstr_an(temp,len));
529 /* convert to unicode if necessary */
530 if( !unicode )
532 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
533 str = heap_alloc( (count+1)*sizeof (WCHAR) );
534 if( !str )
536 heap_free( temp );
537 return E_OUTOFMEMORY;
539 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
540 heap_free( temp );
542 else
544 count /= 2;
545 str = temp;
547 str[count] = 0;
549 *pstr = str;
551 return S_OK;
554 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
556 DWORD size;
557 ULONG count;
558 HRESULT r;
559 struct sized_chunk {
560 DWORD size;
561 unsigned char data[1];
562 } *chunk;
564 TRACE("%p\n",stm);
566 r = IStream_Read( stm, &size, sizeof(size), &count );
567 if( FAILED( r ) || count != sizeof(size) )
568 return E_FAIL;
570 chunk = heap_alloc( size );
571 if( !chunk )
572 return E_OUTOFMEMORY;
574 chunk->size = size;
575 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
576 if( FAILED( r ) || count != (size - sizeof(size)) )
578 heap_free( chunk );
579 return E_FAIL;
582 TRACE("Read %d bytes\n",chunk->size);
584 *data = chunk;
586 return S_OK;
589 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
591 const int label_sz = ARRAY_SIZE(volume->label);
592 LPSTR label;
593 int len;
595 volume->serial = vol->dwVolSerial;
596 volume->type = vol->dwType;
598 if( !vol->dwVolLabelOfs )
599 return FALSE;
600 if( vol->dwSize <= vol->dwVolLabelOfs )
601 return FALSE;
602 len = vol->dwSize - vol->dwVolLabelOfs;
604 label = (LPSTR) vol;
605 label += vol->dwVolLabelOfs;
606 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
608 return TRUE;
611 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
613 int len = 0, wlen;
614 LPWSTR path;
616 while( (len < maxlen) && p[len] )
617 len++;
619 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
620 path = heap_alloc((wlen + 1) * sizeof(WCHAR));
621 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
622 path[wlen] = 0;
624 return path;
627 static HRESULT Stream_LoadLocation( IStream *stm,
628 volume_info *volume, LPWSTR *path )
630 char *p = NULL;
631 LOCATION_INFO *loc;
632 HRESULT r;
633 DWORD n;
635 r = Stream_ReadChunk( stm, (LPVOID*) &p );
636 if( FAILED(r) )
637 return r;
639 loc = (LOCATION_INFO*) p;
640 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
642 heap_free( p );
643 return E_FAIL;
646 /* if there's valid local volume information, load it */
647 if( loc->dwVolTableOfs &&
648 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
650 LOCAL_VOLUME_INFO *volume_info;
652 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
653 Stream_LoadVolume( volume_info, volume );
656 /* if there's a local path, load it */
657 n = loc->dwLocalPathOfs;
658 if( n && (n < loc->dwTotalSize) )
659 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
661 TRACE("type %d serial %08x name %s path %s\n", volume->type,
662 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
664 heap_free( p );
665 return S_OK;
669 * The format of the advertised shortcut info seems to be:
671 * Offset Description
672 * ------ -----------
674 * 0 Length of the block (4 bytes, usually 0x314)
675 * 4 tag (dword)
676 * 8 string data in ASCII
677 * 8+0x104 string data in UNICODE
679 * In the original Win32 implementation the buffers are not initialized
680 * to zero, so data trailing the string is random garbage.
682 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
684 DWORD size;
685 ULONG count;
686 HRESULT r;
687 EXP_DARWIN_LINK buffer;
689 TRACE("%p\n",stm);
691 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
692 if( FAILED( r ) )
693 return r;
695 /* make sure that we read the size of the structure even on error */
696 size = sizeof buffer - sizeof (DWORD);
697 if( buffer.dbh.cbSize != sizeof buffer )
699 ERR("Ooops. This structure is not as expected...\n");
700 return E_FAIL;
703 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
704 if( FAILED( r ) )
705 return r;
707 if( count != size )
708 return E_FAIL;
710 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
712 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
714 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
715 return E_FAIL;
718 *str = heap_alloc((lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
719 lstrcpyW( *str, buffer.szwDarwinID );
721 return S_OK;
724 /************************************************************************
725 * IPersistStream_Load (IPersistStream)
727 static HRESULT WINAPI IPersistStream_fnLoad(
728 IPersistStream* iface,
729 IStream* stm)
731 LINK_HEADER hdr;
732 ULONG dwBytesRead;
733 BOOL unicode;
734 HRESULT r;
735 DWORD zero;
737 IShellLinkImpl *This = impl_from_IPersistStream(iface);
739 TRACE("%p %p\n", This, stm);
741 if( !stm )
742 return STG_E_INVALIDPOINTER;
744 dwBytesRead = 0;
745 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
746 if( FAILED( r ) )
747 return r;
749 if( dwBytesRead != sizeof(hdr))
750 return E_FAIL;
751 if( hdr.dwSize != sizeof(hdr))
752 return E_FAIL;
753 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
754 return E_FAIL;
756 /* free all the old stuff */
757 ILFree(This->pPidl);
758 This->pPidl = NULL;
759 memset( &This->volume, 0, sizeof This->volume );
760 heap_free(This->sPath);
761 This->sPath = NULL;
762 heap_free(This->sDescription);
763 This->sDescription = NULL;
764 heap_free(This->sPathRel);
765 This->sPathRel = NULL;
766 heap_free(This->sWorkDir);
767 This->sWorkDir = NULL;
768 heap_free(This->sArgs);
769 This->sArgs = NULL;
770 heap_free(This->sIcoPath);
771 This->sIcoPath = NULL;
772 heap_free(This->sProduct);
773 This->sProduct = NULL;
774 heap_free(This->sComponent);
775 This->sComponent = NULL;
777 This->wHotKey = (WORD)hdr.wHotKey;
778 This->iIcoNdx = hdr.nIcon;
779 FileTimeToSystemTime (&hdr.Time1, &This->time1);
780 FileTimeToSystemTime (&hdr.Time2, &This->time2);
781 FileTimeToSystemTime (&hdr.Time3, &This->time3);
782 if (TRACE_ON(shell))
784 WCHAR sTemp[MAX_PATH];
785 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->time1, NULL, sTemp, ARRAY_SIZE(sTemp));
786 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
787 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->time2, NULL, sTemp, ARRAY_SIZE(sTemp));
788 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
789 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->time3, NULL, sTemp, ARRAY_SIZE(sTemp));
790 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
793 /* load all the new stuff */
794 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
796 r = ILLoadFromStream( stm, &This->pPidl );
797 if( FAILED( r ) )
798 return r;
800 pdump(This->pPidl);
802 /* load the location information */
803 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
804 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
805 if( FAILED( r ) )
806 goto end;
808 unicode = hdr.dwFlags & SLDF_UNICODE;
809 if( hdr.dwFlags & SLDF_HAS_NAME )
811 r = Stream_LoadString( stm, unicode, &This->sDescription );
812 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
814 if( FAILED( r ) )
815 goto end;
817 if( hdr.dwFlags & SLDF_HAS_RELPATH )
819 r = Stream_LoadString( stm, unicode, &This->sPathRel );
820 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
822 if( FAILED( r ) )
823 goto end;
825 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
827 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
828 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
830 if( FAILED( r ) )
831 goto end;
833 if( hdr.dwFlags & SLDF_HAS_ARGS )
835 r = Stream_LoadString( stm, unicode, &This->sArgs );
836 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
838 if( FAILED( r ) )
839 goto end;
841 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
843 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
844 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
846 if( FAILED( r ) )
847 goto end;
849 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
851 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
852 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
854 if( FAILED( r ) )
855 goto end;
857 if( hdr.dwFlags & SLDF_HAS_DARWINID )
859 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
860 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
862 if( FAILED( r ) )
863 goto end;
865 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
866 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
868 /* Some lnk files have extra data blocks starting with a
869 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
870 * one with a 0xa0000003 signature. However these don't seem to matter
871 * too much.
873 WARN("Last word was not zero\n");
876 TRACE("OK\n");
878 pdump (This->pPidl);
880 return S_OK;
881 end:
882 return r;
885 /************************************************************************
886 * Stream_WriteString
888 * Helper function for IPersistStream_Save. Writes a unicode string
889 * with terminating nul byte to a stream, preceded by the its length.
891 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
893 USHORT len = lstrlenW( str ) + 1;
894 DWORD count;
895 HRESULT r;
897 r = IStream_Write( stm, &len, sizeof(len), &count );
898 if( FAILED( r ) )
899 return r;
901 len *= sizeof(WCHAR);
903 r = IStream_Write( stm, str, len, &count );
904 if( FAILED( r ) )
905 return r;
907 return S_OK;
910 /************************************************************************
911 * Stream_WriteLocationInfo
913 * Writes the location info to a stream
915 * FIXME: One day we might want to write the network volume information
916 * and the final path.
917 * Figure out how Windows deals with unicode paths here.
919 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
920 volume_info *volume )
922 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
923 LOCAL_VOLUME_INFO *vol;
924 LOCATION_INFO *loc;
925 LPSTR szLabel, szPath, szFinalPath;
926 ULONG count = 0;
927 HRESULT hr;
929 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
931 /* figure out the size of everything */
932 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
933 NULL, 0, NULL, NULL );
934 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
935 NULL, 0, NULL, NULL );
936 volume_info_size = sizeof *vol + label_size;
937 final_path_size = 1;
938 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
940 /* create pointers to everything */
941 loc = heap_alloc_zero(total_size);
942 vol = (LOCAL_VOLUME_INFO*) &loc[1];
943 szLabel = (LPSTR) &vol[1];
944 szPath = &szLabel[label_size];
945 szFinalPath = &szPath[path_size];
947 /* fill in the location information header */
948 loc->dwTotalSize = total_size;
949 loc->dwHeaderSize = sizeof (*loc);
950 loc->dwFlags = 1;
951 loc->dwVolTableOfs = sizeof (*loc);
952 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
953 loc->dwNetworkVolTableOfs = 0;
954 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
956 /* fill in the volume information */
957 vol->dwSize = volume_info_size;
958 vol->dwType = volume->type;
959 vol->dwVolSerial = volume->serial;
960 vol->dwVolLabelOfs = sizeof (*vol);
962 /* copy in the strings */
963 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
964 szLabel, label_size, NULL, NULL );
965 WideCharToMultiByte( CP_ACP, 0, path, -1,
966 szPath, path_size, NULL, NULL );
967 szFinalPath[0] = 0;
969 hr = IStream_Write( stm, loc, total_size, &count );
970 heap_free(loc);
972 return hr;
975 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
977 EXP_DARWIN_LINK *buffer;
979 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
980 buffer->dbh.cbSize = sizeof *buffer;
981 buffer->dbh.dwSignature = magic;
982 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
983 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
985 return buffer;
988 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
990 EXP_DARWIN_LINK *buffer;
991 ULONG count;
993 TRACE("%p\n",stm);
995 buffer = shelllink_build_darwinid( string, magic );
997 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1000 /************************************************************************
1001 * IPersistStream_Save (IPersistStream)
1003 * FIXME: makes assumptions about byte order
1005 static HRESULT WINAPI IPersistStream_fnSave(
1006 IPersistStream* iface,
1007 IStream* stm,
1008 BOOL fClearDirty)
1010 LINK_HEADER header;
1011 ULONG count;
1012 DWORD zero;
1013 HRESULT r;
1015 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1017 TRACE("%p %p %x\n", This, stm, fClearDirty);
1019 memset(&header, 0, sizeof(header));
1020 header.dwSize = sizeof(header);
1021 header.fStartup = This->iShowCmd;
1022 header.MagicGuid = CLSID_ShellLink;
1024 header.wHotKey = This->wHotKey;
1025 header.nIcon = This->iIcoNdx;
1026 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1027 if( This->pPidl )
1028 header.dwFlags |= SLDF_HAS_ID_LIST;
1029 if( This->sPath )
1030 header.dwFlags |= SLDF_HAS_LINK_INFO;
1031 if( This->sDescription )
1032 header.dwFlags |= SLDF_HAS_NAME;
1033 if( This->sWorkDir )
1034 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1035 if( This->sArgs )
1036 header.dwFlags |= SLDF_HAS_ARGS;
1037 if( This->sIcoPath )
1038 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1039 if( This->sProduct )
1040 header.dwFlags |= SLDF_HAS_LOGO3ID;
1041 if( This->sComponent )
1042 header.dwFlags |= SLDF_HAS_DARWINID;
1044 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1045 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1046 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1048 /* write the Shortcut header */
1049 r = IStream_Write( stm, &header, sizeof(header), &count );
1050 if( FAILED( r ) )
1052 ERR("Write failed at %d\n",__LINE__);
1053 return r;
1056 TRACE("Writing pidl\n");
1058 /* write the PIDL to the shortcut */
1059 if( This->pPidl )
1061 r = ILSaveToStream( stm, This->pPidl );
1062 if( FAILED( r ) )
1064 ERR("Failed to write PIDL at %d\n",__LINE__);
1065 return r;
1069 if( This->sPath )
1070 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1072 if( This->sDescription )
1073 r = Stream_WriteString( stm, This->sDescription );
1075 if( This->sPathRel )
1076 r = Stream_WriteString( stm, This->sPathRel );
1078 if( This->sWorkDir )
1079 r = Stream_WriteString( stm, This->sWorkDir );
1081 if( This->sArgs )
1082 r = Stream_WriteString( stm, This->sArgs );
1084 if( This->sIcoPath )
1085 r = Stream_WriteString( stm, This->sIcoPath );
1087 if( This->sProduct )
1088 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1090 if( This->sComponent )
1091 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1093 /* the last field is a single zero dword */
1094 zero = 0;
1095 r = IStream_Write( stm, &zero, sizeof zero, &count );
1097 return S_OK;
1100 /************************************************************************
1101 * IPersistStream_GetSizeMax (IPersistStream)
1103 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1104 IPersistStream* iface,
1105 ULARGE_INTEGER* pcbSize)
1107 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1109 TRACE("(%p)\n", This);
1111 return E_NOTIMPL;
1114 static const IPersistStreamVtbl psvt =
1116 IPersistStream_fnQueryInterface,
1117 IPersistStream_fnAddRef,
1118 IPersistStream_fnRelease,
1119 IPersistStream_fnGetClassID,
1120 IPersistStream_fnIsDirty,
1121 IPersistStream_fnLoad,
1122 IPersistStream_fnSave,
1123 IPersistStream_fnGetSizeMax
1126 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1128 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1129 return FALSE;
1130 return TRUE;
1133 /**************************************************************************
1134 * ShellLink_UpdatePath
1135 * update absolute path in sPath using relative path in sPathRel
1137 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1139 if (!path || !psPath)
1140 return E_INVALIDARG;
1142 if (!*psPath && sPathRel) {
1143 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1144 LPWSTR final = NULL;
1146 /* first try if [directory of link file] + [relative path] finds an existing file */
1148 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1149 if( !final )
1150 final = buffer;
1151 lstrcpyW(final, sPathRel);
1153 *abs_path = '\0';
1155 if (SHELL_ExistsFileW(buffer)) {
1156 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1157 lstrcpyW(abs_path, buffer);
1158 } else {
1159 /* try if [working directory] + [relative path] finds an existing file */
1160 if (sWorkDir) {
1161 lstrcpyW(buffer, sWorkDir);
1162 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1164 if (SHELL_ExistsFileW(buffer))
1165 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1166 lstrcpyW(abs_path, buffer);
1170 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1171 if (!*abs_path)
1172 lstrcpyW(abs_path, sPathRel);
1174 *psPath = heap_alloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1175 if (!*psPath)
1176 return E_OUTOFMEMORY;
1178 lstrcpyW(*psPath, abs_path);
1181 return S_OK;
1184 /**************************************************************************
1185 * IShellLink_ConstructFromFile
1187 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1188 LPCITEMIDLIST pidl, IUnknown **ppv)
1190 IShellLinkW* psl;
1192 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1194 if (SUCCEEDED(hr)) {
1195 IPersistFile* ppf;
1197 *ppv = NULL;
1199 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1201 if (SUCCEEDED(hr)) {
1202 WCHAR path[MAX_PATH];
1204 if (SHGetPathFromIDListW(pidl, path))
1205 hr = IPersistFile_Load(ppf, path, 0);
1206 else
1207 hr = E_FAIL;
1209 if (SUCCEEDED(hr))
1210 *ppv = (IUnknown*)psl;
1212 IPersistFile_Release(ppf);
1215 if (!*ppv)
1216 IShellLinkW_Release(psl);
1219 return hr;
1222 /**************************************************************************
1223 * IShellLinkA_QueryInterface
1225 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1227 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1228 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1231 /******************************************************************************
1232 * IShellLinkA_AddRef
1234 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1236 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1237 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1240 /******************************************************************************
1241 * IShellLinkA_Release
1243 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1245 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1246 return IShellLinkW_Release(&This->IShellLinkW_iface);
1249 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1250 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1252 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1253 HRESULT res = S_OK;
1255 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1256 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1258 if (This->sComponent || This->sProduct)
1259 return S_FALSE;
1261 if (cchMaxPath)
1262 pszFile[0] = 0;
1263 if (This->sPath && This->sPath[0])
1264 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1265 pszFile, cchMaxPath, NULL, NULL);
1266 else
1267 res = S_FALSE;
1269 if (pfd)
1271 memset(pfd, 0, sizeof(*pfd));
1273 if (res == S_OK)
1275 char path[MAX_PATH];
1276 WIN32_FILE_ATTRIBUTE_DATA fad;
1278 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1280 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1282 pfd->dwFileAttributes = fad.dwFileAttributes;
1283 pfd->ftCreationTime = fad.ftCreationTime;
1284 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1285 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1286 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1287 pfd->nFileSizeLow = fad.nFileSizeLow;
1290 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1292 if (GetShortPathNameA(path, path, MAX_PATH))
1294 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1298 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1299 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1300 wine_dbgstr_a(pfd->cAlternateFileName));
1303 return res;
1306 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1308 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1309 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1312 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1314 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1315 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1318 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1319 INT cchMaxName)
1321 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1323 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1325 if( cchMaxName )
1326 pszName[0] = 0;
1327 if( This->sDescription )
1328 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1329 pszName, cchMaxName, NULL, NULL);
1331 return S_OK;
1334 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1336 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1337 WCHAR *descrW;
1338 HRESULT hr;
1340 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1342 if (pszName)
1344 descrW = heap_strdupAtoW(pszName);
1345 if (!descrW) return E_OUTOFMEMORY;
1347 else
1348 descrW = NULL;
1350 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1351 heap_free(descrW);
1353 return hr;
1356 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1357 INT cchMaxPath)
1359 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1361 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1363 if( cchMaxPath )
1364 pszDir[0] = 0;
1365 if( This->sWorkDir )
1366 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1367 pszDir, cchMaxPath, NULL, NULL);
1369 return S_OK;
1372 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1374 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1375 WCHAR *dirW;
1376 HRESULT hr;
1378 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1380 dirW = heap_strdupAtoW(pszDir);
1381 if (!dirW) return E_OUTOFMEMORY;
1383 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1384 heap_free(dirW);
1386 return hr;
1389 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1391 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1393 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1395 if( cchMaxPath )
1396 pszArgs[0] = 0;
1397 if( This->sArgs )
1398 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1399 pszArgs, cchMaxPath, NULL, NULL);
1401 return S_OK;
1404 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1406 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1407 WCHAR *argsW;
1408 HRESULT hr;
1410 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1412 if (pszArgs)
1414 argsW = heap_strdupAtoW(pszArgs);
1415 if (!argsW) return E_OUTOFMEMORY;
1417 else
1418 argsW = NULL;
1420 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1421 heap_free(argsW);
1423 return hr;
1426 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1428 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1429 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1432 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1434 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1435 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1438 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1440 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1441 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1444 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1446 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1447 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1450 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1451 INT cchIconPath, INT *piIcon)
1453 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1455 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1457 *piIcon = This->iIcoNdx;
1459 if (This->sIcoPath)
1460 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1461 else
1462 pszIconPath[0] = 0;
1464 return S_OK;
1467 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR pszIconPath,
1468 INT iIcon)
1470 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1471 WCHAR *pathW;
1472 HRESULT hr;
1474 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1476 pathW = heap_strdupAtoW(pszIconPath);
1477 if (!pathW) return E_OUTOFMEMORY;
1479 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, pathW, iIcon);
1480 heap_free(pathW);
1482 return hr;
1485 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1486 DWORD dwReserved)
1488 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1489 WCHAR *pathW;
1490 HRESULT hr;
1492 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1494 pathW = heap_strdupAtoW(pszPathRel);
1495 if (!pathW) return E_OUTOFMEMORY;
1497 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1498 heap_free(pathW);
1500 return hr;
1503 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1505 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1507 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1509 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1512 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1514 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1515 HRESULT r;
1516 LPWSTR str;
1518 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1520 if (!pszFile) return E_INVALIDARG;
1522 str = heap_strdupAtoW(pszFile);
1523 if( !str )
1524 return E_OUTOFMEMORY;
1526 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1527 heap_free( str );
1529 return r;
1532 /**************************************************************************
1533 * IShellLink Implementation
1536 static const IShellLinkAVtbl slvt =
1538 IShellLinkA_fnQueryInterface,
1539 IShellLinkA_fnAddRef,
1540 IShellLinkA_fnRelease,
1541 IShellLinkA_fnGetPath,
1542 IShellLinkA_fnGetIDList,
1543 IShellLinkA_fnSetIDList,
1544 IShellLinkA_fnGetDescription,
1545 IShellLinkA_fnSetDescription,
1546 IShellLinkA_fnGetWorkingDirectory,
1547 IShellLinkA_fnSetWorkingDirectory,
1548 IShellLinkA_fnGetArguments,
1549 IShellLinkA_fnSetArguments,
1550 IShellLinkA_fnGetHotkey,
1551 IShellLinkA_fnSetHotkey,
1552 IShellLinkA_fnGetShowCmd,
1553 IShellLinkA_fnSetShowCmd,
1554 IShellLinkA_fnGetIconLocation,
1555 IShellLinkA_fnSetIconLocation,
1556 IShellLinkA_fnSetRelativePath,
1557 IShellLinkA_fnResolve,
1558 IShellLinkA_fnSetPath
1562 /**************************************************************************
1563 * IShellLinkW_fnQueryInterface
1565 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1566 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1568 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1570 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1572 *ppvObj = NULL;
1574 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1576 *ppvObj = &This->IShellLinkA_iface;
1578 else if(IsEqualIID(riid, &IID_IShellLinkW))
1580 *ppvObj = &This->IShellLinkW_iface;
1582 else if(IsEqualIID(riid, &IID_IPersistFile))
1584 *ppvObj = &This->IPersistFile_iface;
1586 else if(IsEqualIID(riid, &IID_IPersistStream))
1588 *ppvObj = &This->IPersistStream_iface;
1590 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1592 *ppvObj = &This->IShellLinkDataList_iface;
1594 else if(IsEqualIID(riid, &IID_IShellExtInit))
1596 *ppvObj = &This->IShellExtInit_iface;
1598 else if(IsEqualIID(riid, &IID_IContextMenu))
1600 *ppvObj = &This->IContextMenu_iface;
1602 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1604 *ppvObj = &This->IObjectWithSite_iface;
1606 else if(IsEqualIID(riid, &IID_IPropertyStore))
1608 *ppvObj = &This->IPropertyStore_iface;
1611 if(*ppvObj)
1613 IUnknown_AddRef((IUnknown*)*ppvObj);
1614 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1615 return S_OK;
1617 ERR("-- Interface: E_NOINTERFACE\n");
1618 return E_NOINTERFACE;
1621 /******************************************************************************
1622 * IShellLinkW_fnAddRef
1624 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1626 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1627 ULONG ref = InterlockedIncrement(&This->ref);
1629 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1631 return ref;
1634 /******************************************************************************
1635 * IShellLinkW_fnRelease
1637 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1639 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1640 ULONG refCount = InterlockedDecrement(&This->ref);
1642 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1644 if (refCount)
1645 return refCount;
1647 TRACE("-- destroying IShellLink(%p)\n",This);
1649 heap_free(This->sIcoPath);
1650 heap_free(This->sArgs);
1651 heap_free(This->sWorkDir);
1652 heap_free(This->sDescription);
1653 heap_free(This->sPath);
1654 heap_free(This->sPathRel);
1655 heap_free(This->sProduct);
1656 heap_free(This->sComponent);
1657 heap_free(This->filepath);
1659 if (This->site)
1660 IUnknown_Release( This->site );
1662 if (This->pPidl)
1663 ILFree(This->pPidl);
1665 LocalFree(This);
1667 return 0;
1670 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1672 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1673 HRESULT res = S_OK;
1675 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1676 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1678 if (This->sComponent || This->sProduct)
1679 return S_FALSE;
1681 if (cchMaxPath)
1682 pszFile[0] = 0;
1683 if (This->sPath)
1684 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1685 else
1686 res = S_FALSE;
1688 if (pfd)
1690 memset(pfd, 0, sizeof(*pfd));
1692 if (res == S_OK)
1694 WCHAR path[MAX_PATH];
1695 WIN32_FILE_ATTRIBUTE_DATA fad;
1697 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1699 pfd->dwFileAttributes = fad.dwFileAttributes;
1700 pfd->ftCreationTime = fad.ftCreationTime;
1701 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1702 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1703 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1704 pfd->nFileSizeLow = fad.nFileSizeLow;
1707 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1709 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1711 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1715 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1716 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1717 wine_dbgstr_w(pfd->cAlternateFileName));
1720 return res;
1723 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1725 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1727 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1729 if (!This->pPidl)
1731 *ppidl = NULL;
1732 return S_FALSE;
1734 *ppidl = ILClone(This->pPidl);
1735 return S_OK;
1738 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1740 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1741 WCHAR path[MAX_PATH];
1743 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1745 if( This->pPidl )
1746 ILFree( This->pPidl );
1747 This->pPidl = ILClone( pidl );
1748 if( !This->pPidl )
1749 return E_FAIL;
1751 heap_free( This->sPath );
1752 This->sPath = NULL;
1754 if ( SHGetPathFromIDListW( pidl, path ) )
1756 This->sPath = heap_alloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1757 if (!This->sPath)
1758 return E_OUTOFMEMORY;
1760 lstrcpyW(This->sPath, path);
1763 This->bDirty = TRUE;
1765 return S_OK;
1768 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1770 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1772 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1774 pszName[0] = 0;
1775 if( This->sDescription )
1776 lstrcpynW( pszName, This->sDescription, cchMaxName );
1778 return S_OK;
1781 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1783 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1785 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1787 heap_free(This->sDescription);
1788 if (pszName)
1790 This->sDescription = heap_alloc((lstrlenW( pszName )+1)*sizeof(WCHAR) );
1791 if ( !This->sDescription )
1792 return E_OUTOFMEMORY;
1794 lstrcpyW( This->sDescription, pszName );
1796 else
1797 This->sDescription = NULL;
1798 This->bDirty = TRUE;
1800 return S_OK;
1803 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1805 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1807 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1809 if( cchMaxPath )
1810 pszDir[0] = 0;
1811 if( This->sWorkDir )
1812 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1814 return S_OK;
1817 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1819 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1821 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1823 heap_free(This->sWorkDir);
1824 This->sWorkDir = heap_alloc((lstrlenW( pszDir ) + 1) * sizeof (WCHAR) );
1825 if ( !This->sWorkDir )
1826 return E_OUTOFMEMORY;
1827 lstrcpyW( This->sWorkDir, pszDir );
1828 This->bDirty = TRUE;
1830 return S_OK;
1833 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1835 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1837 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1839 if( cchMaxPath )
1840 pszArgs[0] = 0;
1841 if( This->sArgs )
1842 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1844 return S_OK;
1847 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1849 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1851 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1853 heap_free(This->sArgs);
1854 if (pszArgs)
1856 This->sArgs = heap_alloc((lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1857 if ( !This->sArgs )
1858 return E_OUTOFMEMORY;
1859 lstrcpyW( This->sArgs, pszArgs );
1861 else This->sArgs = NULL;
1863 This->bDirty = TRUE;
1865 return S_OK;
1868 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1870 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1872 TRACE("(%p)->(%p)\n",This, pwHotkey);
1874 *pwHotkey=This->wHotKey;
1876 return S_OK;
1879 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1881 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1883 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1885 This->wHotKey = wHotkey;
1886 This->bDirty = TRUE;
1888 return S_OK;
1891 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1893 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1895 TRACE("(%p)->(%p)\n",This, piShowCmd);
1897 *piShowCmd = This->iShowCmd;
1899 return S_OK;
1902 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1904 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1906 TRACE("(%p)->(%d)\n", This, iShowCmd);
1908 This->iShowCmd = iShowCmd;
1909 This->bDirty = TRUE;
1911 return S_OK;
1914 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1916 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1918 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1920 *piIcon = This->iIcoNdx;
1922 if (This->sIcoPath)
1923 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1924 else
1925 pszIconPath[0] = 0;
1927 return S_OK;
1930 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1932 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1934 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1936 heap_free(This->sIcoPath);
1937 This->sIcoPath = heap_alloc((lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1938 if ( !This->sIcoPath )
1939 return E_OUTOFMEMORY;
1940 lstrcpyW( This->sIcoPath, pszIconPath );
1942 This->iIcoNdx = iIcon;
1943 This->bDirty = TRUE;
1945 return S_OK;
1948 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1950 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1952 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1954 heap_free(This->sPathRel);
1955 This->sPathRel = heap_alloc((lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1956 if ( !This->sPathRel )
1957 return E_OUTOFMEMORY;
1958 lstrcpyW( This->sPathRel, pszPathRel );
1959 This->bDirty = TRUE;
1961 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1964 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1966 HRESULT hr = S_OK;
1967 BOOL bSuccess;
1969 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1971 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1973 /*FIXME: use IResolveShellLink interface */
1975 if (!This->sPath && This->pPidl) {
1976 WCHAR buffer[MAX_PATH];
1978 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1980 if (bSuccess && *buffer) {
1981 This->sPath = heap_alloc((lstrlenW(buffer)+1)*sizeof(WCHAR));
1982 if (!This->sPath)
1983 return E_OUTOFMEMORY;
1985 lstrcpyW(This->sPath, buffer);
1987 This->bDirty = TRUE;
1988 } else
1989 hr = S_OK; /* don't report an error occurred while just caching information */
1992 if (!This->sIcoPath && This->sPath) {
1993 This->sIcoPath = heap_alloc((lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1994 if (!This->sIcoPath)
1995 return E_OUTOFMEMORY;
1997 lstrcpyW(This->sIcoPath, This->sPath);
1998 This->iIcoNdx = 0;
2000 This->bDirty = TRUE;
2003 return hr;
2006 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2008 LPWSTR ret;
2009 LPCWSTR p;
2010 DWORD len;
2012 if( !str )
2013 return NULL;
2015 p = strchrW( str, ':' );
2016 if( !p )
2017 return NULL;
2018 len = p - str;
2019 ret = heap_alloc(sizeof(WCHAR)*(len+1));
2020 if( !ret )
2021 return ret;
2022 memcpy( ret, str, sizeof(WCHAR)*len );
2023 ret[len] = 0;
2024 return ret;
2027 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2029 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2030 WCHAR szGuid[39];
2031 HRESULT r;
2032 GUID guid;
2033 int len;
2035 while( str[0] )
2037 /* each segment must start with two colons */
2038 if( str[0] != ':' || str[1] != ':' )
2039 return E_FAIL;
2041 /* the last segment is just two colons */
2042 if( !str[2] )
2043 break;
2044 str += 2;
2046 /* there must be a colon straight after a guid */
2047 p = strchrW( str, ':' );
2048 if( !p )
2049 return E_FAIL;
2050 len = p - str;
2051 if( len != 38 )
2052 return E_FAIL;
2054 /* get the guid, and check it's validly formatted */
2055 memcpy( szGuid, str, sizeof(WCHAR)*len );
2056 szGuid[len] = 0;
2057 r = CLSIDFromString( szGuid, &guid );
2058 if( r != S_OK )
2059 return r;
2060 str = p + 1;
2062 /* match it up to a guid that we care about */
2063 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2064 szComponent = str;
2065 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2066 szProduct = str;
2067 else
2068 return E_FAIL;
2070 /* skip to the next field */
2071 str = strchrW( str, ':' );
2072 if( !str )
2073 return E_FAIL;
2076 /* we have to have a component for an advertised shortcut */
2077 if( !szComponent )
2078 return E_FAIL;
2080 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2081 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2083 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2084 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2086 return S_OK;
2089 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2091 const int label_sz = ARRAY_SIZE(volume->label);
2092 WCHAR drive[] = { path[0], ':', '\\', 0 };
2093 BOOL r;
2095 volume->type = GetDriveTypeW(drive);
2096 r = GetVolumeInformationW(drive, volume->label, label_sz,
2097 &volume->serial, NULL, NULL, NULL, 0);
2098 TRACE("r = %d type %d serial %08x name %s\n", r,
2099 volume->type, volume->serial, debugstr_w(volume->label));
2100 return r;
2103 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2105 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2106 WCHAR buffer[MAX_PATH];
2107 LPWSTR fname, unquoted = NULL;
2108 HRESULT hr = S_OK;
2109 UINT len;
2111 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2113 if (!pszFile) return E_INVALIDARG;
2115 /* quotes at the ends of the string are stripped */
2116 len = lstrlenW(pszFile);
2117 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2119 unquoted = strdupW(pszFile);
2120 PathUnquoteSpacesW(unquoted);
2121 pszFile = unquoted;
2124 /* any other quote marks are invalid */
2125 if (strchrW(pszFile, '"'))
2127 heap_free(unquoted);
2128 return S_FALSE;
2131 heap_free(This->sPath);
2132 This->sPath = NULL;
2134 heap_free(This->sComponent);
2135 This->sComponent = NULL;
2137 if (This->pPidl)
2138 ILFree(This->pPidl);
2139 This->pPidl = NULL;
2141 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2143 if (*pszFile == '\0')
2144 *buffer = '\0';
2145 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2146 return E_FAIL;
2147 else if(!PathFileExistsW(buffer) &&
2148 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2149 hr = S_FALSE;
2151 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2152 ShellLink_GetVolumeInfo(buffer, &This->volume);
2154 This->sPath = heap_alloc( (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2155 if (!This->sPath)
2157 heap_free(unquoted);
2158 return E_OUTOFMEMORY;
2161 lstrcpyW(This->sPath, buffer);
2163 This->bDirty = TRUE;
2164 heap_free(unquoted);
2166 return hr;
2169 /**************************************************************************
2170 * IShellLinkW Implementation
2173 static const IShellLinkWVtbl slvtw =
2175 IShellLinkW_fnQueryInterface,
2176 IShellLinkW_fnAddRef,
2177 IShellLinkW_fnRelease,
2178 IShellLinkW_fnGetPath,
2179 IShellLinkW_fnGetIDList,
2180 IShellLinkW_fnSetIDList,
2181 IShellLinkW_fnGetDescription,
2182 IShellLinkW_fnSetDescription,
2183 IShellLinkW_fnGetWorkingDirectory,
2184 IShellLinkW_fnSetWorkingDirectory,
2185 IShellLinkW_fnGetArguments,
2186 IShellLinkW_fnSetArguments,
2187 IShellLinkW_fnGetHotkey,
2188 IShellLinkW_fnSetHotkey,
2189 IShellLinkW_fnGetShowCmd,
2190 IShellLinkW_fnSetShowCmd,
2191 IShellLinkW_fnGetIconLocation,
2192 IShellLinkW_fnSetIconLocation,
2193 IShellLinkW_fnSetRelativePath,
2194 IShellLinkW_fnResolve,
2195 IShellLinkW_fnSetPath
2198 static HRESULT WINAPI
2199 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2201 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2202 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2205 static ULONG WINAPI
2206 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2208 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2209 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2212 static ULONG WINAPI
2213 ShellLink_DataList_Release( IShellLinkDataList* iface )
2215 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2216 return IShellLinkW_Release(&This->IShellLinkW_iface);
2219 static HRESULT WINAPI
2220 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2222 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2223 return E_NOTIMPL;
2226 static HRESULT WINAPI
2227 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2229 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2230 LPVOID block = NULL;
2231 HRESULT r = E_FAIL;
2233 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2235 switch (dwSig)
2237 case EXP_DARWIN_ID_SIG:
2238 if (!This->sComponent)
2239 break;
2240 block = shelllink_build_darwinid( This->sComponent, dwSig );
2241 r = S_OK;
2242 break;
2243 case EXP_SZ_LINK_SIG:
2244 case NT_CONSOLE_PROPS_SIG:
2245 case NT_FE_CONSOLE_PROPS_SIG:
2246 case EXP_SPECIAL_FOLDER_SIG:
2247 case EXP_SZ_ICON_SIG:
2248 FIXME("valid but unhandled datablock %08x\n", dwSig);
2249 break;
2250 default:
2251 ERR("unknown datablock %08x\n", dwSig);
2253 *ppDataBlock = block;
2254 return r;
2257 static HRESULT WINAPI
2258 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2260 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2261 return E_NOTIMPL;
2264 static HRESULT WINAPI
2265 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2267 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2268 DWORD flags = 0;
2270 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2272 /* FIXME: add more */
2273 if (This->sArgs)
2274 flags |= SLDF_HAS_ARGS;
2275 if (This->sComponent)
2276 flags |= SLDF_HAS_DARWINID;
2277 if (This->sIcoPath)
2278 flags |= SLDF_HAS_ICONLOCATION;
2279 if (This->sProduct)
2280 flags |= SLDF_HAS_LOGO3ID;
2281 if (This->pPidl)
2282 flags |= SLDF_HAS_ID_LIST;
2284 *pdwFlags = flags;
2286 return S_OK;
2289 static HRESULT WINAPI
2290 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2292 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2293 return E_NOTIMPL;
2296 static const IShellLinkDataListVtbl dlvt =
2298 ShellLink_DataList_QueryInterface,
2299 ShellLink_DataList_AddRef,
2300 ShellLink_DataList_Release,
2301 ShellLink_AddDataBlock,
2302 ShellLink_CopyDataBlock,
2303 ShellLink_RemoveDataBlock,
2304 ShellLink_GetFlags,
2305 ShellLink_SetFlags
2308 static HRESULT WINAPI
2309 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2311 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2312 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2315 static ULONG WINAPI
2316 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2318 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2319 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2322 static ULONG WINAPI
2323 ShellLink_ExtInit_Release( IShellExtInit* iface )
2325 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2326 return IShellLinkW_Release(&This->IShellLinkW_iface);
2329 /**************************************************************************
2330 * ShellLink implementation of IShellExtInit::Initialize()
2332 * Loads the shelllink from the dataobject the shell is pointing to.
2334 static HRESULT WINAPI
2335 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2336 IDataObject *pdtobj, HKEY hkeyProgID )
2338 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2339 FORMATETC format;
2340 STGMEDIUM stgm;
2341 UINT count;
2342 HRESULT r = E_FAIL;
2344 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2346 if( !pdtobj )
2347 return r;
2349 format.cfFormat = CF_HDROP;
2350 format.ptd = NULL;
2351 format.dwAspect = DVASPECT_CONTENT;
2352 format.lindex = -1;
2353 format.tymed = TYMED_HGLOBAL;
2355 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2356 return r;
2358 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2359 if( count == 1 )
2361 LPWSTR path;
2363 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2364 count++;
2365 path = heap_alloc(count*sizeof(WCHAR) );
2366 if( path )
2368 IPersistFile *pf = &This->IPersistFile_iface;
2370 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2371 r = IPersistFile_Load( pf, path, 0 );
2372 heap_free( path );
2375 ReleaseStgMedium( &stgm );
2377 return r;
2380 static const IShellExtInitVtbl eivt =
2382 ShellLink_ExtInit_QueryInterface,
2383 ShellLink_ExtInit_AddRef,
2384 ShellLink_ExtInit_Release,
2385 ShellLink_ExtInit_Initialize
2388 static HRESULT WINAPI
2389 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2391 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2392 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2395 static ULONG WINAPI
2396 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2398 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2399 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2402 static ULONG WINAPI
2403 ShellLink_ContextMenu_Release( IContextMenu* iface )
2405 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2406 return IShellLinkW_Release(&This->IShellLinkW_iface);
2409 static HRESULT WINAPI
2410 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2411 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2413 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2414 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2415 MENUITEMINFOW mii;
2416 int id = 1;
2418 TRACE("%p %p %u %u %u %u\n", This,
2419 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2421 if ( !hmenu )
2422 return E_INVALIDARG;
2424 memset( &mii, 0, sizeof mii );
2425 mii.cbSize = sizeof mii;
2426 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2427 mii.dwTypeData = szOpen;
2428 mii.cch = strlenW( mii.dwTypeData );
2429 mii.wID = idCmdFirst + id++;
2430 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2431 mii.fType = MFT_STRING;
2432 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2433 return E_FAIL;
2434 This->iIdOpen = 0;
2436 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2439 static LPWSTR
2440 shelllink_get_msi_component_path( LPWSTR component )
2442 LPWSTR path;
2443 DWORD r, sz = 0;
2445 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2446 if (r != ERROR_SUCCESS)
2447 return NULL;
2449 sz++;
2450 path = heap_alloc( sz*sizeof(WCHAR) );
2451 r = CommandLineFromMsiDescriptor( component, path, &sz );
2452 if (r != ERROR_SUCCESS)
2454 heap_free( path );
2455 path = NULL;
2458 TRACE("returning %s\n", debugstr_w( path ) );
2460 return path;
2463 static HRESULT WINAPI
2464 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2466 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2467 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2468 SHELLEXECUTEINFOW sei;
2469 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2470 LPWSTR args = NULL;
2471 LPWSTR path = NULL;
2472 HRESULT r;
2474 TRACE("%p %p\n", This, lpici );
2476 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2477 return E_INVALIDARG;
2479 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2481 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2482 return E_INVALIDARG;
2485 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2486 if ( FAILED( r ) )
2487 return r;
2489 if ( This->sComponent )
2491 path = shelllink_get_msi_component_path( This->sComponent );
2492 if (!path)
2493 return E_FAIL;
2495 else
2496 path = strdupW( This->sPath );
2498 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2499 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2501 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2502 DWORD len = 2;
2504 if ( This->sArgs )
2505 len += lstrlenW( This->sArgs );
2506 if ( iciex->lpParametersW )
2507 len += lstrlenW( iciex->lpParametersW );
2509 args = heap_alloc( len*sizeof(WCHAR) );
2510 args[0] = 0;
2511 if ( This->sArgs )
2512 lstrcatW( args, This->sArgs );
2513 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2515 static const WCHAR space[] = { ' ', 0 };
2516 lstrcatW( args, space );
2517 lstrcatW( args, iciex->lpParametersW );
2521 memset( &sei, 0, sizeof sei );
2522 sei.cbSize = sizeof sei;
2523 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2524 sei.lpFile = path;
2525 sei.nShow = This->iShowCmd;
2526 sei.lpIDList = This->pPidl;
2527 sei.lpDirectory = This->sWorkDir;
2528 sei.lpParameters = args;
2529 sei.lpVerb = szOpen;
2531 if( ShellExecuteExW( &sei ) )
2532 r = S_OK;
2533 else
2534 r = E_FAIL;
2536 heap_free( args );
2537 heap_free( path );
2539 return r;
2542 static HRESULT WINAPI
2543 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2544 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2546 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2548 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2549 idCmd, uType, pwReserved, pszName, cchMax );
2551 return E_NOTIMPL;
2554 static const IContextMenuVtbl cmvt =
2556 ShellLink_ContextMenu_QueryInterface,
2557 ShellLink_ContextMenu_AddRef,
2558 ShellLink_ContextMenu_Release,
2559 ShellLink_QueryContextMenu,
2560 ShellLink_InvokeCommand,
2561 ShellLink_GetCommandString
2564 static HRESULT WINAPI
2565 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2567 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2568 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2571 static ULONG WINAPI
2572 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2574 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2575 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2578 static ULONG WINAPI
2579 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2581 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2582 return IShellLinkW_Release(&This->IShellLinkW_iface);
2585 static HRESULT WINAPI
2586 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2588 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2590 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2592 if ( !This->site )
2593 return E_FAIL;
2594 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2597 static HRESULT WINAPI
2598 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2600 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2602 TRACE("%p %p\n", iface, punk);
2604 if ( punk )
2605 IUnknown_AddRef( punk );
2607 if( This->site )
2608 IUnknown_Release( This->site );
2610 This->site = punk;
2612 return S_OK;
2615 static const IObjectWithSiteVtbl owsvt =
2617 ShellLink_ObjectWithSite_QueryInterface,
2618 ShellLink_ObjectWithSite_AddRef,
2619 ShellLink_ObjectWithSite_Release,
2620 ShellLink_SetSite,
2621 ShellLink_GetSite,
2624 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2626 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2627 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2630 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2632 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2633 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2636 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2638 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2639 return IShellLinkW_Release(&This->IShellLinkW_iface);
2642 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2644 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2645 FIXME("(%p)->(%p): stub\n", This, props);
2646 return E_NOTIMPL;
2649 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2651 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2652 FIXME("(%p)->(%d %p): stub\n", This, propid, key);
2653 return E_NOTIMPL;
2656 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2658 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2659 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2660 return E_NOTIMPL;
2663 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2665 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2666 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2667 return S_OK;
2670 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2672 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2673 FIXME("(%p): stub\n", This);
2674 return S_OK;
2677 static const IPropertyStoreVtbl propertystorevtbl = {
2678 propertystore_QueryInterface,
2679 propertystore_AddRef,
2680 propertystore_Release,
2681 propertystore_GetCount,
2682 propertystore_GetAt,
2683 propertystore_GetValue,
2684 propertystore_SetValue,
2685 propertystore_Commit
2688 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2690 IShellLinkImpl * sl;
2691 HRESULT r;
2693 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2695 *obj = NULL;
2697 if (outer)
2698 return CLASS_E_NOAGGREGATION;
2700 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2701 if (!sl)
2702 return E_OUTOFMEMORY;
2704 sl->ref = 1;
2705 sl->IShellLinkA_iface.lpVtbl = &slvt;
2706 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2707 sl->IPersistFile_iface.lpVtbl = &pfvt;
2708 sl->IPersistStream_iface.lpVtbl = &psvt;
2709 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2710 sl->IShellExtInit_iface.lpVtbl = &eivt;
2711 sl->IContextMenu_iface.lpVtbl = &cmvt;
2712 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2713 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2714 sl->iShowCmd = SW_SHOWNORMAL;
2715 sl->bDirty = FALSE;
2716 sl->iIdOpen = -1;
2717 sl->site = NULL;
2718 sl->filepath = NULL;
2720 TRACE("(%p)\n", sl);
2722 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2723 IShellLinkW_Release( &sl->IShellLinkW_iface );
2724 return r;