user32/tests: Fix monitor test failures on some systems.
[wine.git] / dlls / shell32 / shelllink.c
blob9a61ea100d26f1dbf10370aa9287b6f2c8dfbe89
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 path, INT icon)
1469 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1470 WCHAR *pathW = NULL;
1471 HRESULT hr;
1473 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1475 if (path)
1477 pathW = heap_strdupAtoW(path);
1478 if (!pathW)
1479 return E_OUTOFMEMORY;
1482 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1483 heap_free(pathW);
1485 return hr;
1488 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1489 DWORD dwReserved)
1491 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1492 WCHAR *pathW;
1493 HRESULT hr;
1495 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1497 pathW = heap_strdupAtoW(pszPathRel);
1498 if (!pathW) return E_OUTOFMEMORY;
1500 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1501 heap_free(pathW);
1503 return hr;
1506 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1508 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1510 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1512 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1515 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1517 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1518 HRESULT r;
1519 LPWSTR str;
1521 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1523 if (!pszFile) return E_INVALIDARG;
1525 str = heap_strdupAtoW(pszFile);
1526 if( !str )
1527 return E_OUTOFMEMORY;
1529 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1530 heap_free( str );
1532 return r;
1535 /**************************************************************************
1536 * IShellLink Implementation
1539 static const IShellLinkAVtbl slvt =
1541 IShellLinkA_fnQueryInterface,
1542 IShellLinkA_fnAddRef,
1543 IShellLinkA_fnRelease,
1544 IShellLinkA_fnGetPath,
1545 IShellLinkA_fnGetIDList,
1546 IShellLinkA_fnSetIDList,
1547 IShellLinkA_fnGetDescription,
1548 IShellLinkA_fnSetDescription,
1549 IShellLinkA_fnGetWorkingDirectory,
1550 IShellLinkA_fnSetWorkingDirectory,
1551 IShellLinkA_fnGetArguments,
1552 IShellLinkA_fnSetArguments,
1553 IShellLinkA_fnGetHotkey,
1554 IShellLinkA_fnSetHotkey,
1555 IShellLinkA_fnGetShowCmd,
1556 IShellLinkA_fnSetShowCmd,
1557 IShellLinkA_fnGetIconLocation,
1558 IShellLinkA_fnSetIconLocation,
1559 IShellLinkA_fnSetRelativePath,
1560 IShellLinkA_fnResolve,
1561 IShellLinkA_fnSetPath
1565 /**************************************************************************
1566 * IShellLinkW_fnQueryInterface
1568 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1569 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1571 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1573 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1575 *ppvObj = NULL;
1577 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1579 *ppvObj = &This->IShellLinkA_iface;
1581 else if(IsEqualIID(riid, &IID_IShellLinkW))
1583 *ppvObj = &This->IShellLinkW_iface;
1585 else if(IsEqualIID(riid, &IID_IPersistFile))
1587 *ppvObj = &This->IPersistFile_iface;
1589 else if(IsEqualIID(riid, &IID_IPersistStream))
1591 *ppvObj = &This->IPersistStream_iface;
1593 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1595 *ppvObj = &This->IShellLinkDataList_iface;
1597 else if(IsEqualIID(riid, &IID_IShellExtInit))
1599 *ppvObj = &This->IShellExtInit_iface;
1601 else if(IsEqualIID(riid, &IID_IContextMenu))
1603 *ppvObj = &This->IContextMenu_iface;
1605 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1607 *ppvObj = &This->IObjectWithSite_iface;
1609 else if(IsEqualIID(riid, &IID_IPropertyStore))
1611 *ppvObj = &This->IPropertyStore_iface;
1614 if(*ppvObj)
1616 IUnknown_AddRef((IUnknown*)*ppvObj);
1617 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1618 return S_OK;
1620 ERR("-- Interface: E_NOINTERFACE\n");
1621 return E_NOINTERFACE;
1624 /******************************************************************************
1625 * IShellLinkW_fnAddRef
1627 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1629 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1630 ULONG ref = InterlockedIncrement(&This->ref);
1632 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1634 return ref;
1637 /******************************************************************************
1638 * IShellLinkW_fnRelease
1640 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1642 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1643 ULONG refCount = InterlockedDecrement(&This->ref);
1645 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1647 if (refCount)
1648 return refCount;
1650 TRACE("-- destroying IShellLink(%p)\n",This);
1652 heap_free(This->sIcoPath);
1653 heap_free(This->sArgs);
1654 heap_free(This->sWorkDir);
1655 heap_free(This->sDescription);
1656 heap_free(This->sPath);
1657 heap_free(This->sPathRel);
1658 heap_free(This->sProduct);
1659 heap_free(This->sComponent);
1660 heap_free(This->filepath);
1662 if (This->site)
1663 IUnknown_Release( This->site );
1665 if (This->pPidl)
1666 ILFree(This->pPidl);
1668 LocalFree(This);
1670 return 0;
1673 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1675 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1676 HRESULT res = S_OK;
1678 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1679 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1681 if (This->sComponent || This->sProduct)
1682 return S_FALSE;
1684 if (cchMaxPath)
1685 pszFile[0] = 0;
1686 if (This->sPath)
1687 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1688 else
1689 res = S_FALSE;
1691 if (pfd)
1693 memset(pfd, 0, sizeof(*pfd));
1695 if (res == S_OK)
1697 WCHAR path[MAX_PATH];
1698 WIN32_FILE_ATTRIBUTE_DATA fad;
1700 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1702 pfd->dwFileAttributes = fad.dwFileAttributes;
1703 pfd->ftCreationTime = fad.ftCreationTime;
1704 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1705 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1706 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1707 pfd->nFileSizeLow = fad.nFileSizeLow;
1710 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1712 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1714 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1718 TRACE("attr 0x%08x size 0x%08x%08x name %s shortname %s\n", pfd->dwFileAttributes,
1719 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1720 wine_dbgstr_w(pfd->cAlternateFileName));
1723 return res;
1726 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1728 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1730 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1732 if (!This->pPidl)
1734 *ppidl = NULL;
1735 return S_FALSE;
1737 *ppidl = ILClone(This->pPidl);
1738 return S_OK;
1741 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1743 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1744 WCHAR path[MAX_PATH];
1746 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1748 if( This->pPidl )
1749 ILFree( This->pPidl );
1750 This->pPidl = ILClone( pidl );
1751 if( !This->pPidl )
1752 return E_FAIL;
1754 heap_free( This->sPath );
1755 This->sPath = NULL;
1757 if ( SHGetPathFromIDListW( pidl, path ) )
1759 This->sPath = heap_alloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1760 if (!This->sPath)
1761 return E_OUTOFMEMORY;
1763 lstrcpyW(This->sPath, path);
1766 This->bDirty = TRUE;
1768 return S_OK;
1771 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1773 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1775 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1777 pszName[0] = 0;
1778 if( This->sDescription )
1779 lstrcpynW( pszName, This->sDescription, cchMaxName );
1781 return S_OK;
1784 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1786 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1788 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1790 heap_free(This->sDescription);
1791 if (pszName)
1793 This->sDescription = heap_alloc((lstrlenW( pszName )+1)*sizeof(WCHAR) );
1794 if ( !This->sDescription )
1795 return E_OUTOFMEMORY;
1797 lstrcpyW( This->sDescription, pszName );
1799 else
1800 This->sDescription = NULL;
1801 This->bDirty = TRUE;
1803 return S_OK;
1806 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1808 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1810 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1812 if( cchMaxPath )
1813 pszDir[0] = 0;
1814 if( This->sWorkDir )
1815 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1817 return S_OK;
1820 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1822 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1824 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1826 heap_free(This->sWorkDir);
1827 This->sWorkDir = heap_alloc((lstrlenW( pszDir ) + 1) * sizeof (WCHAR) );
1828 if ( !This->sWorkDir )
1829 return E_OUTOFMEMORY;
1830 lstrcpyW( This->sWorkDir, pszDir );
1831 This->bDirty = TRUE;
1833 return S_OK;
1836 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1838 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1840 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1842 if( cchMaxPath )
1843 pszArgs[0] = 0;
1844 if( This->sArgs )
1845 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1847 return S_OK;
1850 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1852 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1854 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1856 heap_free(This->sArgs);
1857 if (pszArgs)
1859 This->sArgs = heap_alloc((lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1860 if ( !This->sArgs )
1861 return E_OUTOFMEMORY;
1862 lstrcpyW( This->sArgs, pszArgs );
1864 else This->sArgs = NULL;
1866 This->bDirty = TRUE;
1868 return S_OK;
1871 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1873 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1875 TRACE("(%p)->(%p)\n",This, pwHotkey);
1877 *pwHotkey=This->wHotKey;
1879 return S_OK;
1882 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1884 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1886 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1888 This->wHotKey = wHotkey;
1889 This->bDirty = TRUE;
1891 return S_OK;
1894 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1896 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1898 TRACE("(%p)->(%p)\n",This, piShowCmd);
1900 *piShowCmd = This->iShowCmd;
1902 return S_OK;
1905 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1907 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1909 TRACE("(%p)->(%d)\n", This, iShowCmd);
1911 This->iShowCmd = iShowCmd;
1912 This->bDirty = TRUE;
1914 return S_OK;
1917 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1919 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1921 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1923 *piIcon = This->iIcoNdx;
1925 if (This->sIcoPath)
1926 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1927 else
1928 pszIconPath[0] = 0;
1930 return S_OK;
1933 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1935 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1937 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1939 heap_free(This->sIcoPath);
1940 if (path)
1942 size_t len = (strlenW(path) + 1) * sizeof(WCHAR);
1943 This->sIcoPath = heap_alloc(len);
1944 if (!This->sIcoPath)
1945 return E_OUTOFMEMORY;
1946 memcpy(This->sIcoPath, path, len);
1948 else
1949 This->sIcoPath = NULL;
1950 This->iIcoNdx = icon;
1951 This->bDirty = TRUE;
1953 return S_OK;
1956 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1958 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1960 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1962 heap_free(This->sPathRel);
1963 This->sPathRel = heap_alloc((lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1964 if ( !This->sPathRel )
1965 return E_OUTOFMEMORY;
1966 lstrcpyW( This->sPathRel, pszPathRel );
1967 This->bDirty = TRUE;
1969 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1972 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1974 HRESULT hr = S_OK;
1975 BOOL bSuccess;
1977 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1979 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1981 /*FIXME: use IResolveShellLink interface */
1983 if (!This->sPath && This->pPidl) {
1984 WCHAR buffer[MAX_PATH];
1986 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1988 if (bSuccess && *buffer) {
1989 This->sPath = heap_alloc((lstrlenW(buffer)+1)*sizeof(WCHAR));
1990 if (!This->sPath)
1991 return E_OUTOFMEMORY;
1993 lstrcpyW(This->sPath, buffer);
1995 This->bDirty = TRUE;
1996 } else
1997 hr = S_OK; /* don't report an error occurred while just caching information */
2000 if (!This->sIcoPath && This->sPath) {
2001 This->sIcoPath = heap_alloc((lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2002 if (!This->sIcoPath)
2003 return E_OUTOFMEMORY;
2005 lstrcpyW(This->sIcoPath, This->sPath);
2006 This->iIcoNdx = 0;
2008 This->bDirty = TRUE;
2011 return hr;
2014 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2016 LPWSTR ret;
2017 LPCWSTR p;
2018 DWORD len;
2020 if( !str )
2021 return NULL;
2023 p = strchrW( str, ':' );
2024 if( !p )
2025 return NULL;
2026 len = p - str;
2027 ret = heap_alloc(sizeof(WCHAR)*(len+1));
2028 if( !ret )
2029 return ret;
2030 memcpy( ret, str, sizeof(WCHAR)*len );
2031 ret[len] = 0;
2032 return ret;
2035 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2037 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2038 WCHAR szGuid[39];
2039 HRESULT r;
2040 GUID guid;
2041 int len;
2043 while( str[0] )
2045 /* each segment must start with two colons */
2046 if( str[0] != ':' || str[1] != ':' )
2047 return E_FAIL;
2049 /* the last segment is just two colons */
2050 if( !str[2] )
2051 break;
2052 str += 2;
2054 /* there must be a colon straight after a guid */
2055 p = strchrW( str, ':' );
2056 if( !p )
2057 return E_FAIL;
2058 len = p - str;
2059 if( len != 38 )
2060 return E_FAIL;
2062 /* get the guid, and check it's validly formatted */
2063 memcpy( szGuid, str, sizeof(WCHAR)*len );
2064 szGuid[len] = 0;
2065 r = CLSIDFromString( szGuid, &guid );
2066 if( r != S_OK )
2067 return r;
2068 str = p + 1;
2070 /* match it up to a guid that we care about */
2071 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2072 szComponent = str;
2073 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2074 szProduct = str;
2075 else
2076 return E_FAIL;
2078 /* skip to the next field */
2079 str = strchrW( str, ':' );
2080 if( !str )
2081 return E_FAIL;
2084 /* we have to have a component for an advertised shortcut */
2085 if( !szComponent )
2086 return E_FAIL;
2088 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2089 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2091 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2092 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2094 return S_OK;
2097 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2099 const int label_sz = ARRAY_SIZE(volume->label);
2100 WCHAR drive[] = { path[0], ':', '\\', 0 };
2101 BOOL r;
2103 volume->type = GetDriveTypeW(drive);
2104 r = GetVolumeInformationW(drive, volume->label, label_sz,
2105 &volume->serial, NULL, NULL, NULL, 0);
2106 TRACE("r = %d type %d serial %08x name %s\n", r,
2107 volume->type, volume->serial, debugstr_w(volume->label));
2108 return r;
2111 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2113 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2114 WCHAR buffer[MAX_PATH];
2115 LPWSTR fname, unquoted = NULL;
2116 HRESULT hr = S_OK;
2117 UINT len;
2119 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2121 if (!pszFile) return E_INVALIDARG;
2123 /* quotes at the ends of the string are stripped */
2124 len = lstrlenW(pszFile);
2125 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2127 unquoted = strdupW(pszFile);
2128 PathUnquoteSpacesW(unquoted);
2129 pszFile = unquoted;
2132 /* any other quote marks are invalid */
2133 if (strchrW(pszFile, '"'))
2135 heap_free(unquoted);
2136 return S_FALSE;
2139 heap_free(This->sPath);
2140 This->sPath = NULL;
2142 heap_free(This->sComponent);
2143 This->sComponent = NULL;
2145 if (This->pPidl)
2146 ILFree(This->pPidl);
2147 This->pPidl = NULL;
2149 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2151 if (*pszFile == '\0')
2152 *buffer = '\0';
2153 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2154 return E_FAIL;
2155 else if(!PathFileExistsW(buffer) &&
2156 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2157 hr = S_FALSE;
2159 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2160 ShellLink_GetVolumeInfo(buffer, &This->volume);
2162 This->sPath = heap_alloc( (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2163 if (!This->sPath)
2165 heap_free(unquoted);
2166 return E_OUTOFMEMORY;
2169 lstrcpyW(This->sPath, buffer);
2171 This->bDirty = TRUE;
2172 heap_free(unquoted);
2174 return hr;
2177 /**************************************************************************
2178 * IShellLinkW Implementation
2181 static const IShellLinkWVtbl slvtw =
2183 IShellLinkW_fnQueryInterface,
2184 IShellLinkW_fnAddRef,
2185 IShellLinkW_fnRelease,
2186 IShellLinkW_fnGetPath,
2187 IShellLinkW_fnGetIDList,
2188 IShellLinkW_fnSetIDList,
2189 IShellLinkW_fnGetDescription,
2190 IShellLinkW_fnSetDescription,
2191 IShellLinkW_fnGetWorkingDirectory,
2192 IShellLinkW_fnSetWorkingDirectory,
2193 IShellLinkW_fnGetArguments,
2194 IShellLinkW_fnSetArguments,
2195 IShellLinkW_fnGetHotkey,
2196 IShellLinkW_fnSetHotkey,
2197 IShellLinkW_fnGetShowCmd,
2198 IShellLinkW_fnSetShowCmd,
2199 IShellLinkW_fnGetIconLocation,
2200 IShellLinkW_fnSetIconLocation,
2201 IShellLinkW_fnSetRelativePath,
2202 IShellLinkW_fnResolve,
2203 IShellLinkW_fnSetPath
2206 static HRESULT WINAPI
2207 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2209 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2210 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2213 static ULONG WINAPI
2214 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2216 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2217 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2220 static ULONG WINAPI
2221 ShellLink_DataList_Release( IShellLinkDataList* iface )
2223 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2224 return IShellLinkW_Release(&This->IShellLinkW_iface);
2227 static HRESULT WINAPI
2228 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2230 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2231 return E_NOTIMPL;
2234 static HRESULT WINAPI
2235 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2237 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2238 LPVOID block = NULL;
2239 HRESULT r = E_FAIL;
2241 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2243 switch (dwSig)
2245 case EXP_DARWIN_ID_SIG:
2246 if (!This->sComponent)
2247 break;
2248 block = shelllink_build_darwinid( This->sComponent, dwSig );
2249 r = S_OK;
2250 break;
2251 case EXP_SZ_LINK_SIG:
2252 case NT_CONSOLE_PROPS_SIG:
2253 case NT_FE_CONSOLE_PROPS_SIG:
2254 case EXP_SPECIAL_FOLDER_SIG:
2255 case EXP_SZ_ICON_SIG:
2256 FIXME("valid but unhandled datablock %08x\n", dwSig);
2257 break;
2258 default:
2259 ERR("unknown datablock %08x\n", dwSig);
2261 *ppDataBlock = block;
2262 return r;
2265 static HRESULT WINAPI
2266 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2268 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2269 return E_NOTIMPL;
2272 static HRESULT WINAPI
2273 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2275 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2276 DWORD flags = 0;
2278 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2280 /* FIXME: add more */
2281 if (This->sArgs)
2282 flags |= SLDF_HAS_ARGS;
2283 if (This->sComponent)
2284 flags |= SLDF_HAS_DARWINID;
2285 if (This->sIcoPath)
2286 flags |= SLDF_HAS_ICONLOCATION;
2287 if (This->sProduct)
2288 flags |= SLDF_HAS_LOGO3ID;
2289 if (This->pPidl)
2290 flags |= SLDF_HAS_ID_LIST;
2292 *pdwFlags = flags;
2294 return S_OK;
2297 static HRESULT WINAPI
2298 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2300 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2301 return E_NOTIMPL;
2304 static const IShellLinkDataListVtbl dlvt =
2306 ShellLink_DataList_QueryInterface,
2307 ShellLink_DataList_AddRef,
2308 ShellLink_DataList_Release,
2309 ShellLink_AddDataBlock,
2310 ShellLink_CopyDataBlock,
2311 ShellLink_RemoveDataBlock,
2312 ShellLink_GetFlags,
2313 ShellLink_SetFlags
2316 static HRESULT WINAPI
2317 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2319 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2320 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2323 static ULONG WINAPI
2324 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2326 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2327 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2330 static ULONG WINAPI
2331 ShellLink_ExtInit_Release( IShellExtInit* iface )
2333 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2334 return IShellLinkW_Release(&This->IShellLinkW_iface);
2337 /**************************************************************************
2338 * ShellLink implementation of IShellExtInit::Initialize()
2340 * Loads the shelllink from the dataobject the shell is pointing to.
2342 static HRESULT WINAPI
2343 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2344 IDataObject *pdtobj, HKEY hkeyProgID )
2346 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2347 FORMATETC format;
2348 STGMEDIUM stgm;
2349 UINT count;
2350 HRESULT r = E_FAIL;
2352 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2354 if( !pdtobj )
2355 return r;
2357 format.cfFormat = CF_HDROP;
2358 format.ptd = NULL;
2359 format.dwAspect = DVASPECT_CONTENT;
2360 format.lindex = -1;
2361 format.tymed = TYMED_HGLOBAL;
2363 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2364 return r;
2366 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2367 if( count == 1 )
2369 LPWSTR path;
2371 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2372 count++;
2373 path = heap_alloc(count*sizeof(WCHAR) );
2374 if( path )
2376 IPersistFile *pf = &This->IPersistFile_iface;
2378 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2379 r = IPersistFile_Load( pf, path, 0 );
2380 heap_free( path );
2383 ReleaseStgMedium( &stgm );
2385 return r;
2388 static const IShellExtInitVtbl eivt =
2390 ShellLink_ExtInit_QueryInterface,
2391 ShellLink_ExtInit_AddRef,
2392 ShellLink_ExtInit_Release,
2393 ShellLink_ExtInit_Initialize
2396 static HRESULT WINAPI
2397 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2399 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2400 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2403 static ULONG WINAPI
2404 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2406 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2407 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2410 static ULONG WINAPI
2411 ShellLink_ContextMenu_Release( IContextMenu* iface )
2413 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2414 return IShellLinkW_Release(&This->IShellLinkW_iface);
2417 static HRESULT WINAPI
2418 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2419 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2421 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2422 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2423 MENUITEMINFOW mii;
2424 int id = 1;
2426 TRACE("%p %p %u %u %u %u\n", This,
2427 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2429 if ( !hmenu )
2430 return E_INVALIDARG;
2432 memset( &mii, 0, sizeof mii );
2433 mii.cbSize = sizeof mii;
2434 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2435 mii.dwTypeData = szOpen;
2436 mii.cch = strlenW( mii.dwTypeData );
2437 mii.wID = idCmdFirst + id++;
2438 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2439 mii.fType = MFT_STRING;
2440 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2441 return E_FAIL;
2442 This->iIdOpen = 0;
2444 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2447 static LPWSTR
2448 shelllink_get_msi_component_path( LPWSTR component )
2450 LPWSTR path;
2451 DWORD r, sz = 0;
2453 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2454 if (r != ERROR_SUCCESS)
2455 return NULL;
2457 sz++;
2458 path = heap_alloc( sz*sizeof(WCHAR) );
2459 r = CommandLineFromMsiDescriptor( component, path, &sz );
2460 if (r != ERROR_SUCCESS)
2462 heap_free( path );
2463 path = NULL;
2466 TRACE("returning %s\n", debugstr_w( path ) );
2468 return path;
2471 static HRESULT WINAPI
2472 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2474 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2475 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2476 SHELLEXECUTEINFOW sei;
2477 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2478 LPWSTR args = NULL;
2479 LPWSTR path = NULL;
2480 HRESULT r;
2482 TRACE("%p %p\n", This, lpici );
2484 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2485 return E_INVALIDARG;
2487 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2489 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2490 return E_INVALIDARG;
2493 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2494 if ( FAILED( r ) )
2495 return r;
2497 if ( This->sComponent )
2499 path = shelllink_get_msi_component_path( This->sComponent );
2500 if (!path)
2501 return E_FAIL;
2503 else
2504 path = strdupW( This->sPath );
2506 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2507 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2509 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2510 DWORD len = 2;
2512 if ( This->sArgs )
2513 len += lstrlenW( This->sArgs );
2514 if ( iciex->lpParametersW )
2515 len += lstrlenW( iciex->lpParametersW );
2517 args = heap_alloc( len*sizeof(WCHAR) );
2518 args[0] = 0;
2519 if ( This->sArgs )
2520 lstrcatW( args, This->sArgs );
2521 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2523 static const WCHAR space[] = { ' ', 0 };
2524 lstrcatW( args, space );
2525 lstrcatW( args, iciex->lpParametersW );
2529 memset( &sei, 0, sizeof sei );
2530 sei.cbSize = sizeof sei;
2531 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2532 sei.lpFile = path;
2533 sei.nShow = This->iShowCmd;
2534 sei.lpIDList = This->pPidl;
2535 sei.lpDirectory = This->sWorkDir;
2536 sei.lpParameters = args;
2537 sei.lpVerb = szOpen;
2539 if( ShellExecuteExW( &sei ) )
2540 r = S_OK;
2541 else
2542 r = E_FAIL;
2544 heap_free( args );
2545 heap_free( path );
2547 return r;
2550 static HRESULT WINAPI
2551 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2552 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2554 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2556 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2557 idCmd, uType, pwReserved, pszName, cchMax );
2559 return E_NOTIMPL;
2562 static const IContextMenuVtbl cmvt =
2564 ShellLink_ContextMenu_QueryInterface,
2565 ShellLink_ContextMenu_AddRef,
2566 ShellLink_ContextMenu_Release,
2567 ShellLink_QueryContextMenu,
2568 ShellLink_InvokeCommand,
2569 ShellLink_GetCommandString
2572 static HRESULT WINAPI
2573 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2575 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2576 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2579 static ULONG WINAPI
2580 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2582 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2583 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2586 static ULONG WINAPI
2587 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2589 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2590 return IShellLinkW_Release(&This->IShellLinkW_iface);
2593 static HRESULT WINAPI
2594 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2596 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2598 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2600 if ( !This->site )
2601 return E_FAIL;
2602 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2605 static HRESULT WINAPI
2606 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2608 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2610 TRACE("%p %p\n", iface, punk);
2612 if ( punk )
2613 IUnknown_AddRef( punk );
2615 if( This->site )
2616 IUnknown_Release( This->site );
2618 This->site = punk;
2620 return S_OK;
2623 static const IObjectWithSiteVtbl owsvt =
2625 ShellLink_ObjectWithSite_QueryInterface,
2626 ShellLink_ObjectWithSite_AddRef,
2627 ShellLink_ObjectWithSite_Release,
2628 ShellLink_SetSite,
2629 ShellLink_GetSite,
2632 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2634 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2635 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2638 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2640 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2641 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2644 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2646 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2647 return IShellLinkW_Release(&This->IShellLinkW_iface);
2650 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2652 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2653 FIXME("(%p)->(%p): stub\n", This, props);
2654 return E_NOTIMPL;
2657 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2659 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2660 FIXME("(%p)->(%d %p): stub\n", This, propid, key);
2661 return E_NOTIMPL;
2664 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2666 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2667 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2668 return E_NOTIMPL;
2671 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2673 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2674 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2675 return S_OK;
2678 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2680 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2681 FIXME("(%p): stub\n", This);
2682 return S_OK;
2685 static const IPropertyStoreVtbl propertystorevtbl = {
2686 propertystore_QueryInterface,
2687 propertystore_AddRef,
2688 propertystore_Release,
2689 propertystore_GetCount,
2690 propertystore_GetAt,
2691 propertystore_GetValue,
2692 propertystore_SetValue,
2693 propertystore_Commit
2696 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2698 IShellLinkImpl * sl;
2699 HRESULT r;
2701 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2703 *obj = NULL;
2705 if (outer)
2706 return CLASS_E_NOAGGREGATION;
2708 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2709 if (!sl)
2710 return E_OUTOFMEMORY;
2712 sl->ref = 1;
2713 sl->IShellLinkA_iface.lpVtbl = &slvt;
2714 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2715 sl->IPersistFile_iface.lpVtbl = &pfvt;
2716 sl->IPersistStream_iface.lpVtbl = &psvt;
2717 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2718 sl->IShellExtInit_iface.lpVtbl = &eivt;
2719 sl->IContextMenu_iface.lpVtbl = &cmvt;
2720 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2721 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2722 sl->iShowCmd = SW_SHOWNORMAL;
2723 sl->bDirty = FALSE;
2724 sl->iIdOpen = -1;
2725 sl->site = NULL;
2726 sl->filepath = NULL;
2728 TRACE("(%p)\n", sl);
2730 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2731 IShellLinkW_Release( &sl->IShellLinkW_iface );
2732 return r;