server: Send WM_WINE_SETCURSOR with the thread input cursor handle.
[wine.git] / dlls / shell32 / shelllink.c
blob30cd038728f424b33d5ce99edb99f58c280a7e82
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 #include "wine/debug.h"
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winreg.h"
43 #include "winuser.h"
44 #include "wingdi.h"
45 #include "shlobj.h"
47 #include "pidl.h"
48 #include "shell32_main.h"
49 #include "shlguid.h"
50 #include "shlwapi.h"
51 #include "msi.h"
52 #include "appmgmt.h"
54 #include "initguid.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(shell);
58 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
59 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
60 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
61 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
63 /* link file formats */
65 #include "pshpack1.h"
67 typedef struct _LINK_HEADER
69 DWORD dwSize; /* 0x00 size of the header - 0x4c */
70 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
71 DWORD dwFlags; /* 0x14 describes elements following */
72 DWORD dwFileAttr; /* 0x18 attributes of the target file */
73 FILETIME CreationTime; /* 0x1c creation time of target file */
74 FILETIME AccessTime; /* 0x24 access time of target file */
75 FILETIME WriteTime; /* 0x2c write time of target file */
76 DWORD dwFileSize; /* 0x34 File size of target file */
77 DWORD nIcon; /* 0x38 icon number or index */
78 DWORD fStartup; /* 0x3c startup type or window state of application */
79 WORD wHotKey; /* 0x40 hotkey */
80 WORD Reserved1; /* 0x42 reserved = 0 */
81 DWORD Reserved2; /* 0x44 reserved = 0 */
82 DWORD Reserved3; /* 0x48 reserved = 0 */
83 } LINK_HEADER, * PLINK_HEADER;
85 #define SHLINK_LOCAL 0
86 #define SHLINK_REMOTE 1
88 typedef struct _LOCATION_INFO
90 DWORD dwTotalSize;
91 DWORD dwHeaderSize;
92 DWORD dwFlags;
93 DWORD dwVolTableOfs;
94 DWORD dwLocalPathOfs;
95 DWORD dwNetworkVolTableOfs;
96 DWORD dwFinalPathOfs;
97 } LOCATION_INFO;
99 typedef struct _LOCAL_VOLUME_INFO
101 DWORD dwSize;
102 DWORD dwType;
103 DWORD dwVolSerial;
104 DWORD dwVolLabelOfs;
105 } LOCAL_VOLUME_INFO;
107 typedef struct volume_info_t
109 DWORD type;
110 DWORD serial;
111 WCHAR label[12]; /* assume 8.3 */
112 } volume_info;
114 #include "poppack.h"
116 /* IShellLink Implementation */
118 typedef struct
120 IShellLinkA IShellLinkA_iface;
121 IShellLinkW IShellLinkW_iface;
122 IPersistFile IPersistFile_iface;
123 IPersistStream IPersistStream_iface;
124 IShellLinkDataList IShellLinkDataList_iface;
125 IShellExtInit IShellExtInit_iface;
126 IContextMenu IContextMenu_iface;
127 IObjectWithSite IObjectWithSite_iface;
128 IPropertyStore IPropertyStore_iface;
130 LONG ref;
132 /* data structures according to the information in the link */
133 LPITEMIDLIST pPidl;
134 WORD wHotKey;
135 SYSTEMTIME CreationTime;
136 SYSTEMTIME AccessTime;
137 SYSTEMTIME WriteTime;
139 DWORD iShowCmd;
140 LPWSTR sIcoPath;
141 INT iIcoNdx;
142 LPWSTR sPath;
143 LPWSTR sArgs;
144 LPWSTR sWorkDir;
145 LPWSTR sDescription;
146 LPWSTR sPathRel;
147 LPWSTR sProduct;
148 LPWSTR sComponent;
149 volume_info volume;
151 BOOL bDirty;
152 INT iIdOpen; /* id of the "Open" entry in the context menu */
153 IUnknown *site;
155 LPOLESTR filepath; /* file path returned by IPersistFile::GetCurFile */
156 } IShellLinkImpl;
158 static inline IShellLinkImpl *impl_from_IShellLinkA(IShellLinkA *iface)
160 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkA_iface);
163 static inline IShellLinkImpl *impl_from_IShellLinkW(IShellLinkW *iface)
165 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkW_iface);
168 static inline IShellLinkImpl *impl_from_IPersistFile(IPersistFile *iface)
170 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistFile_iface);
173 static inline IShellLinkImpl *impl_from_IPersistStream(IPersistStream *iface)
175 return CONTAINING_RECORD(iface, IShellLinkImpl, IPersistStream_iface);
178 static inline IShellLinkImpl *impl_from_IShellLinkDataList(IShellLinkDataList *iface)
180 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellLinkDataList_iface);
183 static inline IShellLinkImpl *impl_from_IShellExtInit(IShellExtInit *iface)
185 return CONTAINING_RECORD(iface, IShellLinkImpl, IShellExtInit_iface);
188 static inline IShellLinkImpl *impl_from_IContextMenu(IContextMenu *iface)
190 return CONTAINING_RECORD(iface, IShellLinkImpl, IContextMenu_iface);
193 static inline IShellLinkImpl *impl_from_IObjectWithSite(IObjectWithSite *iface)
195 return CONTAINING_RECORD(iface, IShellLinkImpl, IObjectWithSite_iface);
198 static inline IShellLinkImpl *impl_from_IPropertyStore(IPropertyStore *iface)
200 return CONTAINING_RECORD(iface, IShellLinkImpl, IPropertyStore_iface);
203 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
205 /**************************************************************************
206 * IPersistFile_QueryInterface
208 static HRESULT WINAPI IPersistFile_fnQueryInterface(
209 IPersistFile* iface,
210 REFIID riid,
211 LPVOID *ppvObj)
213 IShellLinkImpl *This = impl_from_IPersistFile(iface);
214 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
217 /******************************************************************************
218 * IPersistFile_AddRef
220 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
222 IShellLinkImpl *This = impl_from_IPersistFile(iface);
223 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
226 /******************************************************************************
227 * IPersistFile_Release
229 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
231 IShellLinkImpl *This = impl_from_IPersistFile(iface);
232 return IShellLinkW_Release(&This->IShellLinkW_iface);
235 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
237 IShellLinkImpl *This = impl_from_IPersistFile(iface);
239 TRACE("(%p)->(%p)\n", This, pClassID);
241 *pClassID = CLSID_ShellLink;
243 return S_OK;
246 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
248 IShellLinkImpl *This = impl_from_IPersistFile(iface);
250 TRACE("(%p)\n",This);
252 if (This->bDirty)
253 return S_OK;
255 return S_FALSE;
258 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
260 IShellLinkImpl *This = impl_from_IPersistFile(iface);
261 IPersistStream *StreamThis = &This->IPersistStream_iface;
262 HRESULT r;
263 IStream *stm;
265 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
267 if( dwMode == 0 ) dwMode = STGM_READ;
268 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
269 if( SUCCEEDED( r ) )
271 r = IPersistStream_Load(StreamThis, stm);
272 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
273 IStream_Release( stm );
275 /* update file path */
276 free(This->filepath);
277 This->filepath = wcsdup(pszFileName);
279 This->bDirty = FALSE;
281 TRACE("-- returning hr %08lx\n", r);
282 return r;
285 BOOL run_winemenubuilder( const WCHAR *args )
287 LONG len;
288 LPWSTR buffer;
289 STARTUPINFOW si;
290 PROCESS_INFORMATION pi;
291 BOOL ret;
292 WCHAR app[MAX_PATH];
293 void *redir;
295 GetSystemDirectoryW( app, MAX_PATH );
296 lstrcatW( app, L"\\winemenubuilder.exe" );
298 len = (lstrlenW( app ) + lstrlenW( args ) + 1) * sizeof(WCHAR);
299 buffer = malloc( len );
300 if( !buffer )
301 return FALSE;
303 lstrcpyW( buffer, app );
304 lstrcatW( buffer, args );
306 TRACE("starting %s\n",debugstr_w(buffer));
308 memset(&si, 0, sizeof(si));
309 si.cb = sizeof(si);
311 Wow64DisableWow64FsRedirection( &redir );
312 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
313 Wow64RevertWow64FsRedirection( redir );
315 free( buffer );
317 if (ret)
319 CloseHandle( pi.hProcess );
320 CloseHandle( pi.hThread );
323 return ret;
326 static BOOL StartLinkProcessor( LPCOLESTR szLink )
328 LONG len;
329 LPWSTR buffer;
330 BOOL ret;
332 len = (lstrlenW( szLink ) + 7) * sizeof(WCHAR);
333 buffer = malloc( len );
334 if( !buffer )
335 return FALSE;
337 swprintf( buffer, len, L" -w \"%s\"", szLink );
338 ret = run_winemenubuilder( buffer );
339 free( buffer );
340 return ret;
343 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
345 IShellLinkImpl *This = impl_from_IPersistFile(iface);
346 IPersistStream *StreamThis = &This->IPersistStream_iface;
347 HRESULT r;
348 IStream *stm;
350 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
352 if (!pszFileName)
354 if (!This->filepath) return S_OK;
356 pszFileName = This->filepath;
357 fRemember = FALSE;
360 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_DENY_WRITE, &stm );
361 if( SUCCEEDED( r ) )
363 r = IPersistStream_Save(StreamThis, stm, FALSE);
364 IStream_Release( stm );
366 if( SUCCEEDED( r ) )
368 StartLinkProcessor( pszFileName );
370 if (fRemember)
372 /* update file path */
373 free(This->filepath);
374 This->filepath = wcsdup(pszFileName);
377 This->bDirty = FALSE;
379 else
381 DeleteFileW( pszFileName );
382 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
386 return r;
389 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
391 IShellLinkImpl *This = impl_from_IPersistFile(iface);
392 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
393 return S_OK;
396 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
398 IShellLinkImpl *This = impl_from_IPersistFile(iface);
400 TRACE("(%p)->(%p)\n", This, filename);
402 if (!This->filepath)
404 *filename = NULL;
405 return S_FALSE;
408 *filename = CoTaskMemAlloc((lstrlenW(This->filepath) + 1) * sizeof(WCHAR));
409 if (!*filename) return E_OUTOFMEMORY;
411 lstrcpyW(*filename, This->filepath);
413 return S_OK;
416 static const IPersistFileVtbl pfvt =
418 IPersistFile_fnQueryInterface,
419 IPersistFile_fnAddRef,
420 IPersistFile_fnRelease,
421 IPersistFile_fnGetClassID,
422 IPersistFile_fnIsDirty,
423 IPersistFile_fnLoad,
424 IPersistFile_fnSave,
425 IPersistFile_fnSaveCompleted,
426 IPersistFile_fnGetCurFile
429 /************************************************************************
430 * IPersistStream_QueryInterface
432 static HRESULT WINAPI IPersistStream_fnQueryInterface(
433 IPersistStream* iface,
434 REFIID riid,
435 VOID** ppvObj)
437 IShellLinkImpl *This = impl_from_IPersistStream(iface);
438 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
441 /************************************************************************
442 * IPersistStream_Release
444 static ULONG WINAPI IPersistStream_fnRelease(
445 IPersistStream* iface)
447 IShellLinkImpl *This = impl_from_IPersistStream(iface);
448 return IShellLinkW_Release(&This->IShellLinkW_iface);
451 /************************************************************************
452 * IPersistStream_AddRef
454 static ULONG WINAPI IPersistStream_fnAddRef(
455 IPersistStream* iface)
457 IShellLinkImpl *This = impl_from_IPersistStream(iface);
458 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
461 /************************************************************************
462 * IPersistStream_GetClassID
465 static HRESULT WINAPI IPersistStream_fnGetClassID(
466 IPersistStream* iface,
467 CLSID* pClassID)
469 IShellLinkImpl *This = impl_from_IPersistStream(iface);
470 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
473 /************************************************************************
474 * IPersistStream_IsDirty (IPersistStream)
476 static HRESULT WINAPI IPersistStream_fnIsDirty(
477 IPersistStream* iface)
479 IShellLinkImpl *This = impl_from_IPersistStream(iface);
481 TRACE("(%p)\n", This);
483 return S_OK;
487 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
489 DWORD count;
490 USHORT len;
491 LPVOID temp;
492 LPWSTR str;
493 HRESULT r;
495 TRACE("%p\n", stm);
497 count = 0;
498 r = IStream_Read(stm, &len, sizeof(len), &count);
499 if ( FAILED (r) || ( count != sizeof(len) ) )
500 return E_FAIL;
502 if( unicode )
503 len *= sizeof (WCHAR);
505 TRACE("reading %d\n", len);
506 temp = malloc(len + sizeof(WCHAR));
507 if( !temp )
508 return E_OUTOFMEMORY;
509 count = 0;
510 r = IStream_Read(stm, temp, len, &count);
511 if( FAILED (r) || ( count != len ) )
513 free( temp );
514 return E_FAIL;
517 TRACE("read %s\n", debugstr_an(temp,len));
519 /* convert to unicode if necessary */
520 if( !unicode )
522 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
523 str = malloc( (count + 1) * sizeof(WCHAR) );
524 if( !str )
526 free( temp );
527 return E_OUTOFMEMORY;
529 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
530 free( temp );
532 else
534 count /= 2;
535 str = temp;
537 str[count] = 0;
539 *pstr = str;
541 return S_OK;
544 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
546 DWORD size;
547 ULONG count;
548 HRESULT r;
549 struct sized_chunk {
550 DWORD size;
551 unsigned char data[1];
552 } *chunk;
554 TRACE("%p\n",stm);
556 r = IStream_Read( stm, &size, sizeof(size), &count );
557 if( FAILED( r ) || count != sizeof(size) )
558 return E_FAIL;
560 chunk = malloc( size );
561 if( !chunk )
562 return E_OUTOFMEMORY;
564 chunk->size = size;
565 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
566 if( FAILED( r ) || count != (size - sizeof(size)) )
568 free( chunk );
569 return E_FAIL;
572 TRACE("Read %ld bytes\n",chunk->size);
574 *data = chunk;
576 return S_OK;
579 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
581 const int label_sz = ARRAY_SIZE(volume->label);
582 LPSTR label;
583 int len;
585 volume->serial = vol->dwVolSerial;
586 volume->type = vol->dwType;
588 if( !vol->dwVolLabelOfs )
589 return FALSE;
590 if( vol->dwSize <= vol->dwVolLabelOfs )
591 return FALSE;
592 len = vol->dwSize - vol->dwVolLabelOfs;
594 label = (LPSTR) vol;
595 label += vol->dwVolLabelOfs;
596 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
598 return TRUE;
601 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
603 int len = 0, wlen;
604 LPWSTR path;
606 while( (len < maxlen) && p[len] )
607 len++;
609 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
610 path = malloc((wlen + 1) * sizeof(WCHAR));
611 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
612 path[wlen] = 0;
614 return path;
617 static HRESULT Stream_LoadLocation( IStream *stm,
618 volume_info *volume, LPWSTR *path )
620 char *p = NULL;
621 LOCATION_INFO *loc;
622 HRESULT r;
623 DWORD n;
625 r = Stream_ReadChunk( stm, (LPVOID*) &p );
626 if( FAILED(r) )
627 return r;
629 loc = (LOCATION_INFO*) p;
630 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
632 free( p );
633 return E_FAIL;
636 /* if there's valid local volume information, load it */
637 if( loc->dwVolTableOfs &&
638 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
640 LOCAL_VOLUME_INFO *volume_info;
642 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
643 Stream_LoadVolume( volume_info, volume );
646 /* if there's a local path, load it */
647 n = loc->dwLocalPathOfs;
648 if( n && (n < loc->dwTotalSize) )
649 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
651 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
652 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
654 free( p );
655 return S_OK;
659 * The format of the advertised shortcut info seems to be:
661 * Offset Description
662 * ------ -----------
664 * 0 Length of the block (4 bytes, usually 0x314)
665 * 4 tag (dword)
666 * 8 string data in ANSI
667 * 8+0x104 string data in UNICODE
669 * In the original Win32 implementation the buffers are not initialized
670 * to zero, so data trailing the string is random garbage.
672 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
674 DWORD size;
675 ULONG count;
676 HRESULT r;
677 EXP_DARWIN_LINK buffer;
679 TRACE("%p\n",stm);
681 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
682 if( FAILED( r ) )
683 return r;
685 /* make sure that we read the size of the structure even on error */
686 size = sizeof buffer - sizeof (DWORD);
687 if( buffer.dbh.cbSize != sizeof buffer )
689 ERR("Ooops. This structure is not as expected...\n");
690 return E_FAIL;
693 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
694 if( FAILED( r ) )
695 return r;
697 if( count != size )
698 return E_FAIL;
700 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
702 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
704 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
705 return E_FAIL;
708 *str = wcsdup( buffer.szwDarwinID );
710 return S_OK;
713 /************************************************************************
714 * IPersistStream_Load (IPersistStream)
716 static HRESULT WINAPI IPersistStream_fnLoad(
717 IPersistStream* iface,
718 IStream* stm)
720 LINK_HEADER hdr;
721 ULONG dwBytesRead;
722 BOOL unicode;
723 HRESULT r;
724 DWORD zero;
726 IShellLinkImpl *This = impl_from_IPersistStream(iface);
728 TRACE("%p %p\n", This, stm);
730 if( !stm )
731 return STG_E_INVALIDPOINTER;
733 dwBytesRead = 0;
734 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
735 if( FAILED( r ) )
736 return r;
738 if( dwBytesRead != sizeof(hdr))
739 return E_FAIL;
740 if( hdr.dwSize != sizeof(hdr))
741 return E_FAIL;
742 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
743 return E_FAIL;
745 /* free all the old stuff */
746 ILFree(This->pPidl);
747 This->pPidl = NULL;
748 memset( &This->volume, 0, sizeof This->volume );
749 free(This->sPath);
750 This->sPath = NULL;
751 free(This->sDescription);
752 This->sDescription = NULL;
753 free(This->sPathRel);
754 This->sPathRel = NULL;
755 free(This->sWorkDir);
756 This->sWorkDir = NULL;
757 free(This->sArgs);
758 This->sArgs = NULL;
759 free(This->sIcoPath);
760 This->sIcoPath = NULL;
761 free(This->sProduct);
762 This->sProduct = NULL;
763 free(This->sComponent);
764 This->sComponent = NULL;
766 This->wHotKey = hdr.wHotKey;
767 This->iIcoNdx = hdr.nIcon;
768 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
769 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
770 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
771 if (TRACE_ON(shell))
773 WCHAR sTemp[MAX_PATH];
774 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
775 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
776 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
777 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
778 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
779 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
782 /* load all the new stuff */
783 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
785 r = ILLoadFromStream( stm, &This->pPidl );
786 if( FAILED( r ) )
787 return r;
789 pdump(This->pPidl);
791 /* load the location information */
792 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
793 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
794 if( FAILED( r ) )
795 goto end;
797 unicode = hdr.dwFlags & SLDF_UNICODE;
798 if( hdr.dwFlags & SLDF_HAS_NAME )
800 r = Stream_LoadString( stm, unicode, &This->sDescription );
801 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
803 if( FAILED( r ) )
804 goto end;
806 if( hdr.dwFlags & SLDF_HAS_RELPATH )
808 r = Stream_LoadString( stm, unicode, &This->sPathRel );
809 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
811 if( FAILED( r ) )
812 goto end;
814 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
816 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
817 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
819 if( FAILED( r ) )
820 goto end;
822 if( hdr.dwFlags & SLDF_HAS_ARGS )
824 r = Stream_LoadString( stm, unicode, &This->sArgs );
825 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
827 if( FAILED( r ) )
828 goto end;
830 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
832 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
833 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
835 if( FAILED( r ) )
836 goto end;
838 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
840 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
841 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
843 if( FAILED( r ) )
844 goto end;
846 if( hdr.dwFlags & SLDF_HAS_DARWINID )
848 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
849 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
851 if( FAILED( r ) )
852 goto end;
854 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
855 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
857 /* Some lnk files have extra data blocks starting with a
858 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
859 * one with a 0xa0000003 signature. However these don't seem to matter
860 * too much.
862 WARN("Last word was not zero\n");
865 TRACE("OK\n");
867 pdump (This->pPidl);
869 return S_OK;
870 end:
871 return r;
874 /************************************************************************
875 * Stream_WriteString
877 * Helper function for IPersistStream_Save. Writes a unicode string
878 * with terminating nul byte to a stream, preceded by the its length.
880 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
882 USHORT len = lstrlenW( str ) + 1;
883 DWORD count;
884 HRESULT r;
886 r = IStream_Write( stm, &len, sizeof(len), &count );
887 if( FAILED( r ) )
888 return r;
890 len *= sizeof(WCHAR);
892 r = IStream_Write( stm, str, len, &count );
893 if( FAILED( r ) )
894 return r;
896 return S_OK;
899 /************************************************************************
900 * Stream_WriteLocationInfo
902 * Writes the location info to a stream
904 * FIXME: One day we might want to write the network volume information
905 * and the final path.
906 * Figure out how Windows deals with unicode paths here.
908 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
909 volume_info *volume )
911 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
912 LOCAL_VOLUME_INFO *vol;
913 LOCATION_INFO *loc;
914 LPSTR szLabel, szPath, szFinalPath;
915 ULONG count = 0;
916 HRESULT hr;
918 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
920 /* figure out the size of everything */
921 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
922 NULL, 0, NULL, NULL );
923 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
924 NULL, 0, NULL, NULL );
925 volume_info_size = sizeof *vol + label_size;
926 final_path_size = 1;
927 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
929 /* create pointers to everything */
930 loc = calloc(1, total_size);
931 vol = (LOCAL_VOLUME_INFO*) &loc[1];
932 szLabel = (LPSTR) &vol[1];
933 szPath = &szLabel[label_size];
934 szFinalPath = &szPath[path_size];
936 /* fill in the location information header */
937 loc->dwTotalSize = total_size;
938 loc->dwHeaderSize = sizeof (*loc);
939 loc->dwFlags = 1;
940 loc->dwVolTableOfs = sizeof (*loc);
941 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
942 loc->dwNetworkVolTableOfs = 0;
943 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
945 /* fill in the volume information */
946 vol->dwSize = volume_info_size;
947 vol->dwType = volume->type;
948 vol->dwVolSerial = volume->serial;
949 vol->dwVolLabelOfs = sizeof (*vol);
951 /* copy in the strings */
952 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
953 szLabel, label_size, NULL, NULL );
954 WideCharToMultiByte( CP_ACP, 0, path, -1,
955 szPath, path_size, NULL, NULL );
956 szFinalPath[0] = 0;
958 hr = IStream_Write( stm, loc, total_size, &count );
959 free(loc);
961 return hr;
964 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
966 EXP_DARWIN_LINK *buffer;
968 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
969 buffer->dbh.cbSize = sizeof *buffer;
970 buffer->dbh.dwSignature = magic;
971 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
972 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
974 return buffer;
977 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
979 EXP_DARWIN_LINK *buffer;
980 ULONG count;
982 TRACE("%p\n",stm);
984 buffer = shelllink_build_darwinid( string, magic );
986 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
989 /************************************************************************
990 * IPersistStream_Save (IPersistStream)
992 * FIXME: makes assumptions about byte order
994 static HRESULT WINAPI IPersistStream_fnSave(
995 IPersistStream* iface,
996 IStream* stm,
997 BOOL fClearDirty)
999 LINK_HEADER header;
1000 ULONG count;
1001 DWORD zero;
1002 HRESULT r;
1004 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1006 TRACE("%p %p %x\n", This, stm, fClearDirty);
1008 memset(&header, 0, sizeof(header));
1009 header.dwSize = sizeof(header);
1010 header.fStartup = This->iShowCmd;
1011 header.MagicGuid = CLSID_ShellLink;
1013 header.wHotKey = This->wHotKey;
1014 header.nIcon = This->iIcoNdx;
1015 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1016 if( This->pPidl )
1017 header.dwFlags |= SLDF_HAS_ID_LIST;
1018 if( This->sPath )
1019 header.dwFlags |= SLDF_HAS_LINK_INFO;
1020 if( This->sDescription )
1021 header.dwFlags |= SLDF_HAS_NAME;
1022 if( This->sWorkDir )
1023 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1024 if( This->sArgs )
1025 header.dwFlags |= SLDF_HAS_ARGS;
1026 if( This->sIcoPath )
1027 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1028 if( This->sProduct )
1029 header.dwFlags |= SLDF_HAS_LOGO3ID;
1030 if( This->sComponent )
1031 header.dwFlags |= SLDF_HAS_DARWINID;
1033 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1034 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1035 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1037 /* write the Shortcut header */
1038 r = IStream_Write( stm, &header, sizeof(header), &count );
1039 if( FAILED( r ) )
1041 ERR("Write failed at %d\n",__LINE__);
1042 return r;
1045 TRACE("Writing pidl\n");
1047 /* write the PIDL to the shortcut */
1048 if( This->pPidl )
1050 r = ILSaveToStream( stm, This->pPidl );
1051 if( FAILED( r ) )
1053 ERR("Failed to write PIDL at %d\n",__LINE__);
1054 return r;
1058 if( This->sPath )
1059 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1061 if( This->sDescription )
1062 r = Stream_WriteString( stm, This->sDescription );
1064 if( This->sPathRel )
1065 r = Stream_WriteString( stm, This->sPathRel );
1067 if( This->sWorkDir )
1068 r = Stream_WriteString( stm, This->sWorkDir );
1070 if( This->sArgs )
1071 r = Stream_WriteString( stm, This->sArgs );
1073 if( This->sIcoPath )
1074 r = Stream_WriteString( stm, This->sIcoPath );
1076 if( This->sProduct )
1077 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1079 if( This->sComponent )
1080 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1082 /* the last field is a single zero dword */
1083 zero = 0;
1084 r = IStream_Write( stm, &zero, sizeof zero, &count );
1086 return S_OK;
1089 /************************************************************************
1090 * IPersistStream_GetSizeMax (IPersistStream)
1092 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1093 IPersistStream* iface,
1094 ULARGE_INTEGER* pcbSize)
1096 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1098 TRACE("(%p)\n", This);
1100 return E_NOTIMPL;
1103 static const IPersistStreamVtbl psvt =
1105 IPersistStream_fnQueryInterface,
1106 IPersistStream_fnAddRef,
1107 IPersistStream_fnRelease,
1108 IPersistStream_fnGetClassID,
1109 IPersistStream_fnIsDirty,
1110 IPersistStream_fnLoad,
1111 IPersistStream_fnSave,
1112 IPersistStream_fnGetSizeMax
1115 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1117 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1118 return FALSE;
1119 return TRUE;
1122 /**************************************************************************
1123 * ShellLink_UpdatePath
1124 * update absolute path in sPath using relative path in sPathRel
1126 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1128 if (!path || !psPath)
1129 return E_INVALIDARG;
1131 if (!*psPath && sPathRel) {
1132 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1133 LPWSTR final = NULL;
1135 /* first try if [directory of link file] + [relative path] finds an existing file */
1137 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1138 if( !final )
1139 final = buffer;
1140 lstrcpyW(final, sPathRel);
1142 *abs_path = '\0';
1144 if (SHELL_ExistsFileW(buffer)) {
1145 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1146 lstrcpyW(abs_path, buffer);
1147 } else {
1148 /* try if [working directory] + [relative path] finds an existing file */
1149 if (sWorkDir) {
1150 lstrcpyW(buffer, sWorkDir);
1151 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1153 if (SHELL_ExistsFileW(buffer))
1154 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1155 lstrcpyW(abs_path, buffer);
1159 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1160 if (!*abs_path)
1161 lstrcpyW(abs_path, sPathRel);
1163 *psPath = wcsdup(abs_path);
1164 if (!*psPath)
1165 return E_OUTOFMEMORY;
1168 return S_OK;
1171 /**************************************************************************
1172 * IShellLink_ConstructFromFile
1174 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1175 LPCITEMIDLIST pidl, IUnknown **ppv)
1177 IShellLinkW* psl;
1179 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1181 if (SUCCEEDED(hr)) {
1182 IPersistFile* ppf;
1184 *ppv = NULL;
1186 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1188 if (SUCCEEDED(hr)) {
1189 WCHAR path[MAX_PATH];
1191 if (SHGetPathFromIDListW(pidl, path))
1192 hr = IPersistFile_Load(ppf, path, 0);
1193 else
1194 hr = E_FAIL;
1196 if (SUCCEEDED(hr))
1197 *ppv = (IUnknown*)psl;
1199 IPersistFile_Release(ppf);
1202 if (!*ppv)
1203 IShellLinkW_Release(psl);
1206 return hr;
1209 /**************************************************************************
1210 * IShellLinkA_QueryInterface
1212 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1214 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1215 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1218 /******************************************************************************
1219 * IShellLinkA_AddRef
1221 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1223 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1224 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1227 /******************************************************************************
1228 * IShellLinkA_Release
1230 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1232 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1233 return IShellLinkW_Release(&This->IShellLinkW_iface);
1236 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1237 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1239 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1240 HRESULT res = S_OK;
1242 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1243 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1245 if (This->sComponent || This->sProduct)
1246 return S_FALSE;
1248 if (cchMaxPath)
1249 pszFile[0] = 0;
1250 if (This->sPath && This->sPath[0])
1251 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1252 pszFile, cchMaxPath, NULL, NULL);
1253 else
1254 res = S_FALSE;
1256 if (pfd)
1258 memset(pfd, 0, sizeof(*pfd));
1260 if (res == S_OK)
1262 char path[MAX_PATH];
1263 WIN32_FILE_ATTRIBUTE_DATA fad;
1265 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1267 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1269 pfd->dwFileAttributes = fad.dwFileAttributes;
1270 pfd->ftCreationTime = fad.ftCreationTime;
1271 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1272 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1273 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1274 pfd->nFileSizeLow = fad.nFileSizeLow;
1277 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1279 if (GetShortPathNameA(path, path, MAX_PATH))
1281 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1285 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1286 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1287 wine_dbgstr_a(pfd->cAlternateFileName));
1290 return res;
1293 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1295 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1296 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1299 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1301 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1302 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1305 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1306 INT cchMaxName)
1308 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1310 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1312 if( cchMaxName )
1313 pszName[0] = 0;
1314 if( This->sDescription )
1315 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1316 pszName, cchMaxName, NULL, NULL);
1318 return S_OK;
1321 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1323 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1324 WCHAR *descrW;
1325 HRESULT hr;
1327 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1329 if (pszName)
1331 descrW = strdupAtoW(pszName);
1332 if (!descrW) return E_OUTOFMEMORY;
1334 else
1335 descrW = NULL;
1337 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1338 free(descrW);
1340 return hr;
1343 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1344 INT cchMaxPath)
1346 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1348 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1350 if( cchMaxPath )
1351 pszDir[0] = 0;
1352 if( This->sWorkDir )
1353 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1354 pszDir, cchMaxPath, NULL, NULL);
1356 return S_OK;
1359 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1361 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1362 WCHAR *dirW;
1363 HRESULT hr;
1365 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1367 dirW = strdupAtoW(pszDir);
1368 if (!dirW) return E_OUTOFMEMORY;
1370 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1371 free(dirW);
1373 return hr;
1376 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1378 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1380 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1382 if( cchMaxPath )
1383 pszArgs[0] = 0;
1384 if( This->sArgs )
1385 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1386 pszArgs, cchMaxPath, NULL, NULL);
1388 return S_OK;
1391 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1393 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1394 WCHAR *argsW;
1395 HRESULT hr;
1397 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1399 if (pszArgs)
1401 argsW = strdupAtoW(pszArgs);
1402 if (!argsW) return E_OUTOFMEMORY;
1404 else
1405 argsW = NULL;
1407 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1408 free(argsW);
1410 return hr;
1413 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1415 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1416 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1419 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1421 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1422 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1425 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1427 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1428 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1431 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1433 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1434 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1437 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1438 INT cchIconPath, INT *piIcon)
1440 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1442 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1444 *piIcon = This->iIcoNdx;
1446 if (This->sIcoPath)
1447 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1448 else
1449 pszIconPath[0] = 0;
1451 return S_OK;
1454 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1456 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1457 WCHAR *pathW = NULL;
1458 HRESULT hr;
1460 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1462 if (path)
1464 pathW = strdupAtoW(path);
1465 if (!pathW)
1466 return E_OUTOFMEMORY;
1469 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1470 free(pathW);
1472 return hr;
1475 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1476 DWORD dwReserved)
1478 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1479 WCHAR *pathW;
1480 HRESULT hr;
1482 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1484 pathW = strdupAtoW(pszPathRel);
1485 if (!pathW) return E_OUTOFMEMORY;
1487 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1488 free(pathW);
1490 return hr;
1493 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1495 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1497 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1499 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1502 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1504 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1505 HRESULT r;
1506 LPWSTR str;
1508 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1510 if (!pszFile) return E_INVALIDARG;
1512 str = strdupAtoW(pszFile);
1513 if (!str) return E_OUTOFMEMORY;
1515 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1516 free(str);
1518 return r;
1521 /**************************************************************************
1522 * IShellLink Implementation
1525 static const IShellLinkAVtbl slvt =
1527 IShellLinkA_fnQueryInterface,
1528 IShellLinkA_fnAddRef,
1529 IShellLinkA_fnRelease,
1530 IShellLinkA_fnGetPath,
1531 IShellLinkA_fnGetIDList,
1532 IShellLinkA_fnSetIDList,
1533 IShellLinkA_fnGetDescription,
1534 IShellLinkA_fnSetDescription,
1535 IShellLinkA_fnGetWorkingDirectory,
1536 IShellLinkA_fnSetWorkingDirectory,
1537 IShellLinkA_fnGetArguments,
1538 IShellLinkA_fnSetArguments,
1539 IShellLinkA_fnGetHotkey,
1540 IShellLinkA_fnSetHotkey,
1541 IShellLinkA_fnGetShowCmd,
1542 IShellLinkA_fnSetShowCmd,
1543 IShellLinkA_fnGetIconLocation,
1544 IShellLinkA_fnSetIconLocation,
1545 IShellLinkA_fnSetRelativePath,
1546 IShellLinkA_fnResolve,
1547 IShellLinkA_fnSetPath
1551 /**************************************************************************
1552 * IShellLinkW_fnQueryInterface
1554 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1555 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1557 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1559 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1561 *ppvObj = NULL;
1563 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1565 *ppvObj = &This->IShellLinkA_iface;
1567 else if(IsEqualIID(riid, &IID_IShellLinkW))
1569 *ppvObj = &This->IShellLinkW_iface;
1571 else if(IsEqualIID(riid, &IID_IPersistFile))
1573 *ppvObj = &This->IPersistFile_iface;
1575 else if(IsEqualIID(riid, &IID_IPersistStream))
1577 *ppvObj = &This->IPersistStream_iface;
1579 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1581 *ppvObj = &This->IShellLinkDataList_iface;
1583 else if(IsEqualIID(riid, &IID_IShellExtInit))
1585 *ppvObj = &This->IShellExtInit_iface;
1587 else if(IsEqualIID(riid, &IID_IContextMenu))
1589 *ppvObj = &This->IContextMenu_iface;
1591 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1593 *ppvObj = &This->IObjectWithSite_iface;
1595 else if(IsEqualIID(riid, &IID_IPropertyStore))
1597 *ppvObj = &This->IPropertyStore_iface;
1600 if(*ppvObj)
1602 IUnknown_AddRef((IUnknown*)*ppvObj);
1603 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1604 return S_OK;
1606 ERR("-- Interface: E_NOINTERFACE\n");
1607 return E_NOINTERFACE;
1610 /******************************************************************************
1611 * IShellLinkW_fnAddRef
1613 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1615 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1616 ULONG ref = InterlockedIncrement(&This->ref);
1618 TRACE("(%p)->(count=%lu)\n", This, ref - 1);
1620 return ref;
1623 /******************************************************************************
1624 * IShellLinkW_fnRelease
1626 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1628 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1629 ULONG refCount = InterlockedDecrement(&This->ref);
1631 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1633 if (refCount)
1634 return refCount;
1636 TRACE("-- destroying IShellLink(%p)\n",This);
1638 free(This->sIcoPath);
1639 free(This->sArgs);
1640 free(This->sWorkDir);
1641 free(This->sDescription);
1642 free(This->sPath);
1643 free(This->sPathRel);
1644 free(This->sProduct);
1645 free(This->sComponent);
1646 free(This->filepath);
1648 if (This->site)
1649 IUnknown_Release( This->site );
1651 if (This->pPidl)
1652 ILFree(This->pPidl);
1654 LocalFree(This);
1656 return 0;
1659 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1661 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1662 HRESULT res = S_OK;
1664 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1665 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1667 if (This->sComponent || This->sProduct)
1668 return S_FALSE;
1670 if (cchMaxPath)
1671 pszFile[0] = 0;
1672 if (This->sPath)
1673 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1674 else
1675 res = S_FALSE;
1677 if (pfd)
1679 memset(pfd, 0, sizeof(*pfd));
1681 if (res == S_OK)
1683 WCHAR path[MAX_PATH];
1684 WIN32_FILE_ATTRIBUTE_DATA fad;
1686 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1688 pfd->dwFileAttributes = fad.dwFileAttributes;
1689 pfd->ftCreationTime = fad.ftCreationTime;
1690 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1691 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1692 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1693 pfd->nFileSizeLow = fad.nFileSizeLow;
1696 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1698 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1700 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1704 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1705 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1706 wine_dbgstr_w(pfd->cAlternateFileName));
1709 return res;
1712 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1714 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1716 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1718 if (!This->pPidl)
1720 *ppidl = NULL;
1721 return S_FALSE;
1723 *ppidl = ILClone(This->pPidl);
1724 return S_OK;
1727 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1729 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1730 WCHAR path[MAX_PATH];
1732 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1734 if( This->pPidl )
1735 ILFree( This->pPidl );
1736 This->pPidl = ILClone( pidl );
1737 if( !This->pPidl )
1738 return E_FAIL;
1740 free( This->sPath );
1741 This->sPath = NULL;
1743 if ( SHGetPathFromIDListW( pidl, path ) )
1745 This->sPath = wcsdup(path);
1746 if (!This->sPath)
1747 return E_OUTOFMEMORY;
1750 This->bDirty = TRUE;
1752 return S_OK;
1755 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1757 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1759 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1761 pszName[0] = 0;
1762 if( This->sDescription )
1763 lstrcpynW( pszName, This->sDescription, cchMaxName );
1765 return S_OK;
1768 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1770 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1772 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1774 free(This->sDescription);
1775 if (pszName)
1777 This->sDescription = wcsdup(pszName);
1778 if ( !This->sDescription )
1779 return E_OUTOFMEMORY;
1781 else
1782 This->sDescription = NULL;
1783 This->bDirty = TRUE;
1785 return S_OK;
1788 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1790 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1792 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1794 if( cchMaxPath )
1795 pszDir[0] = 0;
1796 if( This->sWorkDir )
1797 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1799 return S_OK;
1802 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1804 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1806 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1808 free(This->sWorkDir);
1809 This->sWorkDir = wcsdup(pszDir);
1810 if ( !This->sWorkDir )
1811 return E_OUTOFMEMORY;
1812 This->bDirty = TRUE;
1814 return S_OK;
1817 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1819 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1821 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1823 if( cchMaxPath )
1824 pszArgs[0] = 0;
1825 if( This->sArgs )
1826 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1828 return S_OK;
1831 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1833 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1835 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1837 free(This->sArgs);
1838 if (pszArgs)
1840 This->sArgs = wcsdup(pszArgs);
1841 if ( !This->sArgs )
1842 return E_OUTOFMEMORY;
1844 else This->sArgs = NULL;
1846 This->bDirty = TRUE;
1848 return S_OK;
1851 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1853 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1855 TRACE("(%p)->(%p)\n",This, pwHotkey);
1857 *pwHotkey=This->wHotKey;
1859 return S_OK;
1862 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1864 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1866 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1868 This->wHotKey = wHotkey;
1869 This->bDirty = TRUE;
1871 return S_OK;
1874 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1876 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1878 TRACE("(%p)->(%p)\n",This, piShowCmd);
1880 *piShowCmd = This->iShowCmd;
1882 return S_OK;
1885 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1887 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1889 TRACE("(%p)->(%d)\n", This, iShowCmd);
1891 This->iShowCmd = iShowCmd;
1892 This->bDirty = TRUE;
1894 return S_OK;
1897 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1899 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1901 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1903 *piIcon = This->iIcoNdx;
1905 if (This->sIcoPath)
1906 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1907 else
1908 pszIconPath[0] = 0;
1910 return S_OK;
1913 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1915 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1917 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1919 free(This->sIcoPath);
1920 if (path)
1922 This->sIcoPath = wcsdup(path);
1923 if (!This->sIcoPath)
1924 return E_OUTOFMEMORY;
1926 else
1927 This->sIcoPath = NULL;
1928 This->iIcoNdx = icon;
1929 This->bDirty = TRUE;
1931 return S_OK;
1934 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1936 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1938 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1940 free(This->sPathRel);
1941 This->sPathRel = wcsdup(pszPathRel);
1942 if ( !This->sPathRel )
1943 return E_OUTOFMEMORY;
1944 This->bDirty = TRUE;
1946 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1949 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1951 HRESULT hr = S_OK;
1952 BOOL bSuccess;
1954 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1956 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1958 /*FIXME: use IResolveShellLink interface */
1960 if (!This->sPath && This->pPidl) {
1961 WCHAR buffer[MAX_PATH];
1963 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1965 if (bSuccess && *buffer) {
1966 This->sPath = wcsdup(buffer);
1967 if (!This->sPath)
1968 return E_OUTOFMEMORY;
1970 This->bDirty = TRUE;
1971 } else
1972 hr = S_OK; /* don't report an error occurred while just caching information */
1975 if (!This->sIcoPath && This->sPath) {
1976 This->sIcoPath = wcsdup(This->sPath);
1977 if (!This->sIcoPath)
1978 return E_OUTOFMEMORY;
1980 This->iIcoNdx = 0;
1982 This->bDirty = TRUE;
1985 return hr;
1988 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
1990 LPWSTR ret;
1991 LPCWSTR p;
1992 DWORD len;
1994 if( !str )
1995 return NULL;
1997 p = wcschr( str, ':' );
1998 if( !p )
1999 return NULL;
2000 len = p - str;
2001 ret = malloc(sizeof(WCHAR) * (len + 1));
2002 if( !ret )
2003 return ret;
2004 memcpy( ret, str, sizeof(WCHAR)*len );
2005 ret[len] = 0;
2006 return ret;
2009 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2011 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2012 WCHAR szGuid[39];
2013 HRESULT r;
2014 GUID guid;
2015 int len;
2017 while( str[0] )
2019 /* each segment must start with two colons */
2020 if( str[0] != ':' || str[1] != ':' )
2021 return E_FAIL;
2023 /* the last segment is just two colons */
2024 if( !str[2] )
2025 break;
2026 str += 2;
2028 /* there must be a colon straight after a guid */
2029 p = wcschr( str, ':' );
2030 if( !p )
2031 return E_FAIL;
2032 len = p - str;
2033 if( len != 38 )
2034 return E_FAIL;
2036 /* get the guid, and check it's validly formatted */
2037 memcpy( szGuid, str, sizeof(WCHAR)*len );
2038 szGuid[len] = 0;
2039 r = CLSIDFromString( szGuid, &guid );
2040 if( r != S_OK )
2041 return r;
2042 str = p + 1;
2044 /* match it up to a guid that we care about */
2045 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2046 szComponent = str;
2047 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2048 szProduct = str;
2049 else
2050 return E_FAIL;
2052 /* skip to the next field */
2053 str = wcschr( str, ':' );
2054 if( !str )
2055 return E_FAIL;
2058 /* we have to have a component for an advertised shortcut */
2059 if( !szComponent )
2060 return E_FAIL;
2062 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2063 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2065 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2066 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2068 return S_OK;
2071 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2073 const int label_sz = ARRAY_SIZE(volume->label);
2074 WCHAR drive[] = { path[0], ':', '\\', 0 };
2075 BOOL r;
2077 volume->type = GetDriveTypeW(drive);
2078 r = GetVolumeInformationW(drive, volume->label, label_sz,
2079 &volume->serial, NULL, NULL, NULL, 0);
2080 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2081 volume->type, volume->serial, debugstr_w(volume->label));
2082 return r;
2085 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2087 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2088 WCHAR buffer[MAX_PATH];
2089 LPWSTR fname, unquoted = NULL;
2090 HRESULT hr = S_OK;
2091 UINT len;
2093 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2095 if (!pszFile) return E_INVALIDARG;
2097 /* quotes at the ends of the string are stripped */
2098 len = lstrlenW(pszFile);
2099 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2101 unquoted = wcsdup(pszFile);
2102 PathUnquoteSpacesW(unquoted);
2103 pszFile = unquoted;
2106 /* any other quote marks are invalid */
2107 if (wcschr(pszFile, '"'))
2109 free(unquoted);
2110 return S_FALSE;
2113 free(This->sPath);
2114 This->sPath = NULL;
2116 free(This->sComponent);
2117 This->sComponent = NULL;
2119 if (This->pPidl)
2120 ILFree(This->pPidl);
2121 This->pPidl = NULL;
2123 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2125 if (*pszFile == '\0')
2126 *buffer = '\0';
2127 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2129 free(unquoted);
2130 return E_FAIL;
2132 else if(!PathFileExistsW(buffer) &&
2133 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2134 hr = S_FALSE;
2136 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2137 ShellLink_GetVolumeInfo(buffer, &This->volume);
2139 This->sPath = wcsdup(buffer);
2140 if (!This->sPath)
2142 free(unquoted);
2143 return E_OUTOFMEMORY;
2146 This->bDirty = TRUE;
2147 free(unquoted);
2149 return hr;
2152 /**************************************************************************
2153 * IShellLinkW Implementation
2156 static const IShellLinkWVtbl slvtw =
2158 IShellLinkW_fnQueryInterface,
2159 IShellLinkW_fnAddRef,
2160 IShellLinkW_fnRelease,
2161 IShellLinkW_fnGetPath,
2162 IShellLinkW_fnGetIDList,
2163 IShellLinkW_fnSetIDList,
2164 IShellLinkW_fnGetDescription,
2165 IShellLinkW_fnSetDescription,
2166 IShellLinkW_fnGetWorkingDirectory,
2167 IShellLinkW_fnSetWorkingDirectory,
2168 IShellLinkW_fnGetArguments,
2169 IShellLinkW_fnSetArguments,
2170 IShellLinkW_fnGetHotkey,
2171 IShellLinkW_fnSetHotkey,
2172 IShellLinkW_fnGetShowCmd,
2173 IShellLinkW_fnSetShowCmd,
2174 IShellLinkW_fnGetIconLocation,
2175 IShellLinkW_fnSetIconLocation,
2176 IShellLinkW_fnSetRelativePath,
2177 IShellLinkW_fnResolve,
2178 IShellLinkW_fnSetPath
2181 static HRESULT WINAPI
2182 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2184 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2185 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2188 static ULONG WINAPI
2189 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2191 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2192 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2195 static ULONG WINAPI
2196 ShellLink_DataList_Release( IShellLinkDataList* iface )
2198 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2199 return IShellLinkW_Release(&This->IShellLinkW_iface);
2202 static HRESULT WINAPI
2203 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2205 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2206 return E_NOTIMPL;
2209 static HRESULT WINAPI
2210 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2212 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2213 LPVOID block = NULL;
2214 HRESULT r = E_FAIL;
2216 TRACE("%p %08lx %p\n", iface, dwSig, ppDataBlock );
2218 switch (dwSig)
2220 case EXP_DARWIN_ID_SIG:
2221 if (!This->sComponent)
2222 break;
2223 block = shelllink_build_darwinid( This->sComponent, dwSig );
2224 r = S_OK;
2225 break;
2226 case EXP_SZ_LINK_SIG:
2227 case NT_CONSOLE_PROPS_SIG:
2228 case NT_FE_CONSOLE_PROPS_SIG:
2229 case EXP_SPECIAL_FOLDER_SIG:
2230 case EXP_SZ_ICON_SIG:
2231 FIXME("valid but unhandled datablock %08lx\n", dwSig);
2232 break;
2233 default:
2234 ERR("unknown datablock %08lx\n", dwSig);
2236 *ppDataBlock = block;
2237 return r;
2240 static HRESULT WINAPI
2241 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2243 FIXME("(%p)->(%lu): stub\n", iface, dwSig);
2244 return E_NOTIMPL;
2247 static HRESULT WINAPI
2248 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2250 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2251 DWORD flags = 0;
2253 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2255 /* FIXME: add more */
2256 if (This->sArgs)
2257 flags |= SLDF_HAS_ARGS;
2258 if (This->sComponent)
2259 flags |= SLDF_HAS_DARWINID;
2260 if (This->sIcoPath)
2261 flags |= SLDF_HAS_ICONLOCATION;
2262 if (This->sProduct)
2263 flags |= SLDF_HAS_LOGO3ID;
2264 if (This->pPidl)
2265 flags |= SLDF_HAS_ID_LIST;
2267 *pdwFlags = flags;
2269 return S_OK;
2272 static HRESULT WINAPI
2273 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2275 FIXME("(%p)->(%lu): stub\n", iface, dwFlags);
2276 return S_OK;
2279 static const IShellLinkDataListVtbl dlvt =
2281 ShellLink_DataList_QueryInterface,
2282 ShellLink_DataList_AddRef,
2283 ShellLink_DataList_Release,
2284 ShellLink_AddDataBlock,
2285 ShellLink_CopyDataBlock,
2286 ShellLink_RemoveDataBlock,
2287 ShellLink_GetFlags,
2288 ShellLink_SetFlags
2291 static HRESULT WINAPI
2292 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2294 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2295 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2298 static ULONG WINAPI
2299 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2301 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2302 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2305 static ULONG WINAPI
2306 ShellLink_ExtInit_Release( IShellExtInit* iface )
2308 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2309 return IShellLinkW_Release(&This->IShellLinkW_iface);
2312 /**************************************************************************
2313 * ShellLink implementation of IShellExtInit::Initialize()
2315 * Loads the shelllink from the dataobject the shell is pointing to.
2317 static HRESULT WINAPI
2318 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2319 IDataObject *pdtobj, HKEY hkeyProgID )
2321 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2322 FORMATETC format;
2323 STGMEDIUM stgm;
2324 UINT count;
2325 HRESULT r = E_FAIL;
2327 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2329 if( !pdtobj )
2330 return r;
2332 format.cfFormat = CF_HDROP;
2333 format.ptd = NULL;
2334 format.dwAspect = DVASPECT_CONTENT;
2335 format.lindex = -1;
2336 format.tymed = TYMED_HGLOBAL;
2338 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2339 return r;
2341 count = DragQueryFileW( stgm.hGlobal, -1, NULL, 0 );
2342 if( count == 1 )
2344 LPWSTR path;
2346 count = DragQueryFileW( stgm.hGlobal, 0, NULL, 0 );
2347 count++;
2348 path = malloc( count * sizeof(WCHAR) );
2349 if( path )
2351 IPersistFile *pf = &This->IPersistFile_iface;
2353 count = DragQueryFileW( stgm.hGlobal, 0, path, count );
2354 r = IPersistFile_Load( pf, path, 0 );
2355 free( path );
2358 ReleaseStgMedium( &stgm );
2360 return r;
2363 static const IShellExtInitVtbl eivt =
2365 ShellLink_ExtInit_QueryInterface,
2366 ShellLink_ExtInit_AddRef,
2367 ShellLink_ExtInit_Release,
2368 ShellLink_ExtInit_Initialize
2371 static HRESULT WINAPI
2372 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2374 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2375 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2378 static ULONG WINAPI
2379 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2381 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2382 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2385 static ULONG WINAPI
2386 ShellLink_ContextMenu_Release( IContextMenu* iface )
2388 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2389 return IShellLinkW_Release(&This->IShellLinkW_iface);
2392 static HRESULT WINAPI
2393 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2394 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2396 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2397 MENUITEMINFOW mii;
2398 int id = 1;
2400 TRACE("%p %p %u %u %u %u\n", This,
2401 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2403 if ( !hmenu )
2404 return E_INVALIDARG;
2406 memset( &mii, 0, sizeof mii );
2407 mii.cbSize = sizeof mii;
2408 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2409 mii.dwTypeData = (LPWSTR)L"Open";
2410 mii.cch = lstrlenW( mii.dwTypeData );
2411 mii.wID = idCmdFirst + id++;
2412 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2413 mii.fType = MFT_STRING;
2414 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2415 return E_FAIL;
2416 This->iIdOpen = 0;
2418 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2421 static LPWSTR
2422 shelllink_get_msi_component_path( LPWSTR component )
2424 LPWSTR path;
2425 DWORD r, sz = 0;
2427 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2428 if (r != ERROR_SUCCESS)
2429 return NULL;
2431 sz++;
2432 path = malloc( sz * sizeof(WCHAR) );
2433 r = CommandLineFromMsiDescriptor( component, path, &sz );
2434 if (r != ERROR_SUCCESS)
2436 free( path );
2437 path = NULL;
2440 TRACE("returning %s\n", debugstr_w( path ) );
2442 return path;
2445 static HRESULT WINAPI
2446 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2448 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2449 SHELLEXECUTEINFOW sei;
2450 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2451 LPWSTR args = NULL;
2452 LPWSTR path = NULL;
2453 HRESULT r;
2455 TRACE("%p %p\n", This, lpici );
2457 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2458 return E_INVALIDARG;
2460 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2462 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2463 return E_INVALIDARG;
2466 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2467 if ( FAILED( r ) )
2468 return r;
2470 if ( This->sComponent )
2472 path = shelllink_get_msi_component_path( This->sComponent );
2473 if (!path)
2474 return E_FAIL;
2476 else
2477 path = wcsdup( This->sPath );
2479 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2480 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2482 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2483 DWORD len = 2;
2485 if ( This->sArgs )
2486 len += lstrlenW( This->sArgs );
2487 if ( iciex->lpParametersW )
2488 len += lstrlenW( iciex->lpParametersW );
2490 args = malloc( len * sizeof(WCHAR) );
2491 args[0] = 0;
2492 if ( This->sArgs )
2493 lstrcatW( args, This->sArgs );
2494 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2496 lstrcatW( args, L" " );
2497 lstrcatW( args, iciex->lpParametersW );
2501 memset( &sei, 0, sizeof sei );
2502 sei.cbSize = sizeof sei;
2503 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2504 sei.lpFile = path;
2505 sei.nShow = This->iShowCmd;
2506 sei.lpIDList = This->pPidl;
2507 sei.lpDirectory = This->sWorkDir;
2508 sei.lpParameters = args;
2509 sei.lpVerb = L"Open";
2511 if( ShellExecuteExW( &sei ) )
2512 r = S_OK;
2513 else
2514 r = E_FAIL;
2516 free( args );
2517 free( path );
2519 return r;
2522 static HRESULT WINAPI
2523 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2524 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2526 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2528 FIXME("(%p)->(%Iu %u %p %p %u): stub\n", This,
2529 idCmd, uType, pwReserved, pszName, cchMax );
2531 return E_NOTIMPL;
2534 static const IContextMenuVtbl cmvt =
2536 ShellLink_ContextMenu_QueryInterface,
2537 ShellLink_ContextMenu_AddRef,
2538 ShellLink_ContextMenu_Release,
2539 ShellLink_QueryContextMenu,
2540 ShellLink_InvokeCommand,
2541 ShellLink_GetCommandString
2544 static HRESULT WINAPI
2545 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2547 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2548 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2551 static ULONG WINAPI
2552 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2554 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2555 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2558 static ULONG WINAPI
2559 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2561 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2562 return IShellLinkW_Release(&This->IShellLinkW_iface);
2565 static HRESULT WINAPI
2566 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2568 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2570 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2572 if ( !This->site )
2573 return E_FAIL;
2574 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2577 static HRESULT WINAPI
2578 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2580 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2582 TRACE("%p %p\n", iface, punk);
2584 if ( punk )
2585 IUnknown_AddRef( punk );
2587 if( This->site )
2588 IUnknown_Release( This->site );
2590 This->site = punk;
2592 return S_OK;
2595 static const IObjectWithSiteVtbl owsvt =
2597 ShellLink_ObjectWithSite_QueryInterface,
2598 ShellLink_ObjectWithSite_AddRef,
2599 ShellLink_ObjectWithSite_Release,
2600 ShellLink_SetSite,
2601 ShellLink_GetSite,
2604 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2606 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2607 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2610 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2612 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2613 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2616 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2618 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2619 return IShellLinkW_Release(&This->IShellLinkW_iface);
2622 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2624 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2625 FIXME("(%p)->(%p): stub\n", This, props);
2626 return E_NOTIMPL;
2629 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2631 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2632 FIXME("(%p)->(%ld %p): stub\n", This, propid, key);
2633 return E_NOTIMPL;
2636 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2638 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2639 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2640 return E_NOTIMPL;
2643 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2645 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2646 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2647 return S_OK;
2650 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2652 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2653 FIXME("(%p): stub\n", This);
2654 return S_OK;
2657 static const IPropertyStoreVtbl propertystorevtbl = {
2658 propertystore_QueryInterface,
2659 propertystore_AddRef,
2660 propertystore_Release,
2661 propertystore_GetCount,
2662 propertystore_GetAt,
2663 propertystore_GetValue,
2664 propertystore_SetValue,
2665 propertystore_Commit
2668 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2670 IShellLinkImpl * sl;
2671 HRESULT r;
2673 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2675 *obj = NULL;
2677 if (outer)
2678 return CLASS_E_NOAGGREGATION;
2680 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2681 if (!sl)
2682 return E_OUTOFMEMORY;
2684 sl->ref = 1;
2685 sl->IShellLinkA_iface.lpVtbl = &slvt;
2686 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2687 sl->IPersistFile_iface.lpVtbl = &pfvt;
2688 sl->IPersistStream_iface.lpVtbl = &psvt;
2689 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2690 sl->IShellExtInit_iface.lpVtbl = &eivt;
2691 sl->IContextMenu_iface.lpVtbl = &cmvt;
2692 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2693 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2694 sl->iShowCmd = SW_SHOWNORMAL;
2695 sl->bDirty = FALSE;
2696 sl->iIdOpen = -1;
2697 sl->site = NULL;
2698 sl->filepath = NULL;
2700 TRACE("(%p)\n", sl);
2702 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2703 IShellLinkW_Release( &sl->IShellLinkW_iface );
2704 return r;