d3d11: Implement d3d11_texture3d_GetDevice().
[wine.git] / dlls / shell32 / shelllink.c
blobbd3507e3e1515223ce8d6c3a255422650273df8a
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 = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
212 if( !p )
213 return p;
214 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
215 return p;
218 static inline LPWSTR strdupW( LPCWSTR src )
220 LPWSTR dest;
221 if (!src) return NULL;
222 dest = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(src)+1)*sizeof(WCHAR) );
223 if (dest)
224 lstrcpyW(dest, src);
225 return dest;
228 /**************************************************************************
229 * IPersistFile_QueryInterface
231 static HRESULT WINAPI IPersistFile_fnQueryInterface(
232 IPersistFile* iface,
233 REFIID riid,
234 LPVOID *ppvObj)
236 IShellLinkImpl *This = impl_from_IPersistFile(iface);
237 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
240 /******************************************************************************
241 * IPersistFile_AddRef
243 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
245 IShellLinkImpl *This = impl_from_IPersistFile(iface);
246 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
249 /******************************************************************************
250 * IPersistFile_Release
252 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
254 IShellLinkImpl *This = impl_from_IPersistFile(iface);
255 return IShellLinkW_Release(&This->IShellLinkW_iface);
258 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
260 IShellLinkImpl *This = impl_from_IPersistFile(iface);
262 TRACE("(%p)->(%p)\n", This, pClassID);
264 *pClassID = CLSID_ShellLink;
266 return S_OK;
269 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
271 IShellLinkImpl *This = impl_from_IPersistFile(iface);
273 TRACE("(%p)\n",This);
275 if (This->bDirty)
276 return S_OK;
278 return S_FALSE;
281 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
283 IShellLinkImpl *This = impl_from_IPersistFile(iface);
284 IPersistStream *StreamThis = &This->IPersistStream_iface;
285 HRESULT r;
286 IStream *stm;
288 TRACE("(%p, %s, %x)\n",This, debugstr_w(pszFileName), dwMode);
290 if( dwMode == 0 )
291 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
292 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
293 if( SUCCEEDED( r ) )
295 r = IPersistStream_Load(StreamThis, stm);
296 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
297 IStream_Release( stm );
299 /* update file path */
300 HeapFree(GetProcessHeap(), 0, This->filepath);
301 This->filepath = strdupW(pszFileName);
303 This->bDirty = FALSE;
305 TRACE("-- returning hr %08x\n", r);
306 return r;
309 BOOL run_winemenubuilder( const WCHAR *args )
311 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
312 LONG len;
313 LPWSTR buffer;
314 STARTUPINFOW si;
315 PROCESS_INFORMATION pi;
316 BOOL ret;
317 WCHAR app[MAX_PATH];
318 void *redir;
320 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
321 strcatW( app, menubuilder );
323 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
324 buffer = HeapAlloc( GetProcessHeap(), 0, len );
325 if( !buffer )
326 return FALSE;
328 strcpyW( buffer, app );
329 strcatW( buffer, args );
331 TRACE("starting %s\n",debugstr_w(buffer));
333 memset(&si, 0, sizeof(si));
334 si.cb = sizeof(si);
336 Wow64DisableWow64FsRedirection( &redir );
337 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
338 Wow64RevertWow64FsRedirection( redir );
340 HeapFree( GetProcessHeap(), 0, buffer );
342 if (ret)
344 CloseHandle( pi.hProcess );
345 CloseHandle( pi.hThread );
348 return ret;
351 static BOOL StartLinkProcessor( LPCOLESTR szLink )
353 static const WCHAR szFormat[] = {' ','-','w',' ','"','%','s','"',0 };
354 LONG len;
355 LPWSTR buffer;
356 BOOL ret;
358 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
359 buffer = HeapAlloc( GetProcessHeap(), 0, len );
360 if( !buffer )
361 return FALSE;
363 wsprintfW( buffer, szFormat, szLink );
364 ret = run_winemenubuilder( buffer );
365 HeapFree( GetProcessHeap(), 0, buffer );
366 return ret;
369 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
371 IShellLinkImpl *This = impl_from_IPersistFile(iface);
372 IPersistStream *StreamThis = &This->IPersistStream_iface;
373 HRESULT r;
374 IStream *stm;
376 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
378 if (!pszFileName)
379 return E_FAIL;
381 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
382 if( SUCCEEDED( r ) )
384 r = IPersistStream_Save(StreamThis, stm, FALSE);
385 IStream_Release( stm );
387 if( SUCCEEDED( r ) )
389 StartLinkProcessor( pszFileName );
391 /* update file path */
392 HeapFree(GetProcessHeap(), 0, This->filepath);
393 This->filepath = strdupW(pszFileName);
395 This->bDirty = FALSE;
397 else
399 DeleteFileW( pszFileName );
400 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
404 return r;
407 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
409 IShellLinkImpl *This = impl_from_IPersistFile(iface);
410 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
411 return S_OK;
414 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
416 IShellLinkImpl *This = impl_from_IPersistFile(iface);
417 IMalloc *pMalloc;
419 TRACE("(%p)->(%p)\n", This, filename);
421 if (!This->filepath)
423 *filename = NULL;
424 return S_FALSE;
427 SHGetMalloc(&pMalloc);
428 *filename = IMalloc_Alloc(pMalloc, (strlenW(This->filepath)+1)*sizeof(WCHAR));
429 if (!*filename) return E_OUTOFMEMORY;
431 strcpyW(*filename, This->filepath);
433 return S_OK;
436 static const IPersistFileVtbl pfvt =
438 IPersistFile_fnQueryInterface,
439 IPersistFile_fnAddRef,
440 IPersistFile_fnRelease,
441 IPersistFile_fnGetClassID,
442 IPersistFile_fnIsDirty,
443 IPersistFile_fnLoad,
444 IPersistFile_fnSave,
445 IPersistFile_fnSaveCompleted,
446 IPersistFile_fnGetCurFile
449 /************************************************************************
450 * IPersistStream_QueryInterface
452 static HRESULT WINAPI IPersistStream_fnQueryInterface(
453 IPersistStream* iface,
454 REFIID riid,
455 VOID** ppvObj)
457 IShellLinkImpl *This = impl_from_IPersistStream(iface);
458 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
461 /************************************************************************
462 * IPersistStream_Release
464 static ULONG WINAPI IPersistStream_fnRelease(
465 IPersistStream* iface)
467 IShellLinkImpl *This = impl_from_IPersistStream(iface);
468 return IShellLinkW_Release(&This->IShellLinkW_iface);
471 /************************************************************************
472 * IPersistStream_AddRef
474 static ULONG WINAPI IPersistStream_fnAddRef(
475 IPersistStream* iface)
477 IShellLinkImpl *This = impl_from_IPersistStream(iface);
478 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
481 /************************************************************************
482 * IPersistStream_GetClassID
485 static HRESULT WINAPI IPersistStream_fnGetClassID(
486 IPersistStream* iface,
487 CLSID* pClassID)
489 IShellLinkImpl *This = impl_from_IPersistStream(iface);
490 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
493 /************************************************************************
494 * IPersistStream_IsDirty (IPersistStream)
496 static HRESULT WINAPI IPersistStream_fnIsDirty(
497 IPersistStream* iface)
499 IShellLinkImpl *This = impl_from_IPersistStream(iface);
501 TRACE("(%p)\n", This);
503 return S_OK;
507 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
509 DWORD count;
510 USHORT len;
511 LPVOID temp;
512 LPWSTR str;
513 HRESULT r;
515 TRACE("%p\n", stm);
517 count = 0;
518 r = IStream_Read(stm, &len, sizeof(len), &count);
519 if ( FAILED (r) || ( count != sizeof(len) ) )
520 return E_FAIL;
522 if( unicode )
523 len *= sizeof (WCHAR);
525 TRACE("reading %d\n", len);
526 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
527 if( !temp )
528 return E_OUTOFMEMORY;
529 count = 0;
530 r = IStream_Read(stm, temp, len, &count);
531 if( FAILED (r) || ( count != len ) )
533 HeapFree( GetProcessHeap(), 0, temp );
534 return E_FAIL;
537 TRACE("read %s\n", debugstr_an(temp,len));
539 /* convert to unicode if necessary */
540 if( !unicode )
542 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
543 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
544 if( !str )
546 HeapFree( GetProcessHeap(), 0, temp );
547 return E_OUTOFMEMORY;
549 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
550 HeapFree( GetProcessHeap(), 0, temp );
552 else
554 count /= 2;
555 str = temp;
557 str[count] = 0;
559 *pstr = str;
561 return S_OK;
564 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
566 DWORD size;
567 ULONG count;
568 HRESULT r;
569 struct sized_chunk {
570 DWORD size;
571 unsigned char data[1];
572 } *chunk;
574 TRACE("%p\n",stm);
576 r = IStream_Read( stm, &size, sizeof(size), &count );
577 if( FAILED( r ) || count != sizeof(size) )
578 return E_FAIL;
580 chunk = HeapAlloc( GetProcessHeap(), 0, size );
581 if( !chunk )
582 return E_OUTOFMEMORY;
584 chunk->size = size;
585 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
586 if( FAILED( r ) || count != (size - sizeof(size)) )
588 HeapFree( GetProcessHeap(), 0, chunk );
589 return E_FAIL;
592 TRACE("Read %d bytes\n",chunk->size);
594 *data = chunk;
596 return S_OK;
599 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
601 const int label_sz = sizeof volume->label/sizeof volume->label[0];
602 LPSTR label;
603 int len;
605 volume->serial = vol->dwVolSerial;
606 volume->type = vol->dwType;
608 if( !vol->dwVolLabelOfs )
609 return FALSE;
610 if( vol->dwSize <= vol->dwVolLabelOfs )
611 return FALSE;
612 len = vol->dwSize - vol->dwVolLabelOfs;
614 label = (LPSTR) vol;
615 label += vol->dwVolLabelOfs;
616 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
618 return TRUE;
621 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
623 int len = 0, wlen;
624 LPWSTR path;
626 while( p[len] && (len < maxlen) )
627 len++;
629 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
630 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
631 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
632 path[wlen] = 0;
634 return path;
637 static HRESULT Stream_LoadLocation( IStream *stm,
638 volume_info *volume, LPWSTR *path )
640 char *p = NULL;
641 LOCATION_INFO *loc;
642 HRESULT r;
643 DWORD n;
645 r = Stream_ReadChunk( stm, (LPVOID*) &p );
646 if( FAILED(r) )
647 return r;
649 loc = (LOCATION_INFO*) p;
650 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
652 HeapFree( GetProcessHeap(), 0, p );
653 return E_FAIL;
656 /* if there's valid local volume information, load it */
657 if( loc->dwVolTableOfs &&
658 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
660 LOCAL_VOLUME_INFO *volume_info;
662 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
663 Stream_LoadVolume( volume_info, volume );
666 /* if there's a local path, load it */
667 n = loc->dwLocalPathOfs;
668 if( n && (n < loc->dwTotalSize) )
669 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
671 TRACE("type %d serial %08x name %s path %s\n", volume->type,
672 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
674 HeapFree( GetProcessHeap(), 0, p );
675 return S_OK;
679 * The format of the advertised shortcut info seems to be:
681 * Offset Description
682 * ------ -----------
684 * 0 Length of the block (4 bytes, usually 0x314)
685 * 4 tag (dword)
686 * 8 string data in ASCII
687 * 8+0x104 string data in UNICODE
689 * In the original Win32 implementation the buffers are not initialized
690 * to zero, so data trailing the string is random garbage.
692 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
694 DWORD size;
695 ULONG count;
696 HRESULT r;
697 EXP_DARWIN_LINK buffer;
699 TRACE("%p\n",stm);
701 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
702 if( FAILED( r ) )
703 return r;
705 /* make sure that we read the size of the structure even on error */
706 size = sizeof buffer - sizeof (DWORD);
707 if( buffer.dbh.cbSize != sizeof buffer )
709 ERR("Ooops. This structure is not as expected...\n");
710 return E_FAIL;
713 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
714 if( FAILED( r ) )
715 return r;
717 if( count != size )
718 return E_FAIL;
720 TRACE("magic %08x string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
722 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
724 ERR("Unknown magic number %08x in advertised shortcut\n", buffer.dbh.dwSignature);
725 return E_FAIL;
728 *str = HeapAlloc( GetProcessHeap(), 0,
729 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
730 lstrcpyW( *str, buffer.szwDarwinID );
732 return S_OK;
735 /************************************************************************
736 * IPersistStream_Load (IPersistStream)
738 static HRESULT WINAPI IPersistStream_fnLoad(
739 IPersistStream* iface,
740 IStream* stm)
742 LINK_HEADER hdr;
743 ULONG dwBytesRead;
744 BOOL unicode;
745 HRESULT r;
746 DWORD zero;
748 IShellLinkImpl *This = impl_from_IPersistStream(iface);
750 TRACE("%p %p\n", This, stm);
752 if( !stm )
753 return STG_E_INVALIDPOINTER;
755 dwBytesRead = 0;
756 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
757 if( FAILED( r ) )
758 return r;
760 if( dwBytesRead != sizeof(hdr))
761 return E_FAIL;
762 if( hdr.dwSize != sizeof(hdr))
763 return E_FAIL;
764 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
765 return E_FAIL;
767 /* free all the old stuff */
768 ILFree(This->pPidl);
769 This->pPidl = NULL;
770 memset( &This->volume, 0, sizeof This->volume );
771 HeapFree(GetProcessHeap(), 0, This->sPath);
772 This->sPath = NULL;
773 HeapFree(GetProcessHeap(), 0, This->sDescription);
774 This->sDescription = NULL;
775 HeapFree(GetProcessHeap(), 0, This->sPathRel);
776 This->sPathRel = NULL;
777 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
778 This->sWorkDir = NULL;
779 HeapFree(GetProcessHeap(), 0, This->sArgs);
780 This->sArgs = NULL;
781 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
782 This->sIcoPath = NULL;
783 HeapFree(GetProcessHeap(), 0, This->sProduct);
784 This->sProduct = NULL;
785 HeapFree(GetProcessHeap(), 0, This->sComponent);
786 This->sComponent = NULL;
788 This->wHotKey = (WORD)hdr.wHotKey;
789 This->iIcoNdx = hdr.nIcon;
790 FileTimeToSystemTime (&hdr.Time1, &This->time1);
791 FileTimeToSystemTime (&hdr.Time2, &This->time2);
792 FileTimeToSystemTime (&hdr.Time3, &This->time3);
793 if (TRACE_ON(shell))
795 WCHAR sTemp[MAX_PATH];
796 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
797 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
798 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
799 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
800 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
801 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
802 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
803 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
804 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
807 /* load all the new stuff */
808 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
810 r = ILLoadFromStream( stm, &This->pPidl );
811 if( FAILED( r ) )
812 return r;
814 pdump(This->pPidl);
816 /* load the location information */
817 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
818 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
819 if( FAILED( r ) )
820 goto end;
822 unicode = hdr.dwFlags & SLDF_UNICODE;
823 if( hdr.dwFlags & SLDF_HAS_NAME )
825 r = Stream_LoadString( stm, unicode, &This->sDescription );
826 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
828 if( FAILED( r ) )
829 goto end;
831 if( hdr.dwFlags & SLDF_HAS_RELPATH )
833 r = Stream_LoadString( stm, unicode, &This->sPathRel );
834 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
836 if( FAILED( r ) )
837 goto end;
839 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
841 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
842 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
844 if( FAILED( r ) )
845 goto end;
847 if( hdr.dwFlags & SLDF_HAS_ARGS )
849 r = Stream_LoadString( stm, unicode, &This->sArgs );
850 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
852 if( FAILED( r ) )
853 goto end;
855 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
857 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
858 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
860 if( FAILED( r ) )
861 goto end;
863 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
865 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
866 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
868 if( FAILED( r ) )
869 goto end;
871 if( hdr.dwFlags & SLDF_HAS_DARWINID )
873 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
874 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
876 if( FAILED( r ) )
877 goto end;
879 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
880 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
882 /* Some lnk files have extra data blocks starting with a
883 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
884 * one with a 0xa0000003 signature. However these don't seem to matter
885 * too much.
887 WARN("Last word was not zero\n");
890 TRACE("OK\n");
892 pdump (This->pPidl);
894 return S_OK;
895 end:
896 return r;
899 /************************************************************************
900 * Stream_WriteString
902 * Helper function for IPersistStream_Save. Writes a unicode string
903 * with terminating nul byte to a stream, preceded by the its length.
905 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
907 USHORT len = lstrlenW( str ) + 1;
908 DWORD count;
909 HRESULT r;
911 r = IStream_Write( stm, &len, sizeof(len), &count );
912 if( FAILED( r ) )
913 return r;
915 len *= sizeof(WCHAR);
917 r = IStream_Write( stm, str, len, &count );
918 if( FAILED( r ) )
919 return r;
921 return S_OK;
924 /************************************************************************
925 * Stream_WriteLocationInfo
927 * Writes the location info to a stream
929 * FIXME: One day we might want to write the network volume information
930 * and the final path.
931 * Figure out how Windows deals with unicode paths here.
933 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
934 volume_info *volume )
936 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
937 LOCAL_VOLUME_INFO *vol;
938 LOCATION_INFO *loc;
939 LPSTR szLabel, szPath, szFinalPath;
940 ULONG count = 0;
941 HRESULT hr;
943 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
945 /* figure out the size of everything */
946 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
947 NULL, 0, NULL, NULL );
948 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
949 NULL, 0, NULL, NULL );
950 volume_info_size = sizeof *vol + label_size;
951 final_path_size = 1;
952 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
954 /* create pointers to everything */
955 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
956 vol = (LOCAL_VOLUME_INFO*) &loc[1];
957 szLabel = (LPSTR) &vol[1];
958 szPath = &szLabel[label_size];
959 szFinalPath = &szPath[path_size];
961 /* fill in the location information header */
962 loc->dwTotalSize = total_size;
963 loc->dwHeaderSize = sizeof (*loc);
964 loc->dwFlags = 1;
965 loc->dwVolTableOfs = sizeof (*loc);
966 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
967 loc->dwNetworkVolTableOfs = 0;
968 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
970 /* fill in the volume information */
971 vol->dwSize = volume_info_size;
972 vol->dwType = volume->type;
973 vol->dwVolSerial = volume->serial;
974 vol->dwVolLabelOfs = sizeof (*vol);
976 /* copy in the strings */
977 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
978 szLabel, label_size, NULL, NULL );
979 WideCharToMultiByte( CP_ACP, 0, path, -1,
980 szPath, path_size, NULL, NULL );
981 szFinalPath[0] = 0;
983 hr = IStream_Write( stm, loc, total_size, &count );
984 HeapFree(GetProcessHeap(), 0, loc);
986 return hr;
989 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
991 EXP_DARWIN_LINK *buffer;
993 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
994 buffer->dbh.cbSize = sizeof *buffer;
995 buffer->dbh.dwSignature = magic;
996 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
997 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
999 return buffer;
1002 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
1004 EXP_DARWIN_LINK *buffer;
1005 ULONG count;
1007 TRACE("%p\n",stm);
1009 buffer = shelllink_build_darwinid( string, magic );
1011 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1014 /************************************************************************
1015 * IPersistStream_Save (IPersistStream)
1017 * FIXME: makes assumptions about byte order
1019 static HRESULT WINAPI IPersistStream_fnSave(
1020 IPersistStream* iface,
1021 IStream* stm,
1022 BOOL fClearDirty)
1024 LINK_HEADER header;
1025 ULONG count;
1026 DWORD zero;
1027 HRESULT r;
1029 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1031 TRACE("%p %p %x\n", This, stm, fClearDirty);
1033 memset(&header, 0, sizeof(header));
1034 header.dwSize = sizeof(header);
1035 header.fStartup = This->iShowCmd;
1036 header.MagicGuid = CLSID_ShellLink;
1038 header.wHotKey = This->wHotKey;
1039 header.nIcon = This->iIcoNdx;
1040 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1041 if( This->pPidl )
1042 header.dwFlags |= SLDF_HAS_ID_LIST;
1043 if( This->sPath )
1044 header.dwFlags |= SLDF_HAS_LINK_INFO;
1045 if( This->sDescription )
1046 header.dwFlags |= SLDF_HAS_NAME;
1047 if( This->sWorkDir )
1048 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1049 if( This->sArgs )
1050 header.dwFlags |= SLDF_HAS_ARGS;
1051 if( This->sIcoPath )
1052 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1053 if( This->sProduct )
1054 header.dwFlags |= SLDF_HAS_LOGO3ID;
1055 if( This->sComponent )
1056 header.dwFlags |= SLDF_HAS_DARWINID;
1058 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1059 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1060 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1062 /* write the Shortcut header */
1063 r = IStream_Write( stm, &header, sizeof(header), &count );
1064 if( FAILED( r ) )
1066 ERR("Write failed at %d\n",__LINE__);
1067 return r;
1070 TRACE("Writing pidl\n");
1072 /* write the PIDL to the shortcut */
1073 if( This->pPidl )
1075 r = ILSaveToStream( stm, This->pPidl );
1076 if( FAILED( r ) )
1078 ERR("Failed to write PIDL at %d\n",__LINE__);
1079 return r;
1083 if( This->sPath )
1084 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1086 if( This->sDescription )
1087 r = Stream_WriteString( stm, This->sDescription );
1089 if( This->sPathRel )
1090 r = Stream_WriteString( stm, This->sPathRel );
1092 if( This->sWorkDir )
1093 r = Stream_WriteString( stm, This->sWorkDir );
1095 if( This->sArgs )
1096 r = Stream_WriteString( stm, This->sArgs );
1098 if( This->sIcoPath )
1099 r = Stream_WriteString( stm, This->sIcoPath );
1101 if( This->sProduct )
1102 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1104 if( This->sComponent )
1105 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1107 /* the last field is a single zero dword */
1108 zero = 0;
1109 r = IStream_Write( stm, &zero, sizeof zero, &count );
1111 return S_OK;
1114 /************************************************************************
1115 * IPersistStream_GetSizeMax (IPersistStream)
1117 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1118 IPersistStream* iface,
1119 ULARGE_INTEGER* pcbSize)
1121 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1123 TRACE("(%p)\n", This);
1125 return E_NOTIMPL;
1128 static const IPersistStreamVtbl psvt =
1130 IPersistStream_fnQueryInterface,
1131 IPersistStream_fnAddRef,
1132 IPersistStream_fnRelease,
1133 IPersistStream_fnGetClassID,
1134 IPersistStream_fnIsDirty,
1135 IPersistStream_fnLoad,
1136 IPersistStream_fnSave,
1137 IPersistStream_fnGetSizeMax
1140 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1142 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1143 return FALSE;
1144 return TRUE;
1147 /**************************************************************************
1148 * ShellLink_UpdatePath
1149 * update absolute path in sPath using relative path in sPathRel
1151 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1153 if (!path || !psPath)
1154 return E_INVALIDARG;
1156 if (!*psPath && sPathRel) {
1157 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1158 LPWSTR final = NULL;
1160 /* first try if [directory of link file] + [relative path] finds an existing file */
1162 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1163 if( !final )
1164 final = buffer;
1165 lstrcpyW(final, sPathRel);
1167 *abs_path = '\0';
1169 if (SHELL_ExistsFileW(buffer)) {
1170 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1171 lstrcpyW(abs_path, buffer);
1172 } else {
1173 /* try if [working directory] + [relative path] finds an existing file */
1174 if (sWorkDir) {
1175 lstrcpyW(buffer, sWorkDir);
1176 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1178 if (SHELL_ExistsFileW(buffer))
1179 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1180 lstrcpyW(abs_path, buffer);
1184 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1185 if (!*abs_path)
1186 lstrcpyW(abs_path, sPathRel);
1188 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1189 if (!*psPath)
1190 return E_OUTOFMEMORY;
1192 lstrcpyW(*psPath, abs_path);
1195 return S_OK;
1198 /**************************************************************************
1199 * IShellLink_ConstructFromFile
1201 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1202 LPCITEMIDLIST pidl, IUnknown **ppv)
1204 IShellLinkW* psl;
1206 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1208 if (SUCCEEDED(hr)) {
1209 IPersistFile* ppf;
1211 *ppv = NULL;
1213 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1215 if (SUCCEEDED(hr)) {
1216 WCHAR path[MAX_PATH];
1218 if (SHGetPathFromIDListW(pidl, path))
1219 hr = IPersistFile_Load(ppf, path, 0);
1220 else
1221 hr = E_FAIL;
1223 if (SUCCEEDED(hr))
1224 *ppv = (IUnknown*)psl;
1226 IPersistFile_Release(ppf);
1229 if (!*ppv)
1230 IShellLinkW_Release(psl);
1233 return hr;
1236 /**************************************************************************
1237 * IShellLinkA_QueryInterface
1239 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1241 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1242 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1245 /******************************************************************************
1246 * IShellLinkA_AddRef
1248 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1250 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1251 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1254 /******************************************************************************
1255 * IShellLinkA_Release
1257 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1259 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1260 return IShellLinkW_Release(&This->IShellLinkW_iface);
1263 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1264 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1266 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1268 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1269 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1271 if (This->sComponent || This->sProduct)
1272 return S_FALSE;
1274 if (cchMaxPath)
1275 pszFile[0] = 0;
1276 if (This->sPath)
1277 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1278 pszFile, cchMaxPath, NULL, NULL);
1280 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1282 return S_OK;
1285 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1287 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1288 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1291 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1293 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1294 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1297 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1298 INT cchMaxName)
1300 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1302 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1304 if( cchMaxName )
1305 pszName[0] = 0;
1306 if( This->sDescription )
1307 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1308 pszName, cchMaxName, NULL, NULL);
1310 return S_OK;
1313 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1315 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1316 WCHAR *descrW;
1317 HRESULT hr;
1319 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1321 if (pszName)
1323 descrW = heap_strdupAtoW(pszName);
1324 if (!descrW) return E_OUTOFMEMORY;
1326 else
1327 descrW = NULL;
1329 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1330 HeapFree(GetProcessHeap(), 0, descrW);
1332 return hr;
1335 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1336 INT cchMaxPath)
1338 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1340 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1342 if( cchMaxPath )
1343 pszDir[0] = 0;
1344 if( This->sWorkDir )
1345 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1346 pszDir, cchMaxPath, NULL, NULL);
1348 return S_OK;
1351 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1353 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1354 WCHAR *dirW;
1355 HRESULT hr;
1357 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1359 dirW = heap_strdupAtoW(pszDir);
1360 if (!dirW) return E_OUTOFMEMORY;
1362 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1363 HeapFree(GetProcessHeap(), 0, dirW);
1365 return hr;
1368 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1370 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1372 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1374 if( cchMaxPath )
1375 pszArgs[0] = 0;
1376 if( This->sArgs )
1377 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1378 pszArgs, cchMaxPath, NULL, NULL);
1380 return S_OK;
1383 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1385 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1386 WCHAR *argsW;
1387 HRESULT hr;
1389 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1391 if (pszArgs)
1393 argsW = heap_strdupAtoW(pszArgs);
1394 if (!argsW) return E_OUTOFMEMORY;
1396 else
1397 argsW = NULL;
1399 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1400 HeapFree(GetProcessHeap(), 0, argsW);
1402 return hr;
1405 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1407 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1408 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1411 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1413 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1414 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1417 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1419 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1420 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1423 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1425 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1426 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1429 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1430 INT cchIconPath, INT *piIcon)
1432 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1434 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1436 *piIcon = This->iIcoNdx;
1438 if (This->sIcoPath)
1439 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1440 else
1441 pszIconPath[0] = 0;
1443 return S_OK;
1446 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR pszIconPath,
1447 INT iIcon)
1449 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1450 WCHAR *pathW;
1451 HRESULT hr;
1453 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1455 pathW = heap_strdupAtoW(pszIconPath);
1456 if (!pathW) return E_OUTOFMEMORY;
1458 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, pathW, iIcon);
1459 HeapFree(GetProcessHeap(), 0, pathW);
1461 return hr;
1464 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1465 DWORD dwReserved)
1467 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1468 WCHAR *pathW;
1469 HRESULT hr;
1471 TRACE("(%p)->(path=%s %x)\n",This, pszPathRel, dwReserved);
1473 pathW = heap_strdupAtoW(pszPathRel);
1474 if (!pathW) return E_OUTOFMEMORY;
1476 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1477 HeapFree(GetProcessHeap(), 0, pathW);
1479 return hr;
1482 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1484 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1486 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1488 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1491 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1493 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1494 HRESULT r;
1495 LPWSTR str;
1497 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1499 if (!pszFile) return E_INVALIDARG;
1501 str = heap_strdupAtoW(pszFile);
1502 if( !str )
1503 return E_OUTOFMEMORY;
1505 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1506 HeapFree( GetProcessHeap(), 0, str );
1508 return r;
1511 /**************************************************************************
1512 * IShellLink Implementation
1515 static const IShellLinkAVtbl slvt =
1517 IShellLinkA_fnQueryInterface,
1518 IShellLinkA_fnAddRef,
1519 IShellLinkA_fnRelease,
1520 IShellLinkA_fnGetPath,
1521 IShellLinkA_fnGetIDList,
1522 IShellLinkA_fnSetIDList,
1523 IShellLinkA_fnGetDescription,
1524 IShellLinkA_fnSetDescription,
1525 IShellLinkA_fnGetWorkingDirectory,
1526 IShellLinkA_fnSetWorkingDirectory,
1527 IShellLinkA_fnGetArguments,
1528 IShellLinkA_fnSetArguments,
1529 IShellLinkA_fnGetHotkey,
1530 IShellLinkA_fnSetHotkey,
1531 IShellLinkA_fnGetShowCmd,
1532 IShellLinkA_fnSetShowCmd,
1533 IShellLinkA_fnGetIconLocation,
1534 IShellLinkA_fnSetIconLocation,
1535 IShellLinkA_fnSetRelativePath,
1536 IShellLinkA_fnResolve,
1537 IShellLinkA_fnSetPath
1541 /**************************************************************************
1542 * IShellLinkW_fnQueryInterface
1544 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1545 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1547 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1549 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1551 *ppvObj = NULL;
1553 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1555 *ppvObj = &This->IShellLinkA_iface;
1557 else if(IsEqualIID(riid, &IID_IShellLinkW))
1559 *ppvObj = &This->IShellLinkW_iface;
1561 else if(IsEqualIID(riid, &IID_IPersistFile))
1563 *ppvObj = &This->IPersistFile_iface;
1565 else if(IsEqualIID(riid, &IID_IPersistStream))
1567 *ppvObj = &This->IPersistStream_iface;
1569 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1571 *ppvObj = &This->IShellLinkDataList_iface;
1573 else if(IsEqualIID(riid, &IID_IShellExtInit))
1575 *ppvObj = &This->IShellExtInit_iface;
1577 else if(IsEqualIID(riid, &IID_IContextMenu))
1579 *ppvObj = &This->IContextMenu_iface;
1581 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1583 *ppvObj = &This->IObjectWithSite_iface;
1585 else if(IsEqualIID(riid, &IID_IPropertyStore))
1587 *ppvObj = &This->IPropertyStore_iface;
1590 if(*ppvObj)
1592 IUnknown_AddRef((IUnknown*)*ppvObj);
1593 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1594 return S_OK;
1596 ERR("-- Interface: E_NOINTERFACE\n");
1597 return E_NOINTERFACE;
1600 /******************************************************************************
1601 * IShellLinkW_fnAddRef
1603 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1605 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1606 ULONG ref = InterlockedIncrement(&This->ref);
1608 TRACE("(%p)->(count=%u)\n", This, ref - 1);
1610 return ref;
1613 /******************************************************************************
1614 * IShellLinkW_fnRelease
1616 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1618 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1619 ULONG refCount = InterlockedDecrement(&This->ref);
1621 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
1623 if (refCount)
1624 return refCount;
1626 TRACE("-- destroying IShellLink(%p)\n",This);
1628 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1629 HeapFree(GetProcessHeap(), 0, This->sArgs);
1630 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1631 HeapFree(GetProcessHeap(), 0, This->sDescription);
1632 HeapFree(GetProcessHeap(), 0, This->sPath);
1633 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1634 HeapFree(GetProcessHeap(), 0, This->sProduct);
1635 HeapFree(GetProcessHeap(), 0, This->sComponent);
1636 HeapFree(GetProcessHeap(), 0, This->filepath);
1638 if (This->site)
1639 IUnknown_Release( This->site );
1641 if (This->pPidl)
1642 ILFree(This->pPidl);
1644 LocalFree(This);
1646 return 0;
1649 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1651 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1653 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%u)(%s)\n",
1654 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1656 if (This->sComponent || This->sProduct)
1657 return S_FALSE;
1659 if (cchMaxPath)
1660 pszFile[0] = 0;
1661 if (This->sPath)
1662 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1664 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1666 return S_OK;
1669 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1671 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1673 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1675 if (!This->pPidl)
1677 *ppidl = NULL;
1678 return S_FALSE;
1680 *ppidl = ILClone(This->pPidl);
1681 return S_OK;
1684 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1686 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1688 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1690 if( This->pPidl )
1691 ILFree( This->pPidl );
1692 This->pPidl = ILClone( pidl );
1693 if( !This->pPidl )
1694 return E_FAIL;
1696 This->bDirty = TRUE;
1698 return S_OK;
1701 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1703 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1705 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1707 pszName[0] = 0;
1708 if( This->sDescription )
1709 lstrcpynW( pszName, This->sDescription, cchMaxName );
1711 return S_OK;
1714 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1716 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1718 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1720 HeapFree(GetProcessHeap(), 0, This->sDescription);
1721 if (pszName)
1723 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1724 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1725 if ( !This->sDescription )
1726 return E_OUTOFMEMORY;
1728 lstrcpyW( This->sDescription, pszName );
1730 else
1731 This->sDescription = NULL;
1732 This->bDirty = TRUE;
1734 return S_OK;
1737 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1739 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1741 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1743 if( cchMaxPath )
1744 pszDir[0] = 0;
1745 if( This->sWorkDir )
1746 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1748 return S_OK;
1751 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1753 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1755 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1757 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1758 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1759 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1760 if ( !This->sWorkDir )
1761 return E_OUTOFMEMORY;
1762 lstrcpyW( This->sWorkDir, pszDir );
1763 This->bDirty = TRUE;
1765 return S_OK;
1768 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1770 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1772 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1774 if( cchMaxPath )
1775 pszArgs[0] = 0;
1776 if( This->sArgs )
1777 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1779 return S_OK;
1782 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1784 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1786 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1788 HeapFree(GetProcessHeap(), 0, This->sArgs);
1789 if (pszArgs)
1791 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1792 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1793 if ( !This->sArgs )
1794 return E_OUTOFMEMORY;
1795 lstrcpyW( This->sArgs, pszArgs );
1797 else This->sArgs = NULL;
1799 This->bDirty = TRUE;
1801 return S_OK;
1804 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1806 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1808 TRACE("(%p)->(%p)\n",This, pwHotkey);
1810 *pwHotkey=This->wHotKey;
1812 return S_OK;
1815 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1817 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1819 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1821 This->wHotKey = wHotkey;
1822 This->bDirty = TRUE;
1824 return S_OK;
1827 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1829 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1831 TRACE("(%p)->(%p)\n",This, piShowCmd);
1833 *piShowCmd = This->iShowCmd;
1835 return S_OK;
1838 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1840 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1842 TRACE("(%p)->(%d)\n", This, iShowCmd);
1844 This->iShowCmd = iShowCmd;
1845 This->bDirty = TRUE;
1847 return S_OK;
1850 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1852 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1854 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1856 *piIcon = This->iIcoNdx;
1858 if (This->sIcoPath)
1859 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1860 else
1861 pszIconPath[0] = 0;
1863 return S_OK;
1866 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1868 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1870 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1872 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1873 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1874 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1875 if ( !This->sIcoPath )
1876 return E_OUTOFMEMORY;
1877 lstrcpyW( This->sIcoPath, pszIconPath );
1879 This->iIcoNdx = iIcon;
1880 This->bDirty = TRUE;
1882 return S_OK;
1885 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1887 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1889 TRACE("(%p)->(path=%s %x)\n",This, debugstr_w(pszPathRel), dwReserved);
1891 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1892 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1893 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1894 if ( !This->sPathRel )
1895 return E_OUTOFMEMORY;
1896 lstrcpyW( This->sPathRel, pszPathRel );
1897 This->bDirty = TRUE;
1899 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1902 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1904 HRESULT hr = S_OK;
1905 BOOL bSuccess;
1907 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1909 TRACE("(%p)->(hwnd=%p flags=%x)\n",This, hwnd, fFlags);
1911 /*FIXME: use IResolveShellLink interface */
1913 if (!This->sPath && This->pPidl) {
1914 WCHAR buffer[MAX_PATH];
1916 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1918 if (bSuccess && *buffer) {
1919 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1920 if (!This->sPath)
1921 return E_OUTOFMEMORY;
1923 lstrcpyW(This->sPath, buffer);
1925 This->bDirty = TRUE;
1926 } else
1927 hr = S_OK; /* don't report an error occurred while just caching information */
1930 if (!This->sIcoPath && This->sPath) {
1931 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1932 if (!This->sIcoPath)
1933 return E_OUTOFMEMORY;
1935 lstrcpyW(This->sIcoPath, This->sPath);
1936 This->iIcoNdx = 0;
1938 This->bDirty = TRUE;
1941 return hr;
1944 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
1946 LPWSTR ret;
1947 LPCWSTR p;
1948 DWORD len;
1950 if( !str )
1951 return NULL;
1953 p = strchrW( str, ':' );
1954 if( !p )
1955 return NULL;
1956 len = p - str;
1957 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
1958 if( !ret )
1959 return ret;
1960 memcpy( ret, str, sizeof(WCHAR)*len );
1961 ret[len] = 0;
1962 return ret;
1965 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
1967 LPCWSTR szComponent = NULL, szProduct = NULL, p;
1968 WCHAR szGuid[39];
1969 HRESULT r;
1970 GUID guid;
1971 int len;
1973 while( str[0] )
1975 /* each segment must start with two colons */
1976 if( str[0] != ':' || str[1] != ':' )
1977 return E_FAIL;
1979 /* the last segment is just two colons */
1980 if( !str[2] )
1981 break;
1982 str += 2;
1984 /* there must be a colon straight after a guid */
1985 p = strchrW( str, ':' );
1986 if( !p )
1987 return E_FAIL;
1988 len = p - str;
1989 if( len != 38 )
1990 return E_FAIL;
1992 /* get the guid, and check it's validly formatted */
1993 memcpy( szGuid, str, sizeof(WCHAR)*len );
1994 szGuid[len] = 0;
1995 r = CLSIDFromString( szGuid, &guid );
1996 if( r != S_OK )
1997 return r;
1998 str = p + 1;
2000 /* match it up to a guid that we care about */
2001 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2002 szComponent = str;
2003 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2004 szProduct = str;
2005 else
2006 return E_FAIL;
2008 /* skip to the next field */
2009 str = strchrW( str, ':' );
2010 if( !str )
2011 return E_FAIL;
2014 /* we have to have a component for an advertised shortcut */
2015 if( !szComponent )
2016 return E_FAIL;
2018 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2019 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2021 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2022 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2024 return S_OK;
2027 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2029 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2030 WCHAR drive[] = { path[0], ':', '\\', 0 };
2031 BOOL r;
2033 volume->type = GetDriveTypeW(drive);
2034 r = GetVolumeInformationW(drive, volume->label, label_sz,
2035 &volume->serial, NULL, NULL, NULL, 0);
2036 TRACE("r = %d type %d serial %08x name %s\n", r,
2037 volume->type, volume->serial, debugstr_w(volume->label));
2038 return r;
2041 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2043 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2044 WCHAR buffer[MAX_PATH];
2045 LPWSTR fname, unquoted = NULL;
2046 HRESULT hr = S_OK;
2047 UINT len;
2049 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2051 if (!pszFile) return E_INVALIDARG;
2053 /* quotes at the ends of the string are stripped */
2054 len = lstrlenW(pszFile);
2055 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2057 unquoted = strdupW(pszFile);
2058 PathUnquoteSpacesW(unquoted);
2059 pszFile = unquoted;
2062 /* any other quote marks are invalid */
2063 if (strchrW(pszFile, '"'))
2065 HeapFree(GetProcessHeap(), 0, unquoted);
2066 return S_FALSE;
2069 HeapFree(GetProcessHeap(), 0, This->sPath);
2070 This->sPath = NULL;
2072 HeapFree(GetProcessHeap(), 0, This->sComponent);
2073 This->sComponent = NULL;
2075 if (This->pPidl)
2076 ILFree(This->pPidl);
2077 This->pPidl = NULL;
2079 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2081 if (*pszFile == '\0')
2082 *buffer = '\0';
2083 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2084 return E_FAIL;
2085 else if(!PathFileExistsW(buffer) &&
2086 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2087 hr = S_FALSE;
2089 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2090 ShellLink_GetVolumeInfo(buffer, &This->volume);
2092 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2093 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2094 if (!This->sPath)
2096 HeapFree(GetProcessHeap(), 0, unquoted);
2097 return E_OUTOFMEMORY;
2100 lstrcpyW(This->sPath, buffer);
2102 This->bDirty = TRUE;
2103 HeapFree(GetProcessHeap(), 0, unquoted);
2105 return hr;
2108 /**************************************************************************
2109 * IShellLinkW Implementation
2112 static const IShellLinkWVtbl slvtw =
2114 IShellLinkW_fnQueryInterface,
2115 IShellLinkW_fnAddRef,
2116 IShellLinkW_fnRelease,
2117 IShellLinkW_fnGetPath,
2118 IShellLinkW_fnGetIDList,
2119 IShellLinkW_fnSetIDList,
2120 IShellLinkW_fnGetDescription,
2121 IShellLinkW_fnSetDescription,
2122 IShellLinkW_fnGetWorkingDirectory,
2123 IShellLinkW_fnSetWorkingDirectory,
2124 IShellLinkW_fnGetArguments,
2125 IShellLinkW_fnSetArguments,
2126 IShellLinkW_fnGetHotkey,
2127 IShellLinkW_fnSetHotkey,
2128 IShellLinkW_fnGetShowCmd,
2129 IShellLinkW_fnSetShowCmd,
2130 IShellLinkW_fnGetIconLocation,
2131 IShellLinkW_fnSetIconLocation,
2132 IShellLinkW_fnSetRelativePath,
2133 IShellLinkW_fnResolve,
2134 IShellLinkW_fnSetPath
2137 static HRESULT WINAPI
2138 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2140 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2141 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2144 static ULONG WINAPI
2145 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2147 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2148 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2151 static ULONG WINAPI
2152 ShellLink_DataList_Release( IShellLinkDataList* iface )
2154 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2155 return IShellLinkW_Release(&This->IShellLinkW_iface);
2158 static HRESULT WINAPI
2159 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2161 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2162 return E_NOTIMPL;
2165 static HRESULT WINAPI
2166 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2168 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2169 LPVOID block = NULL;
2170 HRESULT r = E_FAIL;
2172 TRACE("%p %08x %p\n", iface, dwSig, ppDataBlock );
2174 switch (dwSig)
2176 case EXP_DARWIN_ID_SIG:
2177 if (!This->sComponent)
2178 break;
2179 block = shelllink_build_darwinid( This->sComponent, dwSig );
2180 r = S_OK;
2181 break;
2182 case EXP_SZ_LINK_SIG:
2183 case NT_CONSOLE_PROPS_SIG:
2184 case NT_FE_CONSOLE_PROPS_SIG:
2185 case EXP_SPECIAL_FOLDER_SIG:
2186 case EXP_SZ_ICON_SIG:
2187 FIXME("valid but unhandled datablock %08x\n", dwSig);
2188 break;
2189 default:
2190 ERR("unknown datablock %08x\n", dwSig);
2192 *ppDataBlock = block;
2193 return r;
2196 static HRESULT WINAPI
2197 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2199 FIXME("(%p)->(%u): stub\n", iface, dwSig);
2200 return E_NOTIMPL;
2203 static HRESULT WINAPI
2204 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2206 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2207 DWORD flags = 0;
2209 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2211 /* FIXME: add more */
2212 if (This->sArgs)
2213 flags |= SLDF_HAS_ARGS;
2214 if (This->sComponent)
2215 flags |= SLDF_HAS_DARWINID;
2216 if (This->sIcoPath)
2217 flags |= SLDF_HAS_ICONLOCATION;
2218 if (This->sProduct)
2219 flags |= SLDF_HAS_LOGO3ID;
2220 if (This->pPidl)
2221 flags |= SLDF_HAS_ID_LIST;
2223 *pdwFlags = flags;
2225 return S_OK;
2228 static HRESULT WINAPI
2229 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2231 FIXME("(%p)->(%u): stub\n", iface, dwFlags);
2232 return E_NOTIMPL;
2235 static const IShellLinkDataListVtbl dlvt =
2237 ShellLink_DataList_QueryInterface,
2238 ShellLink_DataList_AddRef,
2239 ShellLink_DataList_Release,
2240 ShellLink_AddDataBlock,
2241 ShellLink_CopyDataBlock,
2242 ShellLink_RemoveDataBlock,
2243 ShellLink_GetFlags,
2244 ShellLink_SetFlags
2247 static HRESULT WINAPI
2248 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2250 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2251 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2254 static ULONG WINAPI
2255 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2257 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2258 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2261 static ULONG WINAPI
2262 ShellLink_ExtInit_Release( IShellExtInit* iface )
2264 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2265 return IShellLinkW_Release(&This->IShellLinkW_iface);
2268 /**************************************************************************
2269 * ShellLink implementation of IShellExtInit::Initialize()
2271 * Loads the shelllink from the dataobject the shell is pointing to.
2273 static HRESULT WINAPI
2274 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2275 IDataObject *pdtobj, HKEY hkeyProgID )
2277 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2278 FORMATETC format;
2279 STGMEDIUM stgm;
2280 UINT count;
2281 HRESULT r = E_FAIL;
2283 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2285 if( !pdtobj )
2286 return r;
2288 format.cfFormat = CF_HDROP;
2289 format.ptd = NULL;
2290 format.dwAspect = DVASPECT_CONTENT;
2291 format.lindex = -1;
2292 format.tymed = TYMED_HGLOBAL;
2294 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2295 return r;
2297 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2298 if( count == 1 )
2300 LPWSTR path;
2302 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2303 count++;
2304 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2305 if( path )
2307 IPersistFile *pf = &This->IPersistFile_iface;
2309 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2310 r = IPersistFile_Load( pf, path, 0 );
2311 HeapFree( GetProcessHeap(), 0, path );
2314 ReleaseStgMedium( &stgm );
2316 return r;
2319 static const IShellExtInitVtbl eivt =
2321 ShellLink_ExtInit_QueryInterface,
2322 ShellLink_ExtInit_AddRef,
2323 ShellLink_ExtInit_Release,
2324 ShellLink_ExtInit_Initialize
2327 static HRESULT WINAPI
2328 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2330 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2331 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2334 static ULONG WINAPI
2335 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2337 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2338 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2341 static ULONG WINAPI
2342 ShellLink_ContextMenu_Release( IContextMenu* iface )
2344 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2345 return IShellLinkW_Release(&This->IShellLinkW_iface);
2348 static HRESULT WINAPI
2349 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2350 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2352 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2353 static WCHAR szOpen[] = { 'O','p','e','n',0 };
2354 MENUITEMINFOW mii;
2355 int id = 1;
2357 TRACE("%p %p %u %u %u %u\n", This,
2358 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2360 if ( !hmenu )
2361 return E_INVALIDARG;
2363 memset( &mii, 0, sizeof mii );
2364 mii.cbSize = sizeof mii;
2365 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2366 mii.dwTypeData = szOpen;
2367 mii.cch = strlenW( mii.dwTypeData );
2368 mii.wID = idCmdFirst + id++;
2369 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2370 mii.fType = MFT_STRING;
2371 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2372 return E_FAIL;
2373 This->iIdOpen = 0;
2375 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2378 static LPWSTR
2379 shelllink_get_msi_component_path( LPWSTR component )
2381 LPWSTR path;
2382 DWORD r, sz = 0;
2384 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2385 if (r != ERROR_SUCCESS)
2386 return NULL;
2388 sz++;
2389 path = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
2390 r = CommandLineFromMsiDescriptor( component, path, &sz );
2391 if (r != ERROR_SUCCESS)
2393 HeapFree( GetProcessHeap(), 0, path );
2394 path = NULL;
2397 TRACE("returning %s\n", debugstr_w( path ) );
2399 return path;
2402 static HRESULT WINAPI
2403 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2405 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2406 static const WCHAR szOpen[] = { 'O','p','e','n',0 };
2407 SHELLEXECUTEINFOW sei;
2408 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2409 LPWSTR args = NULL;
2410 LPWSTR path = NULL;
2411 HRESULT r;
2413 TRACE("%p %p\n", This, lpici );
2415 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2416 return E_INVALIDARG;
2418 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2420 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2421 return E_INVALIDARG;
2424 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2425 if ( FAILED( r ) )
2426 return r;
2428 if ( This->sComponent )
2430 path = shelllink_get_msi_component_path( This->sComponent );
2431 if (!path)
2432 return E_FAIL;
2434 else
2435 path = strdupW( This->sPath );
2437 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2438 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2440 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2441 DWORD len = 2;
2443 if ( This->sArgs )
2444 len += lstrlenW( This->sArgs );
2445 if ( iciex->lpParametersW )
2446 len += lstrlenW( iciex->lpParametersW );
2448 args = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2449 args[0] = 0;
2450 if ( This->sArgs )
2451 lstrcatW( args, This->sArgs );
2452 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2454 static const WCHAR space[] = { ' ', 0 };
2455 lstrcatW( args, space );
2456 lstrcatW( args, iciex->lpParametersW );
2460 memset( &sei, 0, sizeof sei );
2461 sei.cbSize = sizeof sei;
2462 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2463 sei.lpFile = path;
2464 sei.nShow = This->iShowCmd;
2465 sei.lpIDList = This->pPidl;
2466 sei.lpDirectory = This->sWorkDir;
2467 sei.lpParameters = args;
2468 sei.lpVerb = szOpen;
2470 if( ShellExecuteExW( &sei ) )
2471 r = S_OK;
2472 else
2473 r = E_FAIL;
2475 HeapFree( GetProcessHeap(), 0, args );
2476 HeapFree( GetProcessHeap(), 0, path );
2478 return r;
2481 static HRESULT WINAPI
2482 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2483 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2485 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2487 FIXME("(%p)->(%lu %u %p %p %u): stub\n", This,
2488 idCmd, uType, pwReserved, pszName, cchMax );
2490 return E_NOTIMPL;
2493 static const IContextMenuVtbl cmvt =
2495 ShellLink_ContextMenu_QueryInterface,
2496 ShellLink_ContextMenu_AddRef,
2497 ShellLink_ContextMenu_Release,
2498 ShellLink_QueryContextMenu,
2499 ShellLink_InvokeCommand,
2500 ShellLink_GetCommandString
2503 static HRESULT WINAPI
2504 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2506 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2507 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2510 static ULONG WINAPI
2511 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2513 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2514 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2517 static ULONG WINAPI
2518 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2520 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2521 return IShellLinkW_Release(&This->IShellLinkW_iface);
2524 static HRESULT WINAPI
2525 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2527 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2529 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2531 if ( !This->site )
2532 return E_FAIL;
2533 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2536 static HRESULT WINAPI
2537 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2539 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2541 TRACE("%p %p\n", iface, punk);
2543 if ( punk )
2544 IUnknown_AddRef( punk );
2546 if( This->site )
2547 IUnknown_Release( This->site );
2549 This->site = punk;
2551 return S_OK;
2554 static const IObjectWithSiteVtbl owsvt =
2556 ShellLink_ObjectWithSite_QueryInterface,
2557 ShellLink_ObjectWithSite_AddRef,
2558 ShellLink_ObjectWithSite_Release,
2559 ShellLink_SetSite,
2560 ShellLink_GetSite,
2563 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2565 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2566 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2569 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2571 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2572 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2575 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2577 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2578 return IShellLinkW_Release(&This->IShellLinkW_iface);
2581 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2583 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2584 FIXME("(%p)->(%p): stub\n", This, props);
2585 return E_NOTIMPL;
2588 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2590 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2591 FIXME("(%p)->(%d %p): stub\n", This, propid, key);
2592 return E_NOTIMPL;
2595 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2597 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2598 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2599 return E_NOTIMPL;
2602 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2604 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2605 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2606 return E_NOTIMPL;
2609 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2611 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2612 FIXME("(%p): stub\n", This);
2613 return E_NOTIMPL;
2616 static const IPropertyStoreVtbl propertystorevtbl = {
2617 propertystore_QueryInterface,
2618 propertystore_AddRef,
2619 propertystore_Release,
2620 propertystore_GetCount,
2621 propertystore_GetAt,
2622 propertystore_GetValue,
2623 propertystore_SetValue,
2624 propertystore_Commit
2627 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2629 IShellLinkImpl * sl;
2630 HRESULT r;
2632 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2634 *obj = NULL;
2636 if (outer)
2637 return CLASS_E_NOAGGREGATION;
2639 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2640 if (!sl)
2641 return E_OUTOFMEMORY;
2643 sl->ref = 1;
2644 sl->IShellLinkA_iface.lpVtbl = &slvt;
2645 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2646 sl->IPersistFile_iface.lpVtbl = &pfvt;
2647 sl->IPersistStream_iface.lpVtbl = &psvt;
2648 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2649 sl->IShellExtInit_iface.lpVtbl = &eivt;
2650 sl->IContextMenu_iface.lpVtbl = &cmvt;
2651 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2652 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2653 sl->iShowCmd = SW_SHOWNORMAL;
2654 sl->bDirty = FALSE;
2655 sl->iIdOpen = -1;
2656 sl->site = NULL;
2657 sl->filepath = NULL;
2659 TRACE("(%p)\n", sl);
2661 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2662 IShellLinkW_Release( &sl->IShellLinkW_iface );
2663 return r;