ntdll: Use a separate memory allocation for the kernel stack.
[wine.git] / dlls / shell32 / shelllink.c
blob0a090febd2f42a25bbe7a55ca6d642e27e798c50
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"
49 #include "pidl.h"
50 #include "shell32_main.h"
51 #include "shlguid.h"
52 #include "shlwapi.h"
53 #include "msi.h"
54 #include "appmgmt.h"
56 #include "initguid.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(shell);
60 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
61 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
62 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
63 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
65 /* link file formats */
67 #include "pshpack1.h"
69 typedef struct _LINK_HEADER
71 DWORD dwSize; /* 0x00 size of the header - 0x4c */
72 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
73 DWORD dwFlags; /* 0x14 describes elements following */
74 DWORD dwFileAttr; /* 0x18 attributes of the target file */
75 FILETIME CreationTime; /* 0x1c creation time of target file */
76 FILETIME AccessTime; /* 0x24 access time of target file */
77 FILETIME WriteTime; /* 0x2c write time of target file */
78 DWORD dwFileSize; /* 0x34 File size of target file */
79 DWORD nIcon; /* 0x38 icon number or index */
80 DWORD fStartup; /* 0x3c startup type or window state of application */
81 WORD wHotKey; /* 0x40 hotkey */
82 WORD Reserved1; /* 0x42 reserved = 0 */
83 DWORD Reserved2; /* 0x44 reserved = 0 */
84 DWORD Reserved3; /* 0x48 reserved = 0 */
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 CreationTime;
138 SYSTEMTIME AccessTime;
139 SYSTEMTIME WriteTime;
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 WCHAR *p = malloc( len * sizeof(WCHAR) );
212 if( !p )
213 return p;
214 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
215 return p;
218 /**************************************************************************
219 * IPersistFile_QueryInterface
221 static HRESULT WINAPI IPersistFile_fnQueryInterface(
222 IPersistFile* iface,
223 REFIID riid,
224 LPVOID *ppvObj)
226 IShellLinkImpl *This = impl_from_IPersistFile(iface);
227 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
230 /******************************************************************************
231 * IPersistFile_AddRef
233 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
235 IShellLinkImpl *This = impl_from_IPersistFile(iface);
236 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
239 /******************************************************************************
240 * IPersistFile_Release
242 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
244 IShellLinkImpl *This = impl_from_IPersistFile(iface);
245 return IShellLinkW_Release(&This->IShellLinkW_iface);
248 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
250 IShellLinkImpl *This = impl_from_IPersistFile(iface);
252 TRACE("(%p)->(%p)\n", This, pClassID);
254 *pClassID = CLSID_ShellLink;
256 return S_OK;
259 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
261 IShellLinkImpl *This = impl_from_IPersistFile(iface);
263 TRACE("(%p)\n",This);
265 if (This->bDirty)
266 return S_OK;
268 return S_FALSE;
271 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
273 IShellLinkImpl *This = impl_from_IPersistFile(iface);
274 IPersistStream *StreamThis = &This->IPersistStream_iface;
275 HRESULT r;
276 IStream *stm;
278 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
280 if( dwMode == 0 ) dwMode = STGM_READ;
281 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
282 if( SUCCEEDED( r ) )
284 r = IPersistStream_Load(StreamThis, stm);
285 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
286 IStream_Release( stm );
288 /* update file path */
289 free(This->filepath);
290 This->filepath = wcsdup(pszFileName);
292 This->bDirty = FALSE;
294 TRACE("-- returning hr %08lx\n", r);
295 return r;
298 BOOL run_winemenubuilder( const WCHAR *args )
300 LONG len;
301 LPWSTR buffer;
302 STARTUPINFOW si;
303 PROCESS_INFORMATION pi;
304 BOOL ret;
305 WCHAR app[MAX_PATH];
306 void *redir;
308 GetSystemDirectoryW( app, MAX_PATH );
309 lstrcatW( app, L"\\winemenubuilder.exe" );
311 len = (lstrlenW( app ) + lstrlenW( args ) + 1) * sizeof(WCHAR);
312 buffer = malloc( len );
313 if( !buffer )
314 return FALSE;
316 lstrcpyW( buffer, app );
317 lstrcatW( buffer, args );
319 TRACE("starting %s\n",debugstr_w(buffer));
321 memset(&si, 0, sizeof(si));
322 si.cb = sizeof(si);
324 Wow64DisableWow64FsRedirection( &redir );
325 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi );
326 Wow64RevertWow64FsRedirection( redir );
328 free( buffer );
330 if (ret)
332 CloseHandle( pi.hProcess );
333 CloseHandle( pi.hThread );
336 return ret;
339 static BOOL StartLinkProcessor( LPCOLESTR szLink )
341 LONG len;
342 LPWSTR buffer;
343 BOOL ret;
345 len = (lstrlenW( szLink ) + 7) * sizeof(WCHAR);
346 buffer = malloc( len );
347 if( !buffer )
348 return FALSE;
350 swprintf( buffer, len, L" -w \"%s\"", szLink );
351 ret = run_winemenubuilder( buffer );
352 free( buffer );
353 return ret;
356 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
358 IShellLinkImpl *This = impl_from_IPersistFile(iface);
359 IPersistStream *StreamThis = &This->IPersistStream_iface;
360 HRESULT r;
361 IStream *stm;
363 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
365 if (!pszFileName)
367 if (!This->filepath) return S_OK;
369 pszFileName = This->filepath;
370 fRemember = FALSE;
373 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_DENY_WRITE, &stm );
374 if( SUCCEEDED( r ) )
376 r = IPersistStream_Save(StreamThis, stm, FALSE);
377 IStream_Release( stm );
379 if( SUCCEEDED( r ) )
381 StartLinkProcessor( pszFileName );
383 if (fRemember)
385 /* update file path */
386 free(This->filepath);
387 This->filepath = wcsdup(pszFileName);
390 This->bDirty = FALSE;
392 else
394 DeleteFileW( pszFileName );
395 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
399 return r;
402 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR filename)
404 IShellLinkImpl *This = impl_from_IPersistFile(iface);
405 FIXME("(%p)->(%s): stub\n", This, debugstr_w(filename));
406 return S_OK;
409 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *filename)
411 IShellLinkImpl *This = impl_from_IPersistFile(iface);
413 TRACE("(%p)->(%p)\n", This, filename);
415 if (!This->filepath)
417 *filename = NULL;
418 return S_FALSE;
421 *filename = CoTaskMemAlloc((lstrlenW(This->filepath) + 1) * sizeof(WCHAR));
422 if (!*filename) return E_OUTOFMEMORY;
424 lstrcpyW(*filename, This->filepath);
426 return S_OK;
429 static const IPersistFileVtbl pfvt =
431 IPersistFile_fnQueryInterface,
432 IPersistFile_fnAddRef,
433 IPersistFile_fnRelease,
434 IPersistFile_fnGetClassID,
435 IPersistFile_fnIsDirty,
436 IPersistFile_fnLoad,
437 IPersistFile_fnSave,
438 IPersistFile_fnSaveCompleted,
439 IPersistFile_fnGetCurFile
442 /************************************************************************
443 * IPersistStream_QueryInterface
445 static HRESULT WINAPI IPersistStream_fnQueryInterface(
446 IPersistStream* iface,
447 REFIID riid,
448 VOID** ppvObj)
450 IShellLinkImpl *This = impl_from_IPersistStream(iface);
451 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
454 /************************************************************************
455 * IPersistStream_Release
457 static ULONG WINAPI IPersistStream_fnRelease(
458 IPersistStream* iface)
460 IShellLinkImpl *This = impl_from_IPersistStream(iface);
461 return IShellLinkW_Release(&This->IShellLinkW_iface);
464 /************************************************************************
465 * IPersistStream_AddRef
467 static ULONG WINAPI IPersistStream_fnAddRef(
468 IPersistStream* iface)
470 IShellLinkImpl *This = impl_from_IPersistStream(iface);
471 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
474 /************************************************************************
475 * IPersistStream_GetClassID
478 static HRESULT WINAPI IPersistStream_fnGetClassID(
479 IPersistStream* iface,
480 CLSID* pClassID)
482 IShellLinkImpl *This = impl_from_IPersistStream(iface);
483 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
486 /************************************************************************
487 * IPersistStream_IsDirty (IPersistStream)
489 static HRESULT WINAPI IPersistStream_fnIsDirty(
490 IPersistStream* iface)
492 IShellLinkImpl *This = impl_from_IPersistStream(iface);
494 TRACE("(%p)\n", This);
496 return S_OK;
500 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
502 DWORD count;
503 USHORT len;
504 LPVOID temp;
505 LPWSTR str;
506 HRESULT r;
508 TRACE("%p\n", stm);
510 count = 0;
511 r = IStream_Read(stm, &len, sizeof(len), &count);
512 if ( FAILED (r) || ( count != sizeof(len) ) )
513 return E_FAIL;
515 if( unicode )
516 len *= sizeof (WCHAR);
518 TRACE("reading %d\n", len);
519 temp = malloc(len + sizeof(WCHAR));
520 if( !temp )
521 return E_OUTOFMEMORY;
522 count = 0;
523 r = IStream_Read(stm, temp, len, &count);
524 if( FAILED (r) || ( count != len ) )
526 free( temp );
527 return E_FAIL;
530 TRACE("read %s\n", debugstr_an(temp,len));
532 /* convert to unicode if necessary */
533 if( !unicode )
535 count = MultiByteToWideChar( CP_ACP, 0, temp, len, NULL, 0 );
536 str = malloc( (count + 1) * sizeof(WCHAR) );
537 if( !str )
539 free( temp );
540 return E_OUTOFMEMORY;
542 MultiByteToWideChar( CP_ACP, 0, temp, len, str, count );
543 free( temp );
545 else
547 count /= 2;
548 str = temp;
550 str[count] = 0;
552 *pstr = str;
554 return S_OK;
557 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
559 DWORD size;
560 ULONG count;
561 HRESULT r;
562 struct sized_chunk {
563 DWORD size;
564 unsigned char data[1];
565 } *chunk;
567 TRACE("%p\n",stm);
569 r = IStream_Read( stm, &size, sizeof(size), &count );
570 if( FAILED( r ) || count != sizeof(size) )
571 return E_FAIL;
573 chunk = malloc( size );
574 if( !chunk )
575 return E_OUTOFMEMORY;
577 chunk->size = size;
578 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
579 if( FAILED( r ) || count != (size - sizeof(size)) )
581 free( chunk );
582 return E_FAIL;
585 TRACE("Read %ld bytes\n",chunk->size);
587 *data = chunk;
589 return S_OK;
592 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
594 const int label_sz = ARRAY_SIZE(volume->label);
595 LPSTR label;
596 int len;
598 volume->serial = vol->dwVolSerial;
599 volume->type = vol->dwType;
601 if( !vol->dwVolLabelOfs )
602 return FALSE;
603 if( vol->dwSize <= vol->dwVolLabelOfs )
604 return FALSE;
605 len = vol->dwSize - vol->dwVolLabelOfs;
607 label = (LPSTR) vol;
608 label += vol->dwVolLabelOfs;
609 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
611 return TRUE;
614 static LPWSTR Stream_LoadPath( LPCSTR p, DWORD maxlen )
616 int len = 0, wlen;
617 LPWSTR path;
619 while( (len < maxlen) && p[len] )
620 len++;
622 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
623 path = malloc((wlen + 1) * sizeof(WCHAR));
624 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
625 path[wlen] = 0;
627 return path;
630 static HRESULT Stream_LoadLocation( IStream *stm,
631 volume_info *volume, LPWSTR *path )
633 char *p = NULL;
634 LOCATION_INFO *loc;
635 HRESULT r;
636 DWORD n;
638 r = Stream_ReadChunk( stm, (LPVOID*) &p );
639 if( FAILED(r) )
640 return r;
642 loc = (LOCATION_INFO*) p;
643 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
645 free( p );
646 return E_FAIL;
649 /* if there's valid local volume information, load it */
650 if( loc->dwVolTableOfs &&
651 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
653 LOCAL_VOLUME_INFO *volume_info;
655 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
656 Stream_LoadVolume( volume_info, volume );
659 /* if there's a local path, load it */
660 n = loc->dwLocalPathOfs;
661 if( n && (n < loc->dwTotalSize) )
662 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
664 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
665 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
667 free( p );
668 return S_OK;
672 * The format of the advertised shortcut info seems to be:
674 * Offset Description
675 * ------ -----------
677 * 0 Length of the block (4 bytes, usually 0x314)
678 * 4 tag (dword)
679 * 8 string data in ANSI
680 * 8+0x104 string data in UNICODE
682 * In the original Win32 implementation the buffers are not initialized
683 * to zero, so data trailing the string is random garbage.
685 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
687 DWORD size;
688 ULONG count;
689 HRESULT r;
690 EXP_DARWIN_LINK buffer;
692 TRACE("%p\n",stm);
694 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
695 if( FAILED( r ) )
696 return r;
698 /* make sure that we read the size of the structure even on error */
699 size = sizeof buffer - sizeof (DWORD);
700 if( buffer.dbh.cbSize != sizeof buffer )
702 ERR("Ooops. This structure is not as expected...\n");
703 return E_FAIL;
706 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
707 if( FAILED( r ) )
708 return r;
710 if( count != size )
711 return E_FAIL;
713 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
715 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
717 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
718 return E_FAIL;
721 *str = malloc( (lstrlenW(buffer.szwDarwinID) + 1) * sizeof(WCHAR) );
722 lstrcpyW( *str, buffer.szwDarwinID );
724 return S_OK;
727 /************************************************************************
728 * IPersistStream_Load (IPersistStream)
730 static HRESULT WINAPI IPersistStream_fnLoad(
731 IPersistStream* iface,
732 IStream* stm)
734 LINK_HEADER hdr;
735 ULONG dwBytesRead;
736 BOOL unicode;
737 HRESULT r;
738 DWORD zero;
740 IShellLinkImpl *This = impl_from_IPersistStream(iface);
742 TRACE("%p %p\n", This, stm);
744 if( !stm )
745 return STG_E_INVALIDPOINTER;
747 dwBytesRead = 0;
748 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
749 if( FAILED( r ) )
750 return r;
752 if( dwBytesRead != sizeof(hdr))
753 return E_FAIL;
754 if( hdr.dwSize != sizeof(hdr))
755 return E_FAIL;
756 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
757 return E_FAIL;
759 /* free all the old stuff */
760 ILFree(This->pPidl);
761 This->pPidl = NULL;
762 memset( &This->volume, 0, sizeof This->volume );
763 free(This->sPath);
764 This->sPath = NULL;
765 free(This->sDescription);
766 This->sDescription = NULL;
767 free(This->sPathRel);
768 This->sPathRel = NULL;
769 free(This->sWorkDir);
770 This->sWorkDir = NULL;
771 free(This->sArgs);
772 This->sArgs = NULL;
773 free(This->sIcoPath);
774 This->sIcoPath = NULL;
775 free(This->sProduct);
776 This->sProduct = NULL;
777 free(This->sComponent);
778 This->sComponent = NULL;
780 This->wHotKey = hdr.wHotKey;
781 This->iIcoNdx = hdr.nIcon;
782 FileTimeToSystemTime (&hdr.CreationTime, &This->CreationTime);
783 FileTimeToSystemTime (&hdr.AccessTime, &This->AccessTime);
784 FileTimeToSystemTime (&hdr.WriteTime, &This->WriteTime);
785 if (TRACE_ON(shell))
787 WCHAR sTemp[MAX_PATH];
788 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->CreationTime, NULL, sTemp, ARRAY_SIZE(sTemp));
789 TRACE("-- CreationTime: %s\n", debugstr_w(sTemp) );
790 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->AccessTime, NULL, sTemp, ARRAY_SIZE(sTemp));
791 TRACE("-- AccessTime: %s\n", debugstr_w(sTemp) );
792 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &This->WriteTime, NULL, sTemp, ARRAY_SIZE(sTemp));
793 TRACE("-- WriteTime: %s\n", debugstr_w(sTemp) );
796 /* load all the new stuff */
797 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
799 r = ILLoadFromStream( stm, &This->pPidl );
800 if( FAILED( r ) )
801 return r;
803 pdump(This->pPidl);
805 /* load the location information */
806 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
807 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
808 if( FAILED( r ) )
809 goto end;
811 unicode = hdr.dwFlags & SLDF_UNICODE;
812 if( hdr.dwFlags & SLDF_HAS_NAME )
814 r = Stream_LoadString( stm, unicode, &This->sDescription );
815 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
817 if( FAILED( r ) )
818 goto end;
820 if( hdr.dwFlags & SLDF_HAS_RELPATH )
822 r = Stream_LoadString( stm, unicode, &This->sPathRel );
823 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
825 if( FAILED( r ) )
826 goto end;
828 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
830 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
831 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
833 if( FAILED( r ) )
834 goto end;
836 if( hdr.dwFlags & SLDF_HAS_ARGS )
838 r = Stream_LoadString( stm, unicode, &This->sArgs );
839 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
841 if( FAILED( r ) )
842 goto end;
844 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
846 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
847 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
849 if( FAILED( r ) )
850 goto end;
852 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
854 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
855 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
857 if( FAILED( r ) )
858 goto end;
860 if( hdr.dwFlags & SLDF_HAS_DARWINID )
862 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
863 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
865 if( FAILED( r ) )
866 goto end;
868 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
869 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
871 /* Some lnk files have extra data blocks starting with a
872 * DATABLOCK_HEADER. For instance EXP_SPECIAL_FOLDER and an unknown
873 * one with a 0xa0000003 signature. However these don't seem to matter
874 * too much.
876 WARN("Last word was not zero\n");
879 TRACE("OK\n");
881 pdump (This->pPidl);
883 return S_OK;
884 end:
885 return r;
888 /************************************************************************
889 * Stream_WriteString
891 * Helper function for IPersistStream_Save. Writes a unicode string
892 * with terminating nul byte to a stream, preceded by the its length.
894 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
896 USHORT len = lstrlenW( str ) + 1;
897 DWORD count;
898 HRESULT r;
900 r = IStream_Write( stm, &len, sizeof(len), &count );
901 if( FAILED( r ) )
902 return r;
904 len *= sizeof(WCHAR);
906 r = IStream_Write( stm, str, len, &count );
907 if( FAILED( r ) )
908 return r;
910 return S_OK;
913 /************************************************************************
914 * Stream_WriteLocationInfo
916 * Writes the location info to a stream
918 * FIXME: One day we might want to write the network volume information
919 * and the final path.
920 * Figure out how Windows deals with unicode paths here.
922 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
923 volume_info *volume )
925 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
926 LOCAL_VOLUME_INFO *vol;
927 LOCATION_INFO *loc;
928 LPSTR szLabel, szPath, szFinalPath;
929 ULONG count = 0;
930 HRESULT hr;
932 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
934 /* figure out the size of everything */
935 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
936 NULL, 0, NULL, NULL );
937 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
938 NULL, 0, NULL, NULL );
939 volume_info_size = sizeof *vol + label_size;
940 final_path_size = 1;
941 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
943 /* create pointers to everything */
944 loc = calloc(1, total_size);
945 vol = (LOCAL_VOLUME_INFO*) &loc[1];
946 szLabel = (LPSTR) &vol[1];
947 szPath = &szLabel[label_size];
948 szFinalPath = &szPath[path_size];
950 /* fill in the location information header */
951 loc->dwTotalSize = total_size;
952 loc->dwHeaderSize = sizeof (*loc);
953 loc->dwFlags = 1;
954 loc->dwVolTableOfs = sizeof (*loc);
955 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
956 loc->dwNetworkVolTableOfs = 0;
957 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
959 /* fill in the volume information */
960 vol->dwSize = volume_info_size;
961 vol->dwType = volume->type;
962 vol->dwVolSerial = volume->serial;
963 vol->dwVolLabelOfs = sizeof (*vol);
965 /* copy in the strings */
966 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
967 szLabel, label_size, NULL, NULL );
968 WideCharToMultiByte( CP_ACP, 0, path, -1,
969 szPath, path_size, NULL, NULL );
970 szFinalPath[0] = 0;
972 hr = IStream_Write( stm, loc, total_size, &count );
973 free(loc);
975 return hr;
978 static EXP_DARWIN_LINK* shelllink_build_darwinid( LPCWSTR string, DWORD magic )
980 EXP_DARWIN_LINK *buffer;
982 buffer = LocalAlloc( LMEM_ZEROINIT, sizeof *buffer );
983 buffer->dbh.cbSize = sizeof *buffer;
984 buffer->dbh.dwSignature = magic;
985 lstrcpynW( buffer->szwDarwinID, string, MAX_PATH );
986 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer->szDarwinID, MAX_PATH, NULL, NULL );
988 return buffer;
991 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
993 EXP_DARWIN_LINK *buffer;
994 ULONG count;
996 TRACE("%p\n",stm);
998 buffer = shelllink_build_darwinid( string, magic );
1000 return IStream_Write( stm, buffer, buffer->dbh.cbSize, &count );
1003 /************************************************************************
1004 * IPersistStream_Save (IPersistStream)
1006 * FIXME: makes assumptions about byte order
1008 static HRESULT WINAPI IPersistStream_fnSave(
1009 IPersistStream* iface,
1010 IStream* stm,
1011 BOOL fClearDirty)
1013 LINK_HEADER header;
1014 ULONG count;
1015 DWORD zero;
1016 HRESULT r;
1018 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1020 TRACE("%p %p %x\n", This, stm, fClearDirty);
1022 memset(&header, 0, sizeof(header));
1023 header.dwSize = sizeof(header);
1024 header.fStartup = This->iShowCmd;
1025 header.MagicGuid = CLSID_ShellLink;
1027 header.wHotKey = This->wHotKey;
1028 header.nIcon = This->iIcoNdx;
1029 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1030 if( This->pPidl )
1031 header.dwFlags |= SLDF_HAS_ID_LIST;
1032 if( This->sPath )
1033 header.dwFlags |= SLDF_HAS_LINK_INFO;
1034 if( This->sDescription )
1035 header.dwFlags |= SLDF_HAS_NAME;
1036 if( This->sWorkDir )
1037 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1038 if( This->sArgs )
1039 header.dwFlags |= SLDF_HAS_ARGS;
1040 if( This->sIcoPath )
1041 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1042 if( This->sProduct )
1043 header.dwFlags |= SLDF_HAS_LOGO3ID;
1044 if( This->sComponent )
1045 header.dwFlags |= SLDF_HAS_DARWINID;
1047 SystemTimeToFileTime ( &This->CreationTime, &header.CreationTime );
1048 SystemTimeToFileTime ( &This->AccessTime, &header.AccessTime );
1049 SystemTimeToFileTime ( &This->WriteTime, &header.WriteTime );
1051 /* write the Shortcut header */
1052 r = IStream_Write( stm, &header, sizeof(header), &count );
1053 if( FAILED( r ) )
1055 ERR("Write failed at %d\n",__LINE__);
1056 return r;
1059 TRACE("Writing pidl\n");
1061 /* write the PIDL to the shortcut */
1062 if( This->pPidl )
1064 r = ILSaveToStream( stm, This->pPidl );
1065 if( FAILED( r ) )
1067 ERR("Failed to write PIDL at %d\n",__LINE__);
1068 return r;
1072 if( This->sPath )
1073 Stream_WriteLocationInfo( stm, This->sPath, &This->volume );
1075 if( This->sDescription )
1076 r = Stream_WriteString( stm, This->sDescription );
1078 if( This->sPathRel )
1079 r = Stream_WriteString( stm, This->sPathRel );
1081 if( This->sWorkDir )
1082 r = Stream_WriteString( stm, This->sWorkDir );
1084 if( This->sArgs )
1085 r = Stream_WriteString( stm, This->sArgs );
1087 if( This->sIcoPath )
1088 r = Stream_WriteString( stm, This->sIcoPath );
1090 if( This->sProduct )
1091 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1093 if( This->sComponent )
1094 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1096 /* the last field is a single zero dword */
1097 zero = 0;
1098 r = IStream_Write( stm, &zero, sizeof zero, &count );
1100 return S_OK;
1103 /************************************************************************
1104 * IPersistStream_GetSizeMax (IPersistStream)
1106 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1107 IPersistStream* iface,
1108 ULARGE_INTEGER* pcbSize)
1110 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1112 TRACE("(%p)\n", This);
1114 return E_NOTIMPL;
1117 static const IPersistStreamVtbl psvt =
1119 IPersistStream_fnQueryInterface,
1120 IPersistStream_fnAddRef,
1121 IPersistStream_fnRelease,
1122 IPersistStream_fnGetClassID,
1123 IPersistStream_fnIsDirty,
1124 IPersistStream_fnLoad,
1125 IPersistStream_fnSave,
1126 IPersistStream_fnGetSizeMax
1129 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1131 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1132 return FALSE;
1133 return TRUE;
1136 /**************************************************************************
1137 * ShellLink_UpdatePath
1138 * update absolute path in sPath using relative path in sPathRel
1140 static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1142 if (!path || !psPath)
1143 return E_INVALIDARG;
1145 if (!*psPath && sPathRel) {
1146 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1147 LPWSTR final = NULL;
1149 /* first try if [directory of link file] + [relative path] finds an existing file */
1151 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1152 if( !final )
1153 final = buffer;
1154 lstrcpyW(final, sPathRel);
1156 *abs_path = '\0';
1158 if (SHELL_ExistsFileW(buffer)) {
1159 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1160 lstrcpyW(abs_path, buffer);
1161 } else {
1162 /* try if [working directory] + [relative path] finds an existing file */
1163 if (sWorkDir) {
1164 lstrcpyW(buffer, sWorkDir);
1165 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1167 if (SHELL_ExistsFileW(buffer))
1168 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1169 lstrcpyW(abs_path, buffer);
1173 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1174 if (!*abs_path)
1175 lstrcpyW(abs_path, sPathRel);
1177 *psPath = malloc((lstrlenW(abs_path) + 1) * sizeof(WCHAR));
1178 if (!*psPath)
1179 return E_OUTOFMEMORY;
1181 lstrcpyW(*psPath, abs_path);
1184 return S_OK;
1187 /**************************************************************************
1188 * IShellLink_ConstructFromFile
1190 HRESULT IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1191 LPCITEMIDLIST pidl, IUnknown **ppv)
1193 IShellLinkW* psl;
1195 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1197 if (SUCCEEDED(hr)) {
1198 IPersistFile* ppf;
1200 *ppv = NULL;
1202 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1204 if (SUCCEEDED(hr)) {
1205 WCHAR path[MAX_PATH];
1207 if (SHGetPathFromIDListW(pidl, path))
1208 hr = IPersistFile_Load(ppf, path, 0);
1209 else
1210 hr = E_FAIL;
1212 if (SUCCEEDED(hr))
1213 *ppv = (IUnknown*)psl;
1215 IPersistFile_Release(ppf);
1218 if (!*ppv)
1219 IShellLinkW_Release(psl);
1222 return hr;
1225 /**************************************************************************
1226 * IShellLinkA_QueryInterface
1228 static HRESULT WINAPI IShellLinkA_fnQueryInterface(IShellLinkA *iface, REFIID riid, void **ppvObj)
1230 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1231 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObj);
1234 /******************************************************************************
1235 * IShellLinkA_AddRef
1237 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA *iface)
1239 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1240 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
1243 /******************************************************************************
1244 * IShellLinkA_Release
1246 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA *iface)
1248 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1249 return IShellLinkW_Release(&This->IShellLinkW_iface);
1252 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA *iface, LPSTR pszFile, INT cchMaxPath,
1253 WIN32_FIND_DATAA *pfd, DWORD fFlags)
1255 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1256 HRESULT res = S_OK;
1258 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1259 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1261 if (This->sComponent || This->sProduct)
1262 return S_FALSE;
1264 if (cchMaxPath)
1265 pszFile[0] = 0;
1266 if (This->sPath && This->sPath[0])
1267 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1268 pszFile, cchMaxPath, NULL, NULL);
1269 else
1270 res = S_FALSE;
1272 if (pfd)
1274 memset(pfd, 0, sizeof(*pfd));
1276 if (res == S_OK)
1278 char path[MAX_PATH];
1279 WIN32_FILE_ATTRIBUTE_DATA fad;
1281 WideCharToMultiByte(CP_ACP, 0, This->sPath, -1, path, MAX_PATH, NULL, NULL);
1283 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1285 pfd->dwFileAttributes = fad.dwFileAttributes;
1286 pfd->ftCreationTime = fad.ftCreationTime;
1287 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1288 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1289 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1290 pfd->nFileSizeLow = fad.nFileSizeLow;
1293 lstrcpyA(pfd->cFileName, PathFindFileNameA(path));
1295 if (GetShortPathNameA(path, path, MAX_PATH))
1297 lstrcpyA(pfd->cAlternateFileName, PathFindFileNameA(path));
1301 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1302 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_a(pfd->cFileName),
1303 wine_dbgstr_a(pfd->cAlternateFileName));
1306 return res;
1309 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA *iface, LPITEMIDLIST *ppidl)
1311 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1312 return IShellLinkW_GetIDList(&This->IShellLinkW_iface, ppidl);
1315 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA *iface, LPCITEMIDLIST pidl)
1317 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1318 return IShellLinkW_SetIDList(&This->IShellLinkW_iface, pidl);
1321 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA *iface, LPSTR pszName,
1322 INT cchMaxName)
1324 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1326 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1328 if( cchMaxName )
1329 pszName[0] = 0;
1330 if( This->sDescription )
1331 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1332 pszName, cchMaxName, NULL, NULL);
1334 return S_OK;
1337 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA *iface, LPCSTR pszName)
1339 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1340 WCHAR *descrW;
1341 HRESULT hr;
1343 TRACE("(%p)->(pName=%s)\n", This, debugstr_a(pszName));
1345 if (pszName)
1347 descrW = heap_strdupAtoW(pszName);
1348 if (!descrW) return E_OUTOFMEMORY;
1350 else
1351 descrW = NULL;
1353 hr = IShellLinkW_SetDescription(&This->IShellLinkW_iface, descrW);
1354 free(descrW);
1356 return hr;
1359 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA *iface, LPSTR pszDir,
1360 INT cchMaxPath)
1362 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1364 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1366 if( cchMaxPath )
1367 pszDir[0] = 0;
1368 if( This->sWorkDir )
1369 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1370 pszDir, cchMaxPath, NULL, NULL);
1372 return S_OK;
1375 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA *iface, LPCSTR pszDir)
1377 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1378 WCHAR *dirW;
1379 HRESULT hr;
1381 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1383 dirW = heap_strdupAtoW(pszDir);
1384 if (!dirW) return E_OUTOFMEMORY;
1386 hr = IShellLinkW_SetWorkingDirectory(&This->IShellLinkW_iface, dirW);
1387 free(dirW);
1389 return hr;
1392 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA *iface, LPSTR pszArgs, INT cchMaxPath)
1394 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1396 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1398 if( cchMaxPath )
1399 pszArgs[0] = 0;
1400 if( This->sArgs )
1401 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1402 pszArgs, cchMaxPath, NULL, NULL);
1404 return S_OK;
1407 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA *iface, LPCSTR pszArgs)
1409 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1410 WCHAR *argsW;
1411 HRESULT hr;
1413 TRACE("(%p)->(args=%s)\n",This, debugstr_a(pszArgs));
1415 if (pszArgs)
1417 argsW = heap_strdupAtoW(pszArgs);
1418 if (!argsW) return E_OUTOFMEMORY;
1420 else
1421 argsW = NULL;
1423 hr = IShellLinkW_SetArguments(&This->IShellLinkW_iface, argsW);
1424 free(argsW);
1426 return hr;
1429 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA *iface, WORD *pwHotkey)
1431 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1432 return IShellLinkW_GetHotkey(&This->IShellLinkW_iface, pwHotkey);
1435 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA *iface, WORD wHotkey)
1437 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1438 return IShellLinkW_SetHotkey(&This->IShellLinkW_iface, wHotkey);
1441 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA *iface, INT *piShowCmd)
1443 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1444 return IShellLinkW_GetShowCmd(&This->IShellLinkW_iface, piShowCmd);
1447 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA *iface, INT iShowCmd)
1449 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1450 return IShellLinkW_SetShowCmd(&This->IShellLinkW_iface, iShowCmd);
1453 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA *iface, LPSTR pszIconPath,
1454 INT cchIconPath, INT *piIcon)
1456 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1458 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1460 *piIcon = This->iIcoNdx;
1462 if (This->sIcoPath)
1463 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1464 else
1465 pszIconPath[0] = 0;
1467 return S_OK;
1470 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA *iface, LPCSTR path, INT icon)
1472 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1473 WCHAR *pathW = NULL;
1474 HRESULT hr;
1476 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_a(path), icon);
1478 if (path)
1480 pathW = heap_strdupAtoW(path);
1481 if (!pathW)
1482 return E_OUTOFMEMORY;
1485 hr = IShellLinkW_SetIconLocation(&This->IShellLinkW_iface, path ? pathW : NULL, icon);
1486 free(pathW);
1488 return hr;
1491 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA *iface, LPCSTR pszPathRel,
1492 DWORD dwReserved)
1494 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1495 WCHAR *pathW;
1496 HRESULT hr;
1498 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1500 pathW = heap_strdupAtoW(pszPathRel);
1501 if (!pathW) return E_OUTOFMEMORY;
1503 hr = IShellLinkW_SetRelativePath(&This->IShellLinkW_iface, pathW, dwReserved);
1504 free(pathW);
1506 return hr;
1509 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA *iface, HWND hwnd, DWORD fFlags)
1511 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1513 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1515 return IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, fFlags);
1518 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA *iface, LPCSTR pszFile)
1520 IShellLinkImpl *This = impl_from_IShellLinkA(iface);
1521 HRESULT r;
1522 LPWSTR str;
1524 TRACE("(%p)->(path=%s)\n",This, debugstr_a(pszFile));
1526 if (!pszFile) return E_INVALIDARG;
1528 str = heap_strdupAtoW(pszFile);
1529 if( !str )
1530 return E_OUTOFMEMORY;
1532 r = IShellLinkW_SetPath(&This->IShellLinkW_iface, str);
1533 free( str );
1535 return r;
1538 /**************************************************************************
1539 * IShellLink Implementation
1542 static const IShellLinkAVtbl slvt =
1544 IShellLinkA_fnQueryInterface,
1545 IShellLinkA_fnAddRef,
1546 IShellLinkA_fnRelease,
1547 IShellLinkA_fnGetPath,
1548 IShellLinkA_fnGetIDList,
1549 IShellLinkA_fnSetIDList,
1550 IShellLinkA_fnGetDescription,
1551 IShellLinkA_fnSetDescription,
1552 IShellLinkA_fnGetWorkingDirectory,
1553 IShellLinkA_fnSetWorkingDirectory,
1554 IShellLinkA_fnGetArguments,
1555 IShellLinkA_fnSetArguments,
1556 IShellLinkA_fnGetHotkey,
1557 IShellLinkA_fnSetHotkey,
1558 IShellLinkA_fnGetShowCmd,
1559 IShellLinkA_fnSetShowCmd,
1560 IShellLinkA_fnGetIconLocation,
1561 IShellLinkA_fnSetIconLocation,
1562 IShellLinkA_fnSetRelativePath,
1563 IShellLinkA_fnResolve,
1564 IShellLinkA_fnSetPath
1568 /**************************************************************************
1569 * IShellLinkW_fnQueryInterface
1571 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1572 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1574 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1576 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
1578 *ppvObj = NULL;
1580 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
1582 *ppvObj = &This->IShellLinkA_iface;
1584 else if(IsEqualIID(riid, &IID_IShellLinkW))
1586 *ppvObj = &This->IShellLinkW_iface;
1588 else if(IsEqualIID(riid, &IID_IPersistFile))
1590 *ppvObj = &This->IPersistFile_iface;
1592 else if(IsEqualIID(riid, &IID_IPersistStream))
1594 *ppvObj = &This->IPersistStream_iface;
1596 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1598 *ppvObj = &This->IShellLinkDataList_iface;
1600 else if(IsEqualIID(riid, &IID_IShellExtInit))
1602 *ppvObj = &This->IShellExtInit_iface;
1604 else if(IsEqualIID(riid, &IID_IContextMenu))
1606 *ppvObj = &This->IContextMenu_iface;
1608 else if(IsEqualIID(riid, &IID_IObjectWithSite))
1610 *ppvObj = &This->IObjectWithSite_iface;
1612 else if(IsEqualIID(riid, &IID_IPropertyStore))
1614 *ppvObj = &This->IPropertyStore_iface;
1617 if(*ppvObj)
1619 IUnknown_AddRef((IUnknown*)*ppvObj);
1620 TRACE("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
1621 return S_OK;
1623 ERR("-- Interface: E_NOINTERFACE\n");
1624 return E_NOINTERFACE;
1627 /******************************************************************************
1628 * IShellLinkW_fnAddRef
1630 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1632 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1633 ULONG ref = InterlockedIncrement(&This->ref);
1635 TRACE("(%p)->(count=%lu)\n", This, ref - 1);
1637 return ref;
1640 /******************************************************************************
1641 * IShellLinkW_fnRelease
1643 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1645 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1646 ULONG refCount = InterlockedDecrement(&This->ref);
1648 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1650 if (refCount)
1651 return refCount;
1653 TRACE("-- destroying IShellLink(%p)\n",This);
1655 free(This->sIcoPath);
1656 free(This->sArgs);
1657 free(This->sWorkDir);
1658 free(This->sDescription);
1659 free(This->sPath);
1660 free(This->sPathRel);
1661 free(This->sProduct);
1662 free(This->sComponent);
1663 free(This->filepath);
1665 if (This->site)
1666 IUnknown_Release( This->site );
1668 if (This->pPidl)
1669 ILFree(This->pPidl);
1671 LocalFree(This);
1673 return 0;
1676 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1678 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1679 HRESULT res = S_OK;
1681 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1682 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1684 if (This->sComponent || This->sProduct)
1685 return S_FALSE;
1687 if (cchMaxPath)
1688 pszFile[0] = 0;
1689 if (This->sPath)
1690 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1691 else
1692 res = S_FALSE;
1694 if (pfd)
1696 memset(pfd, 0, sizeof(*pfd));
1698 if (res == S_OK)
1700 WCHAR path[MAX_PATH];
1701 WIN32_FILE_ATTRIBUTE_DATA fad;
1703 if (GetFileAttributesExW(This->sPath, GetFileExInfoStandard, &fad))
1705 pfd->dwFileAttributes = fad.dwFileAttributes;
1706 pfd->ftCreationTime = fad.ftCreationTime;
1707 pfd->ftLastAccessTime = fad.ftLastAccessTime;
1708 pfd->ftLastWriteTime = fad.ftLastWriteTime;
1709 pfd->nFileSizeHigh = fad.nFileSizeHigh;
1710 pfd->nFileSizeLow = fad.nFileSizeLow;
1713 lstrcpyW(pfd->cFileName, PathFindFileNameW(This->sPath));
1715 if (GetShortPathNameW(This->sPath, path, MAX_PATH))
1717 lstrcpyW(pfd->cAlternateFileName, PathFindFileNameW(path));
1721 TRACE("attr 0x%08lx size 0x%08lx%08lx name %s shortname %s\n", pfd->dwFileAttributes,
1722 pfd->nFileSizeHigh, pfd->nFileSizeLow, wine_dbgstr_w(pfd->cFileName),
1723 wine_dbgstr_w(pfd->cAlternateFileName));
1726 return res;
1729 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1731 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1733 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1735 if (!This->pPidl)
1737 *ppidl = NULL;
1738 return S_FALSE;
1740 *ppidl = ILClone(This->pPidl);
1741 return S_OK;
1744 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1746 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1747 WCHAR path[MAX_PATH];
1749 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1751 if( This->pPidl )
1752 ILFree( This->pPidl );
1753 This->pPidl = ILClone( pidl );
1754 if( !This->pPidl )
1755 return E_FAIL;
1757 free( This->sPath );
1758 This->sPath = NULL;
1760 if ( SHGetPathFromIDListW( pidl, path ) )
1762 This->sPath = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
1763 if (!This->sPath)
1764 return E_OUTOFMEMORY;
1766 lstrcpyW(This->sPath, path);
1769 This->bDirty = TRUE;
1771 return S_OK;
1774 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1776 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1778 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1780 pszName[0] = 0;
1781 if( This->sDescription )
1782 lstrcpynW( pszName, This->sDescription, cchMaxName );
1784 return S_OK;
1787 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1789 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1791 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1793 free(This->sDescription);
1794 if (pszName)
1796 This->sDescription = malloc( (lstrlenW( pszName ) + 1) * sizeof(WCHAR) );
1797 if ( !This->sDescription )
1798 return E_OUTOFMEMORY;
1800 lstrcpyW( This->sDescription, pszName );
1802 else
1803 This->sDescription = NULL;
1804 This->bDirty = TRUE;
1806 return S_OK;
1809 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1811 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1813 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1815 if( cchMaxPath )
1816 pszDir[0] = 0;
1817 if( This->sWorkDir )
1818 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1820 return S_OK;
1823 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1825 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1827 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1829 free(This->sWorkDir);
1830 This->sWorkDir = malloc( (lstrlenW( pszDir ) + 1) * sizeof(WCHAR) );
1831 if ( !This->sWorkDir )
1832 return E_OUTOFMEMORY;
1833 lstrcpyW( This->sWorkDir, pszDir );
1834 This->bDirty = TRUE;
1836 return S_OK;
1839 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1841 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1843 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1845 if( cchMaxPath )
1846 pszArgs[0] = 0;
1847 if( This->sArgs )
1848 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1850 return S_OK;
1853 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1855 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1857 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1859 free(This->sArgs);
1860 if (pszArgs)
1862 This->sArgs = malloc( (lstrlenW( pszArgs ) + 1) * sizeof(WCHAR) );
1863 if ( !This->sArgs )
1864 return E_OUTOFMEMORY;
1865 lstrcpyW( This->sArgs, pszArgs );
1867 else This->sArgs = NULL;
1869 This->bDirty = TRUE;
1871 return S_OK;
1874 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1876 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1878 TRACE("(%p)->(%p)\n",This, pwHotkey);
1880 *pwHotkey=This->wHotKey;
1882 return S_OK;
1885 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1887 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1889 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1891 This->wHotKey = wHotkey;
1892 This->bDirty = TRUE;
1894 return S_OK;
1897 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1899 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1901 TRACE("(%p)->(%p)\n",This, piShowCmd);
1903 *piShowCmd = This->iShowCmd;
1905 return S_OK;
1908 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1910 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1912 TRACE("(%p)->(%d)\n", This, iShowCmd);
1914 This->iShowCmd = iShowCmd;
1915 This->bDirty = TRUE;
1917 return S_OK;
1920 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1922 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1924 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1926 *piIcon = This->iIcoNdx;
1928 if (This->sIcoPath)
1929 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1930 else
1931 pszIconPath[0] = 0;
1933 return S_OK;
1936 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, const WCHAR *path, INT icon)
1938 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1940 TRACE("(%p)->(path=%s icon=%u)\n", This, debugstr_w(path), icon);
1942 free(This->sIcoPath);
1943 if (path)
1945 size_t len = (lstrlenW(path) + 1) * sizeof(WCHAR);
1946 This->sIcoPath = malloc(len);
1947 if (!This->sIcoPath)
1948 return E_OUTOFMEMORY;
1949 memcpy(This->sIcoPath, path, len);
1951 else
1952 This->sIcoPath = NULL;
1953 This->iIcoNdx = icon;
1954 This->bDirty = TRUE;
1956 return S_OK;
1959 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1961 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1963 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1965 free(This->sPathRel);
1966 This->sPathRel = malloc( (lstrlenW( pszPathRel ) + 1) * sizeof(WCHAR) );
1967 if ( !This->sPathRel )
1968 return E_OUTOFMEMORY;
1969 lstrcpyW( This->sPathRel, pszPathRel );
1970 This->bDirty = TRUE;
1972 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1975 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1977 HRESULT hr = S_OK;
1978 BOOL bSuccess;
1980 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1982 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1984 /*FIXME: use IResolveShellLink interface */
1986 if (!This->sPath && This->pPidl) {
1987 WCHAR buffer[MAX_PATH];
1989 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1991 if (bSuccess && *buffer) {
1992 This->sPath = malloc((lstrlenW(buffer) + 1) * sizeof(WCHAR));
1993 if (!This->sPath)
1994 return E_OUTOFMEMORY;
1996 lstrcpyW(This->sPath, buffer);
1998 This->bDirty = TRUE;
1999 } else
2000 hr = S_OK; /* don't report an error occurred while just caching information */
2003 if (!This->sIcoPath && This->sPath) {
2004 This->sIcoPath = malloc((lstrlenW(This->sPath) + 1) * sizeof(WCHAR));
2005 if (!This->sIcoPath)
2006 return E_OUTOFMEMORY;
2008 lstrcpyW(This->sIcoPath, This->sPath);
2009 This->iIcoNdx = 0;
2011 This->bDirty = TRUE;
2014 return hr;
2017 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2019 LPWSTR ret;
2020 LPCWSTR p;
2021 DWORD len;
2023 if( !str )
2024 return NULL;
2026 p = wcschr( str, ':' );
2027 if( !p )
2028 return NULL;
2029 len = p - str;
2030 ret = malloc(sizeof(WCHAR) * (len + 1));
2031 if( !ret )
2032 return ret;
2033 memcpy( ret, str, sizeof(WCHAR)*len );
2034 ret[len] = 0;
2035 return ret;
2038 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2040 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2041 WCHAR szGuid[39];
2042 HRESULT r;
2043 GUID guid;
2044 int len;
2046 while( str[0] )
2048 /* each segment must start with two colons */
2049 if( str[0] != ':' || str[1] != ':' )
2050 return E_FAIL;
2052 /* the last segment is just two colons */
2053 if( !str[2] )
2054 break;
2055 str += 2;
2057 /* there must be a colon straight after a guid */
2058 p = wcschr( str, ':' );
2059 if( !p )
2060 return E_FAIL;
2061 len = p - str;
2062 if( len != 38 )
2063 return E_FAIL;
2065 /* get the guid, and check it's validly formatted */
2066 memcpy( szGuid, str, sizeof(WCHAR)*len );
2067 szGuid[len] = 0;
2068 r = CLSIDFromString( szGuid, &guid );
2069 if( r != S_OK )
2070 return r;
2071 str = p + 1;
2073 /* match it up to a guid that we care about */
2074 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2075 szComponent = str;
2076 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2077 szProduct = str;
2078 else
2079 return E_FAIL;
2081 /* skip to the next field */
2082 str = wcschr( str, ':' );
2083 if( !str )
2084 return E_FAIL;
2087 /* we have to have a component for an advertised shortcut */
2088 if( !szComponent )
2089 return E_FAIL;
2091 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2092 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2094 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2095 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2097 return S_OK;
2100 static BOOL ShellLink_GetVolumeInfo(LPCWSTR path, volume_info *volume)
2102 const int label_sz = ARRAY_SIZE(volume->label);
2103 WCHAR drive[] = { path[0], ':', '\\', 0 };
2104 BOOL r;
2106 volume->type = GetDriveTypeW(drive);
2107 r = GetVolumeInformationW(drive, volume->label, label_sz,
2108 &volume->serial, NULL, NULL, NULL, 0);
2109 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2110 volume->type, volume->serial, debugstr_w(volume->label));
2111 return r;
2114 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2116 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2117 WCHAR buffer[MAX_PATH];
2118 LPWSTR fname, unquoted = NULL;
2119 HRESULT hr = S_OK;
2120 UINT len;
2122 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2124 if (!pszFile) return E_INVALIDARG;
2126 /* quotes at the ends of the string are stripped */
2127 len = lstrlenW(pszFile);
2128 if (pszFile[0] == '"' && pszFile[len-1] == '"')
2130 unquoted = wcsdup(pszFile);
2131 PathUnquoteSpacesW(unquoted);
2132 pszFile = unquoted;
2135 /* any other quote marks are invalid */
2136 if (wcschr(pszFile, '"'))
2138 free(unquoted);
2139 return S_FALSE;
2142 free(This->sPath);
2143 This->sPath = NULL;
2145 free(This->sComponent);
2146 This->sComponent = NULL;
2148 if (This->pPidl)
2149 ILFree(This->pPidl);
2150 This->pPidl = NULL;
2152 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2154 if (*pszFile == '\0')
2155 *buffer = '\0';
2156 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2158 free(unquoted);
2159 return E_FAIL;
2161 else if(!PathFileExistsW(buffer) &&
2162 !SearchPathW(NULL, pszFile, NULL, MAX_PATH, buffer, NULL))
2163 hr = S_FALSE;
2165 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2166 ShellLink_GetVolumeInfo(buffer, &This->volume);
2168 This->sPath = malloc( (lstrlenW( buffer ) + 1) * sizeof(WCHAR) );
2169 if (!This->sPath)
2171 free(unquoted);
2172 return E_OUTOFMEMORY;
2175 lstrcpyW(This->sPath, buffer);
2177 This->bDirty = TRUE;
2178 free(unquoted);
2180 return hr;
2183 /**************************************************************************
2184 * IShellLinkW Implementation
2187 static const IShellLinkWVtbl slvtw =
2189 IShellLinkW_fnQueryInterface,
2190 IShellLinkW_fnAddRef,
2191 IShellLinkW_fnRelease,
2192 IShellLinkW_fnGetPath,
2193 IShellLinkW_fnGetIDList,
2194 IShellLinkW_fnSetIDList,
2195 IShellLinkW_fnGetDescription,
2196 IShellLinkW_fnSetDescription,
2197 IShellLinkW_fnGetWorkingDirectory,
2198 IShellLinkW_fnSetWorkingDirectory,
2199 IShellLinkW_fnGetArguments,
2200 IShellLinkW_fnSetArguments,
2201 IShellLinkW_fnGetHotkey,
2202 IShellLinkW_fnSetHotkey,
2203 IShellLinkW_fnGetShowCmd,
2204 IShellLinkW_fnSetShowCmd,
2205 IShellLinkW_fnGetIconLocation,
2206 IShellLinkW_fnSetIconLocation,
2207 IShellLinkW_fnSetRelativePath,
2208 IShellLinkW_fnResolve,
2209 IShellLinkW_fnSetPath
2212 static HRESULT WINAPI
2213 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2215 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2216 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2219 static ULONG WINAPI
2220 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2222 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2223 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2226 static ULONG WINAPI
2227 ShellLink_DataList_Release( IShellLinkDataList* iface )
2229 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2230 return IShellLinkW_Release(&This->IShellLinkW_iface);
2233 static HRESULT WINAPI
2234 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2236 FIXME("(%p)->(%p): stub\n", iface, pDataBlock);
2237 return E_NOTIMPL;
2240 static HRESULT WINAPI
2241 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2243 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2244 LPVOID block = NULL;
2245 HRESULT r = E_FAIL;
2247 TRACE("%p %08lx %p\n", iface, dwSig, ppDataBlock );
2249 switch (dwSig)
2251 case EXP_DARWIN_ID_SIG:
2252 if (!This->sComponent)
2253 break;
2254 block = shelllink_build_darwinid( This->sComponent, dwSig );
2255 r = S_OK;
2256 break;
2257 case EXP_SZ_LINK_SIG:
2258 case NT_CONSOLE_PROPS_SIG:
2259 case NT_FE_CONSOLE_PROPS_SIG:
2260 case EXP_SPECIAL_FOLDER_SIG:
2261 case EXP_SZ_ICON_SIG:
2262 FIXME("valid but unhandled datablock %08lx\n", dwSig);
2263 break;
2264 default:
2265 ERR("unknown datablock %08lx\n", dwSig);
2267 *ppDataBlock = block;
2268 return r;
2271 static HRESULT WINAPI
2272 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2274 FIXME("(%p)->(%lu): stub\n", iface, dwSig);
2275 return E_NOTIMPL;
2278 static HRESULT WINAPI
2279 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2281 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2282 DWORD flags = 0;
2284 FIXME("(%p)->(%p): partially implemented\n", This, pdwFlags);
2286 /* FIXME: add more */
2287 if (This->sArgs)
2288 flags |= SLDF_HAS_ARGS;
2289 if (This->sComponent)
2290 flags |= SLDF_HAS_DARWINID;
2291 if (This->sIcoPath)
2292 flags |= SLDF_HAS_ICONLOCATION;
2293 if (This->sProduct)
2294 flags |= SLDF_HAS_LOGO3ID;
2295 if (This->pPidl)
2296 flags |= SLDF_HAS_ID_LIST;
2298 *pdwFlags = flags;
2300 return S_OK;
2303 static HRESULT WINAPI
2304 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2306 FIXME("(%p)->(%lu): stub\n", iface, dwFlags);
2307 return S_OK;
2310 static const IShellLinkDataListVtbl dlvt =
2312 ShellLink_DataList_QueryInterface,
2313 ShellLink_DataList_AddRef,
2314 ShellLink_DataList_Release,
2315 ShellLink_AddDataBlock,
2316 ShellLink_CopyDataBlock,
2317 ShellLink_RemoveDataBlock,
2318 ShellLink_GetFlags,
2319 ShellLink_SetFlags
2322 static HRESULT WINAPI
2323 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2325 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2326 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2329 static ULONG WINAPI
2330 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2332 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2333 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2336 static ULONG WINAPI
2337 ShellLink_ExtInit_Release( IShellExtInit* iface )
2339 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2340 return IShellLinkW_Release(&This->IShellLinkW_iface);
2343 /**************************************************************************
2344 * ShellLink implementation of IShellExtInit::Initialize()
2346 * Loads the shelllink from the dataobject the shell is pointing to.
2348 static HRESULT WINAPI
2349 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2350 IDataObject *pdtobj, HKEY hkeyProgID )
2352 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2353 FORMATETC format;
2354 STGMEDIUM stgm;
2355 UINT count;
2356 HRESULT r = E_FAIL;
2358 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2360 if( !pdtobj )
2361 return r;
2363 format.cfFormat = CF_HDROP;
2364 format.ptd = NULL;
2365 format.dwAspect = DVASPECT_CONTENT;
2366 format.lindex = -1;
2367 format.tymed = TYMED_HGLOBAL;
2369 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2370 return r;
2372 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2373 if( count == 1 )
2375 LPWSTR path;
2377 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2378 count++;
2379 path = malloc( count * sizeof(WCHAR) );
2380 if( path )
2382 IPersistFile *pf = &This->IPersistFile_iface;
2384 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2385 r = IPersistFile_Load( pf, path, 0 );
2386 free( path );
2389 ReleaseStgMedium( &stgm );
2391 return r;
2394 static const IShellExtInitVtbl eivt =
2396 ShellLink_ExtInit_QueryInterface,
2397 ShellLink_ExtInit_AddRef,
2398 ShellLink_ExtInit_Release,
2399 ShellLink_ExtInit_Initialize
2402 static HRESULT WINAPI
2403 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2405 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2406 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject);
2409 static ULONG WINAPI
2410 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2412 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2413 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2416 static ULONG WINAPI
2417 ShellLink_ContextMenu_Release( IContextMenu* iface )
2419 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2420 return IShellLinkW_Release(&This->IShellLinkW_iface);
2423 static HRESULT WINAPI
2424 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2425 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2427 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2428 MENUITEMINFOW mii;
2429 int id = 1;
2431 TRACE("%p %p %u %u %u %u\n", This,
2432 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2434 if ( !hmenu )
2435 return E_INVALIDARG;
2437 memset( &mii, 0, sizeof mii );
2438 mii.cbSize = sizeof mii;
2439 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_STATE;
2440 mii.dwTypeData = (LPWSTR)L"Open";
2441 mii.cch = lstrlenW( mii.dwTypeData );
2442 mii.wID = idCmdFirst + id++;
2443 mii.fState = MFS_DEFAULT | MFS_ENABLED;
2444 mii.fType = MFT_STRING;
2445 if (!InsertMenuItemW( hmenu, indexMenu, TRUE, &mii ))
2446 return E_FAIL;
2447 This->iIdOpen = 0;
2449 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, id );
2452 static LPWSTR
2453 shelllink_get_msi_component_path( LPWSTR component )
2455 LPWSTR path;
2456 DWORD r, sz = 0;
2458 r = CommandLineFromMsiDescriptor( component, NULL, &sz );
2459 if (r != ERROR_SUCCESS)
2460 return NULL;
2462 sz++;
2463 path = malloc( sz * sizeof(WCHAR) );
2464 r = CommandLineFromMsiDescriptor( component, path, &sz );
2465 if (r != ERROR_SUCCESS)
2467 free( path );
2468 path = NULL;
2471 TRACE("returning %s\n", debugstr_w( path ) );
2473 return path;
2476 static HRESULT WINAPI
2477 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2479 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2480 SHELLEXECUTEINFOW sei;
2481 HWND hwnd = NULL; /* FIXME: get using interface set from IObjectWithSite */
2482 LPWSTR args = NULL;
2483 LPWSTR path = NULL;
2484 HRESULT r;
2486 TRACE("%p %p\n", This, lpici );
2488 if ( lpici->cbSize < sizeof (CMINVOKECOMMANDINFO) )
2489 return E_INVALIDARG;
2491 if ( lpici->lpVerb != MAKEINTRESOURCEA(This->iIdOpen) )
2493 ERR("Unknown id %p != %d\n", lpici->lpVerb, This->iIdOpen );
2494 return E_INVALIDARG;
2497 r = IShellLinkW_Resolve(&This->IShellLinkW_iface, hwnd, 0);
2498 if ( FAILED( r ) )
2499 return r;
2501 if ( This->sComponent )
2503 path = shelllink_get_msi_component_path( This->sComponent );
2504 if (!path)
2505 return E_FAIL;
2507 else
2508 path = wcsdup( This->sPath );
2510 if ( lpici->cbSize == sizeof (CMINVOKECOMMANDINFOEX) &&
2511 ( lpici->fMask & CMIC_MASK_UNICODE ) )
2513 LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX) lpici;
2514 DWORD len = 2;
2516 if ( This->sArgs )
2517 len += lstrlenW( This->sArgs );
2518 if ( iciex->lpParametersW )
2519 len += lstrlenW( iciex->lpParametersW );
2521 args = malloc( len * sizeof(WCHAR) );
2522 args[0] = 0;
2523 if ( This->sArgs )
2524 lstrcatW( args, This->sArgs );
2525 if ( iciex->lpParametersW && iciex->lpParametersW[0] )
2527 lstrcatW( args, L" " );
2528 lstrcatW( args, iciex->lpParametersW );
2532 memset( &sei, 0, sizeof sei );
2533 sei.cbSize = sizeof sei;
2534 sei.fMask = SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC|SEE_MASK_NO_CONSOLE|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
2535 sei.lpFile = path;
2536 sei.nShow = This->iShowCmd;
2537 sei.lpIDList = This->pPidl;
2538 sei.lpDirectory = This->sWorkDir;
2539 sei.lpParameters = args;
2540 sei.lpVerb = L"Open";
2542 if( ShellExecuteExW( &sei ) )
2543 r = S_OK;
2544 else
2545 r = E_FAIL;
2547 free( args );
2548 free( path );
2550 return r;
2553 static HRESULT WINAPI
2554 ShellLink_GetCommandString( IContextMenu* iface, UINT_PTR idCmd, UINT uType,
2555 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2557 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2559 FIXME("(%p)->(%Iu %u %p %p %u): stub\n", This,
2560 idCmd, uType, pwReserved, pszName, cchMax );
2562 return E_NOTIMPL;
2565 static const IContextMenuVtbl cmvt =
2567 ShellLink_ContextMenu_QueryInterface,
2568 ShellLink_ContextMenu_AddRef,
2569 ShellLink_ContextMenu_Release,
2570 ShellLink_QueryContextMenu,
2571 ShellLink_InvokeCommand,
2572 ShellLink_GetCommandString
2575 static HRESULT WINAPI
2576 ShellLink_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
2578 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2579 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, ppvObject );
2582 static ULONG WINAPI
2583 ShellLink_ObjectWithSite_AddRef( IObjectWithSite* iface )
2585 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2586 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2589 static ULONG WINAPI
2590 ShellLink_ObjectWithSite_Release( IObjectWithSite* iface )
2592 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2593 return IShellLinkW_Release(&This->IShellLinkW_iface);
2596 static HRESULT WINAPI
2597 ShellLink_GetSite( IObjectWithSite *iface, REFIID iid, void ** ppvSite )
2599 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2601 TRACE("%p %s %p\n", This, debugstr_guid( iid ), ppvSite );
2603 if ( !This->site )
2604 return E_FAIL;
2605 return IUnknown_QueryInterface( This->site, iid, ppvSite );
2608 static HRESULT WINAPI
2609 ShellLink_SetSite( IObjectWithSite *iface, IUnknown *punk )
2611 IShellLinkImpl *This = impl_from_IObjectWithSite(iface);
2613 TRACE("%p %p\n", iface, punk);
2615 if ( punk )
2616 IUnknown_AddRef( punk );
2618 if( This->site )
2619 IUnknown_Release( This->site );
2621 This->site = punk;
2623 return S_OK;
2626 static const IObjectWithSiteVtbl owsvt =
2628 ShellLink_ObjectWithSite_QueryInterface,
2629 ShellLink_ObjectWithSite_AddRef,
2630 ShellLink_ObjectWithSite_Release,
2631 ShellLink_SetSite,
2632 ShellLink_GetSite,
2635 static HRESULT WINAPI propertystore_QueryInterface(IPropertyStore *iface, REFIID riid, void **obj)
2637 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2638 return IShellLinkW_QueryInterface(&This->IShellLinkW_iface, riid, obj);
2641 static ULONG WINAPI propertystore_AddRef(IPropertyStore *iface)
2643 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2644 return IShellLinkW_AddRef(&This->IShellLinkW_iface);
2647 static ULONG WINAPI propertystore_Release(IPropertyStore *iface)
2649 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2650 return IShellLinkW_Release(&This->IShellLinkW_iface);
2653 static HRESULT WINAPI propertystore_GetCount(IPropertyStore *iface, DWORD *props)
2655 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2656 FIXME("(%p)->(%p): stub\n", This, props);
2657 return E_NOTIMPL;
2660 static HRESULT WINAPI propertystore_GetAt(IPropertyStore *iface, DWORD propid, PROPERTYKEY *key)
2662 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2663 FIXME("(%p)->(%ld %p): stub\n", This, propid, key);
2664 return E_NOTIMPL;
2667 static HRESULT WINAPI propertystore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *value)
2669 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2670 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2671 return E_NOTIMPL;
2674 static HRESULT WINAPI propertystore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT value)
2676 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2677 FIXME("(%p)->(%p %p): stub\n", This, key, value);
2678 return S_OK;
2681 static HRESULT WINAPI propertystore_Commit(IPropertyStore *iface)
2683 IShellLinkImpl *This = impl_from_IPropertyStore(iface);
2684 FIXME("(%p): stub\n", This);
2685 return S_OK;
2688 static const IPropertyStoreVtbl propertystorevtbl = {
2689 propertystore_QueryInterface,
2690 propertystore_AddRef,
2691 propertystore_Release,
2692 propertystore_GetCount,
2693 propertystore_GetAt,
2694 propertystore_GetValue,
2695 propertystore_SetValue,
2696 propertystore_Commit
2699 HRESULT WINAPI IShellLink_Constructor(IUnknown *outer, REFIID riid, void **obj)
2701 IShellLinkImpl * sl;
2702 HRESULT r;
2704 TRACE("outer=%p riid=%s\n", outer, debugstr_guid(riid));
2706 *obj = NULL;
2708 if (outer)
2709 return CLASS_E_NOAGGREGATION;
2711 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
2712 if (!sl)
2713 return E_OUTOFMEMORY;
2715 sl->ref = 1;
2716 sl->IShellLinkA_iface.lpVtbl = &slvt;
2717 sl->IShellLinkW_iface.lpVtbl = &slvtw;
2718 sl->IPersistFile_iface.lpVtbl = &pfvt;
2719 sl->IPersistStream_iface.lpVtbl = &psvt;
2720 sl->IShellLinkDataList_iface.lpVtbl = &dlvt;
2721 sl->IShellExtInit_iface.lpVtbl = &eivt;
2722 sl->IContextMenu_iface.lpVtbl = &cmvt;
2723 sl->IObjectWithSite_iface.lpVtbl = &owsvt;
2724 sl->IPropertyStore_iface.lpVtbl = &propertystorevtbl;
2725 sl->iShowCmd = SW_SHOWNORMAL;
2726 sl->bDirty = FALSE;
2727 sl->iIdOpen = -1;
2728 sl->site = NULL;
2729 sl->filepath = NULL;
2731 TRACE("(%p)\n", sl);
2733 r = IShellLinkW_QueryInterface( &sl->IShellLinkW_iface, riid, obj );
2734 IShellLinkW_Release( &sl->IShellLinkW_iface );
2735 return r;