Get rid of the ICOM_MSVTABLE_COMPAT support, g++ no longer requires
[wine/multimedia.git] / dlls / shell32 / shelllink.c
blob862b1a2aa741cb12694868c3a31f5f4d852a0948
1 /*
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTES
21 * Nearly complete informations about the binary formats
22 * of .lnk files available at http://www.wotsit.org
26 #include "config.h"
27 #include "wine/port.h"
29 #include <ctype.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <errno.h>
37 #include <limits.h>
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
40 #endif
41 #include "wine/debug.h"
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "winreg.h"
48 #include "winuser.h"
49 #include "wingdi.h"
50 #include "shlobj.h"
51 #include "undocshell.h"
53 #include "pidl.h"
54 #include "shell32_main.h"
55 #include "shlguid.h"
56 #include "shlwapi.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(shell);
60 /* link file formats */
62 /* flag1: lnk elements: simple link has 0x0B */
63 #define SCF_PIDL 1
64 #define SCF_NORMAL 2
65 #define SCF_DESCRIPTION 4
66 #define SCF_RELATIVE 8
67 #define SCF_WORKDIR 0x10
68 #define SCF_ARGS 0x20
69 #define SCF_CUSTOMICON 0x40
70 #define SCF_UNICODE 0x80
72 #include "pshpack1.h"
74 typedef struct _LINK_HEADER
76 DWORD dwSize; /* 0x00 size of the header - 0x4c */
77 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
78 DWORD dwFlags; /* 0x14 describes elements following */
79 DWORD dwFileAttr; /* 0x18 attributes of the target file */
80 FILETIME Time1; /* 0x1c */
81 FILETIME Time2; /* 0x24 */
82 FILETIME Time3; /* 0x2c */
83 DWORD dwFileLength; /* 0x34 File length */
84 DWORD nIcon; /* 0x38 icon number */
85 DWORD fStartup; /* 0x3c startup type */
86 DWORD wHotKey; /* 0x40 hotkey */
87 DWORD Unknown5; /* 0x44 */
88 DWORD Unknown6; /* 0x48 */
89 } LINK_HEADER, * PLINK_HEADER;
91 #define SHLINK_LOCAL 0
92 #define SHLINK_REMOTE 1
94 typedef struct _LOCATION_INFO
96 DWORD dwTotalSize;
97 DWORD dwHeaderSize;
98 DWORD dwFlags;
99 DWORD dwVolTableOfs;
100 DWORD dwLocalPathOfs;
101 DWORD dwNetworkVolTableOfs;
102 DWORD dwFinalPathOfs;
103 } LOCATION_INFO;
105 typedef struct _LOCAL_VOLUME_INFO
107 DWORD dwSize;
108 DWORD dwType;
109 DWORD dwVolSerial;
110 DWORD dwVolLabelOfs;
111 } LOCAL_VOLUME_INFO;
113 #include "poppack.h"
115 static IShellLinkAVtbl slvt;
116 static IShellLinkWVtbl slvtw;
117 static IPersistFileVtbl pfvt;
118 static IPersistStreamVtbl psvt;
120 /* IShellLink Implementation */
122 typedef struct
124 IShellLinkAVtbl *lpVtbl;
125 DWORD ref;
127 IShellLinkWVtbl *lpvtblw;
128 IPersistFileVtbl *lpvtblPersistFile;
129 IPersistStreamVtbl *lpvtblPersistStream;
131 /* data structures according to the informations in the link */
132 LPITEMIDLIST pPidl;
133 WORD wHotKey;
134 SYSTEMTIME time1;
135 SYSTEMTIME time2;
136 SYSTEMTIME time3;
138 DWORD iShowCmd;
139 LPWSTR sIcoPath;
140 INT iIcoNdx;
141 LPWSTR sPath;
142 LPWSTR sArgs;
143 LPWSTR sWorkDir;
144 LPWSTR sDescription;
145 LPWSTR sPathRel;
147 BOOL bDirty;
148 } IShellLinkImpl;
150 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
151 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
153 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
154 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
156 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
157 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
158 #define _IPersistStream_From_ICOM_THIS(class, name) class* StreamThis = (class*)(((char*)name)+_IPersistStream_Offset)
160 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
162 /* strdup on the process heap */
163 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
165 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
166 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
167 if( !p )
168 return p;
169 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
170 return p;
174 /**************************************************************************
175 * IPersistFile_QueryInterface
177 static HRESULT WINAPI IPersistFile_fnQueryInterface(
178 IPersistFile* iface,
179 REFIID riid,
180 LPVOID *ppvObj)
182 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
184 TRACE("(%p)\n",This);
186 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
189 /******************************************************************************
190 * IPersistFile_AddRef
192 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
194 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
196 TRACE("(%p)->(count=%lu)\n",This,This->ref);
198 return IShellLinkA_AddRef((IShellLinkA*)This);
200 /******************************************************************************
201 * IPersistFile_Release
203 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
205 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
207 TRACE("(%p)->(count=%lu)\n",This,This->ref);
209 return IShellLinkA_Release((IShellLinkA*)This);
212 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
214 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
215 FIXME("(%p)\n",This);
216 return NOERROR;
218 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
220 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
222 TRACE("(%p)\n",This);
224 if (This->bDirty)
225 return S_OK;
227 return S_FALSE;
229 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
231 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
232 _IPersistStream_From_ICOM_THIS(IPersistStream, This);
233 HRESULT r;
234 IStream *stm;
236 TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
238 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
239 if( SUCCEEDED( r ) )
241 r = IPersistStream_Load(StreamThis, stm);
242 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
243 IStream_Release( stm );
244 This->bDirty = FALSE;
247 return r;
250 static BOOL StartLinkProcessor( LPCOLESTR szLink )
252 static const WCHAR szFormat[] = {'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
253 ' ','-','r',' ','"','%','s','"',0 };
254 LONG len;
255 LPWSTR buffer;
256 STARTUPINFOW si;
257 PROCESS_INFORMATION pi;
259 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
260 buffer = HeapAlloc( GetProcessHeap(), 0, len );
261 if( !buffer )
262 return FALSE;
264 wsprintfW( buffer, szFormat, szLink );
266 TRACE("starting %s\n",debugstr_w(buffer));
268 memset(&si, 0, sizeof(si));
269 si.cb = sizeof(si);
270 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
272 /* wait for a while to throttle the creation of linker processes */
273 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
274 WARN("Timed out waiting for shell linker\n");
276 CloseHandle( pi.hProcess );
277 CloseHandle( pi.hThread );
279 return TRUE;
282 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
284 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
285 _IPersistStream_From_ICOM_THIS(IPersistStream, This);
286 HRESULT r;
287 IStream *stm;
289 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
291 if (!pszFileName || !This->sPath)
292 return E_FAIL;
294 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
295 if( SUCCEEDED( r ) )
297 r = IPersistStream_Save(StreamThis, stm, FALSE);
298 IStream_Release( stm );
300 if( SUCCEEDED( r ) )
302 StartLinkProcessor( pszFileName );
304 This->bDirty = FALSE;
306 else
308 DeleteFileW( pszFileName );
309 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
313 return r;
316 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
318 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
319 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
320 return NOERROR;
322 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
324 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
325 FIXME("(%p)\n",This);
326 return NOERROR;
329 static IPersistFileVtbl pfvt =
331 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
332 IPersistFile_fnQueryInterface,
333 IPersistFile_fnAddRef,
334 IPersistFile_fnRelease,
335 IPersistFile_fnGetClassID,
336 IPersistFile_fnIsDirty,
337 IPersistFile_fnLoad,
338 IPersistFile_fnSave,
339 IPersistFile_fnSaveCompleted,
340 IPersistFile_fnGetCurFile
343 /************************************************************************
344 * IPersistStream_QueryInterface
346 static HRESULT WINAPI IPersistStream_fnQueryInterface(
347 IPersistStream* iface,
348 REFIID riid,
349 VOID** ppvoid)
351 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
353 TRACE("(%p)\n",This);
355 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
358 /************************************************************************
359 * IPersistStream_Release
361 static ULONG WINAPI IPersistStream_fnRelease(
362 IPersistStream* iface)
364 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
366 TRACE("(%p)\n",This);
368 return IShellLinkA_Release((IShellLinkA*)This);
371 /************************************************************************
372 * IPersistStream_AddRef
374 static ULONG WINAPI IPersistStream_fnAddRef(
375 IPersistStream* iface)
377 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
379 TRACE("(%p)\n",This);
381 return IShellLinkA_AddRef((IShellLinkA*)This);
384 /************************************************************************
385 * IPersistStream_GetClassID
388 static HRESULT WINAPI IPersistStream_fnGetClassID(
389 IPersistStream* iface,
390 CLSID* pClassID)
392 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
394 TRACE("(%p)\n", This);
396 if (pClassID==0)
397 return E_POINTER;
399 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
401 return S_OK;
404 /************************************************************************
405 * IPersistStream_IsDirty (IPersistStream)
407 static HRESULT WINAPI IPersistStream_fnIsDirty(
408 IPersistStream* iface)
410 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
412 TRACE("(%p)\n", This);
414 return S_OK;
418 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
420 DWORD count;
421 USHORT len;
422 LPVOID temp;
423 LPWSTR str;
424 HRESULT r;
426 TRACE("%p\n", stm);
428 count = 0;
429 r = IStream_Read(stm, &len, sizeof(len), &count);
430 if ( FAILED (r) || ( count != sizeof(len) ) )
431 return E_FAIL;
433 if( unicode )
434 len *= sizeof (WCHAR);
436 TRACE("reading %d\n", len);
437 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
438 if( !temp )
439 return E_OUTOFMEMORY;
440 count = 0;
441 r = IStream_Read(stm, temp, len, &count);
442 if( FAILED (r) || ( count != len ) )
444 HeapFree( GetProcessHeap(), 0, temp );
445 return E_FAIL;
448 TRACE("read %s\n", debugstr_an(temp,len));
450 /* convert to unicode if necessary */
451 if( !unicode )
453 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
454 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
455 if( str )
456 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
457 HeapFree( GetProcessHeap(), 0, temp );
459 else
461 count /= 2;
462 str = (LPWSTR) temp;
464 str[count] = 0;
466 *pstr = str;
468 return S_OK;
471 static HRESULT Stream_LoadLocation( IStream* stm )
473 DWORD size;
474 ULONG count;
475 HRESULT r;
476 LOCATION_INFO *loc;
478 TRACE("%p\n",stm);
480 r = IStream_Read( stm, &size, sizeof(size), &count );
481 if( FAILED( r ) )
482 return r;
483 if( count != sizeof(loc->dwTotalSize) )
484 return E_FAIL;
486 loc = HeapAlloc( GetProcessHeap(), 0, size );
487 if( ! loc )
488 return E_OUTOFMEMORY;
490 r = IStream_Read( stm, &loc->dwHeaderSize, size-sizeof(size), &count );
491 if( FAILED( r ) )
492 goto end;
493 if( count != (size - sizeof(size)) )
495 r = E_FAIL;
496 goto end;
498 loc->dwTotalSize = size;
500 TRACE("Read %ld bytes\n",count);
502 /* FIXME: do something useful with it */
503 HeapFree( GetProcessHeap(), 0, loc );
505 return S_OK;
506 end:
507 HeapFree( GetProcessHeap(), 0, loc );
508 return r;
511 /************************************************************************
512 * IPersistStream_Load (IPersistStream)
514 static HRESULT WINAPI IPersistStream_fnLoad(
515 IPersistStream* iface,
516 IStream* stm)
518 LINK_HEADER hdr;
519 ULONG dwBytesRead;
520 BOOL unicode;
521 WCHAR sTemp[MAX_PATH];
522 HRESULT r;
524 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
526 TRACE("(%p)(%p)\n", This, stm);
528 if( !stm )
529 return STG_E_INVALIDPOINTER;
531 dwBytesRead = 0;
532 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
533 if( FAILED( r ) )
534 return r;
536 if( dwBytesRead != sizeof(hdr))
537 return E_FAIL;
538 if( hdr.dwSize != sizeof(hdr))
539 return E_FAIL;
540 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
541 return E_FAIL;
543 /* if( hdr.dwFlags & SCF_PIDL ) */ /* FIXME: seems to always have a PIDL */
545 r = ILLoadFromStream( stm, &This->pPidl );
546 if( FAILED( r ) )
547 return r;
549 This->wHotKey = (WORD)hdr.wHotKey;
550 This->iIcoNdx = hdr.nIcon;
551 FileTimeToSystemTime (&hdr.Time1, &This->time1);
552 FileTimeToSystemTime (&hdr.Time2, &This->time2);
553 FileTimeToSystemTime (&hdr.Time3, &This->time3);
554 #if 1
555 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time1, NULL, sTemp, 256);
556 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
557 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time2, NULL, sTemp, 256);
558 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
559 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time3, NULL, sTemp, 256);
560 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
561 pdump (This->pPidl);
562 #endif
563 if( hdr.dwFlags & SCF_NORMAL )
564 r = Stream_LoadLocation( stm );
565 if( FAILED( r ) )
566 goto end;
567 unicode = hdr.dwFlags & SCF_UNICODE;
568 if( hdr.dwFlags & SCF_DESCRIPTION )
570 r = Stream_LoadString( stm, unicode, &This->sDescription );
571 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
573 if( FAILED( r ) )
574 goto end;
576 if( hdr.dwFlags & SCF_RELATIVE )
578 r = Stream_LoadString( stm, unicode, &This->sPathRel );
579 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
581 if( FAILED( r ) )
582 goto end;
584 if( hdr.dwFlags & SCF_WORKDIR )
586 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
587 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
589 if( FAILED( r ) )
590 goto end;
592 if( hdr.dwFlags & SCF_ARGS )
594 r = Stream_LoadString( stm, unicode, &This->sArgs );
595 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
597 if( FAILED( r ) )
598 goto end;
600 if( hdr.dwFlags & SCF_CUSTOMICON )
602 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
603 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
605 if( FAILED( r ) )
606 goto end;
608 TRACE("OK\n");
610 pdump (This->pPidl);
612 return S_OK;
613 end:
614 return r;
617 /************************************************************************
618 * Stream_WriteString
620 * Helper function for IPersistStream_Save. Writes a unicode string
621 * with terminating nul byte to a stream, preceded by the its length.
623 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
625 USHORT len = lstrlenW( str ) + 1;
626 DWORD count;
627 HRESULT r;
629 r = IStream_Write( stm, &len, sizeof(len), &count );
630 if( FAILED( r ) )
631 return r;
633 len *= sizeof(WCHAR);
635 r = IStream_Write( stm, str, len, &count );
636 if( FAILED( r ) )
637 return r;
639 return S_OK;
642 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR filename )
644 LOCATION_INFO loc;
645 ULONG count;
647 FIXME("writing empty location info\n");
649 memset( &loc, 0, sizeof(loc) );
650 loc.dwTotalSize = sizeof(loc) - sizeof(loc.dwTotalSize);
652 /* FIXME: fill this in */
654 return IStream_Write( stm, &loc, loc.dwTotalSize, &count );
657 /************************************************************************
658 * IPersistStream_Save (IPersistStream)
660 * FIXME: makes assumptions about byte order
662 static HRESULT WINAPI IPersistStream_fnSave(
663 IPersistStream* iface,
664 IStream* stm,
665 BOOL fClearDirty)
667 static const WCHAR wOpen[] = {'o','p','e','n',0};
669 LINK_HEADER header;
670 WCHAR exePath[MAX_PATH];
671 ULONG count;
672 HRESULT r;
674 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
676 TRACE("(%p) %p %x\n", This, stm, fClearDirty);
678 *exePath = '\0';
680 if (This->sPath)
682 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH, NULL, NULL, NULL, NULL);
684 * windows can create lnk files to executables that do not exist yet
685 * so if the executable does not exist the just trust the path they
686 * gave us
688 if( !*exePath ) strcpyW(exePath,This->sPath);
691 /* if there's no PIDL, generate one */
692 if( ! This->pPidl ) This->pPidl = ILCreateFromPathW(exePath);
694 memset(&header, 0, sizeof(header));
695 header.dwSize = sizeof(header);
696 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
698 header.wHotKey = This->wHotKey;
699 header.nIcon = This->iIcoNdx;
700 header.dwFlags = SCF_UNICODE; /* strings are in unicode */
701 header.dwFlags |= SCF_NORMAL; /* how do we determine this ? */
702 if( This->pPidl )
703 header.dwFlags |= SCF_PIDL;
704 if( This->sDescription )
705 header.dwFlags |= SCF_DESCRIPTION;
706 if( This->sWorkDir )
707 header.dwFlags |= SCF_WORKDIR;
708 if( This->sArgs )
709 header.dwFlags |= SCF_ARGS;
710 if( This->sIcoPath )
711 header.dwFlags |= SCF_CUSTOMICON;
713 SystemTimeToFileTime ( &This->time1, &header.Time1 );
714 SystemTimeToFileTime ( &This->time2, &header.Time2 );
715 SystemTimeToFileTime ( &This->time3, &header.Time3 );
717 /* write the Shortcut header */
718 r = IStream_Write( stm, &header, sizeof(header), &count );
719 if( FAILED( r ) )
721 ERR("Write failed at %d\n",__LINE__);
722 return r;
725 TRACE("Writing pidl \n");
727 /* write the PIDL to the shortcut */
728 if( This->pPidl )
730 r = ILSaveToStream( stm, This->pPidl );
731 if( FAILED( r ) )
733 ERR("Failed to write PIDL at %d\n",__LINE__);
734 return r;
738 Stream_WriteLocationInfo( stm, exePath );
740 TRACE("Description = %s\n", debugstr_w(This->sDescription));
741 if( This->sDescription )
742 r = Stream_WriteString( stm, This->sDescription );
744 if( This->sPathRel )
745 r = Stream_WriteString( stm, This->sPathRel );
747 if( This->sWorkDir )
748 r = Stream_WriteString( stm, This->sWorkDir );
750 if( This->sArgs )
751 r = Stream_WriteString( stm, This->sArgs );
753 if( This->sIcoPath )
754 r = Stream_WriteString( stm, This->sIcoPath );
756 return S_OK;
759 /************************************************************************
760 * IPersistStream_GetSizeMax (IPersistStream)
762 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
763 IPersistStream* iface,
764 ULARGE_INTEGER* pcbSize)
766 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
768 TRACE("(%p)\n", This);
770 return E_NOTIMPL;
773 static IPersistStreamVtbl psvt =
775 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
776 IPersistStream_fnQueryInterface,
777 IPersistStream_fnAddRef,
778 IPersistStream_fnRelease,
779 IPersistStream_fnGetClassID,
780 IPersistStream_fnIsDirty,
781 IPersistStream_fnLoad,
782 IPersistStream_fnSave,
783 IPersistStream_fnGetSizeMax
786 /**************************************************************************
787 * IShellLink_Constructor
789 HRESULT WINAPI IShellLink_Constructor (
790 IUnknown * pUnkOuter,
791 REFIID riid,
792 LPVOID * ppv)
794 IShellLinkImpl * sl;
796 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
798 *ppv = NULL;
800 if(pUnkOuter) return CLASS_E_NOAGGREGATION;
801 sl = (IShellLinkImpl *) LocalAlloc(GMEM_ZEROINIT,sizeof(IShellLinkImpl));
802 if (!sl) return E_OUTOFMEMORY;
804 sl->ref = 1;
805 sl->lpVtbl = &slvt;
806 sl->lpvtblw = &slvtw;
807 sl->lpvtblPersistFile = &pfvt;
808 sl->lpvtblPersistStream = &psvt;
809 sl->iShowCmd = SW_SHOWNORMAL;
810 sl->bDirty = FALSE;
812 TRACE("(%p)->()\n",sl);
814 if (IsEqualIID(riid, &IID_IUnknown) ||
815 IsEqualIID(riid, &IID_IShellLinkA))
816 *ppv = sl;
817 else if (IsEqualIID(riid, &IID_IShellLinkW))
818 *ppv = &(sl->lpvtblw);
819 else {
820 LocalFree((HLOCAL)sl);
821 ERR("E_NOINTERFACE\n");
822 return E_NOINTERFACE;
825 return S_OK;
829 static BOOL SHELL_ExistsFileW(LPCWSTR path)
831 HANDLE hfile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
833 if (hfile != INVALID_HANDLE_VALUE) {
834 CloseHandle(hfile);
835 return TRUE;
836 } else
837 return FALSE;
840 /**************************************************************************
841 * ShellLink_UpdatePath
842 * update absolute path in sPath using relative path in sPathRel
844 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
846 if (!path || !psPath)
847 return E_INVALIDARG;
849 if (!*psPath && sPathRel) {
850 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
851 LPWSTR final = NULL;
853 /* first try if [directory of link file] + [relative path] finds an existing file */
855 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
856 if( !final )
857 final = buffer;
858 lstrcpyW(final, sPathRel);
860 *abs_path = '\0';
862 if (SHELL_ExistsFileW(buffer)) {
863 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
864 lstrcpyW(abs_path, buffer);
865 } else {
866 /* try if [working directory] + [relative path] finds an existing file */
867 if (sWorkDir) {
868 lstrcpyW(buffer, sWorkDir);
869 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
871 if (SHELL_ExistsFileW(buffer))
872 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
873 lstrcpyW(abs_path, buffer);
877 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
878 if (!*abs_path)
879 lstrcpyW(abs_path, sPathRel);
881 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
882 if (!*psPath)
883 return E_OUTOFMEMORY;
885 lstrcpyW(*psPath, abs_path);
888 return S_OK;
891 /**************************************************************************
892 * IShellLink_ConstructFromFile
894 HRESULT WINAPI IShellLink_ConstructFromFile (
895 IUnknown* pUnkOuter,
896 REFIID riid,
897 LPCITEMIDLIST pidl,
898 LPVOID* ppv
901 IShellLinkW* psl;
903 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
905 if (SUCCEEDED(hr)) {
906 IPersistFile* ppf;
908 *ppv = NULL;
910 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
912 if (SUCCEEDED(hr)) {
913 WCHAR path[MAX_PATH];
915 if (SHGetPathFromIDListW(pidl, path))
916 hr = IPersistFile_Load(ppf, path, 0);
917 else
918 hr = E_FAIL;
920 if (SUCCEEDED(hr))
921 *ppv = (IUnknown*) psl;
923 IPersistFile_Release(ppf);
926 if (!*ppv)
927 IShellLinkW_Release(psl);
930 return hr;
933 /**************************************************************************
934 * IShellLinkA_QueryInterface
936 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
938 ICOM_THIS(IShellLinkImpl, iface);
940 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
942 *ppvObj = NULL;
944 if(IsEqualIID(riid, &IID_IUnknown) ||
945 IsEqualIID(riid, &IID_IShellLinkA))
947 *ppvObj = This;
949 else if(IsEqualIID(riid, &IID_IShellLinkW))
951 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
953 else if(IsEqualIID(riid, &IID_IPersistFile))
955 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
957 else if(IsEqualIID(riid, &IID_IPersistStream))
959 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
962 if(*ppvObj)
964 IUnknown_AddRef((IUnknown*)(*ppvObj));
965 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
966 return S_OK;
968 TRACE("-- Interface: E_NOINTERFACE\n");
969 return E_NOINTERFACE;
971 /******************************************************************************
972 * IShellLinkA_AddRef
974 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
976 ICOM_THIS(IShellLinkImpl, iface);
978 TRACE("(%p)->(count=%lu)\n",This,This->ref);
980 return ++(This->ref);
982 /******************************************************************************
983 * IShellLinkA_Release
985 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
987 ICOM_THIS(IShellLinkImpl, iface);
989 TRACE("(%p)->(count=%lu)\n",This,This->ref);
991 if (--(This->ref))
992 return This->ref;
994 TRACE("-- destroying IShellLink(%p)\n",This);
996 if (This->sIcoPath)
997 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
999 if (This->sArgs)
1000 HeapFree(GetProcessHeap(), 0, This->sArgs);
1002 if (This->sWorkDir)
1003 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1005 if (This->sDescription)
1006 HeapFree(GetProcessHeap(), 0, This->sDescription);
1008 if (This->sPath)
1009 HeapFree(GetProcessHeap(),0,This->sPath);
1011 if (This->pPidl)
1012 ILFree(This->pPidl);
1014 LocalFree((HANDLE)This);
1016 return 0;
1019 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1020 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1022 ICOM_THIS(IShellLinkImpl, iface);
1024 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1025 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1027 if( cchMaxPath )
1028 pszFile[0] = 0;
1029 if (This->sPath)
1030 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1031 pszFile, cchMaxPath, NULL, NULL);
1033 return NOERROR;
1036 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1038 ICOM_THIS(IShellLinkImpl, iface);
1040 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1042 *ppidl = ILClone(This->pPidl);
1044 return NOERROR;
1047 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1049 ICOM_THIS(IShellLinkImpl, iface);
1051 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1053 if (This->pPidl)
1054 ILFree(This->pPidl);
1055 This->pPidl = ILClone (pidl);
1056 This->bDirty = TRUE;
1058 return S_OK;
1061 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1063 ICOM_THIS(IShellLinkImpl, iface);
1065 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1067 if( cchMaxName )
1068 pszName[0] = 0;
1069 if( This->sDescription )
1070 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1071 pszName, cchMaxName, NULL, NULL);
1073 return S_OK;
1075 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1077 ICOM_THIS(IShellLinkImpl, iface);
1079 TRACE("(%p)->(pName=%s)\n", This, pszName);
1081 if (This->sDescription)
1082 HeapFree(GetProcessHeap(), 0, This->sDescription);
1083 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1084 if ( !This->sDescription )
1085 return E_OUTOFMEMORY;
1087 This->bDirty = TRUE;
1089 return S_OK;
1092 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1094 ICOM_THIS(IShellLinkImpl, iface);
1096 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1098 if( cchMaxPath )
1099 pszDir[0] = 0;
1100 if( This->sWorkDir )
1101 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1102 pszDir, cchMaxPath, NULL, NULL);
1104 return S_OK;
1107 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1109 ICOM_THIS(IShellLinkImpl, iface);
1111 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1113 if (This->sWorkDir)
1114 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1115 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1116 if ( !This->sWorkDir )
1117 return E_OUTOFMEMORY;
1119 This->bDirty = TRUE;
1121 return S_OK;
1124 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1126 ICOM_THIS(IShellLinkImpl, iface);
1128 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1130 if( cchMaxPath )
1131 pszArgs[0] = 0;
1132 if( This->sArgs )
1133 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1134 pszArgs, cchMaxPath, NULL, NULL);
1136 return S_OK;
1139 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1141 ICOM_THIS(IShellLinkImpl, iface);
1143 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1145 if (This->sArgs)
1146 HeapFree(GetProcessHeap(), 0, This->sArgs);
1147 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1148 if( !This->sArgs )
1149 return E_OUTOFMEMORY;
1151 This->bDirty = TRUE;
1153 return S_OK;
1156 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1158 ICOM_THIS(IShellLinkImpl, iface);
1160 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1162 *pwHotkey = This->wHotKey;
1164 return S_OK;
1167 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1169 ICOM_THIS(IShellLinkImpl, iface);
1171 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1173 This->wHotKey = wHotkey;
1174 This->bDirty = TRUE;
1176 return S_OK;
1179 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1181 ICOM_THIS(IShellLinkImpl, iface);
1183 TRACE("(%p)->(%p)\n",This, piShowCmd);
1184 *piShowCmd = This->iShowCmd;
1185 return S_OK;
1188 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1190 ICOM_THIS(IShellLinkImpl, iface);
1192 TRACE("(%p) %d\n",This, iShowCmd);
1194 This->iShowCmd = iShowCmd;
1195 This->bDirty = TRUE;
1197 return NOERROR;
1200 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1202 LPCITEMIDLIST pidlLast;
1204 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1206 if (SUCCEEDED(hr)) {
1207 IExtractIconA* pei;
1209 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1211 if (SUCCEEDED(hr)) {
1212 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1214 IExtractIconA_Release(pei);
1217 IShellFolder_Release(psf);
1220 return hr;
1223 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1225 ICOM_THIS(IShellLinkImpl, iface);
1227 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1229 if (cchIconPath)
1230 pszIconPath[0] = 0;
1232 if (This->sIcoPath) {
1233 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1234 *piIcon = This->iIcoNdx;
1235 return S_OK;
1238 if (This->pPidl || This->sPath) {
1239 IShellFolder* pdsk;
1241 HRESULT hr = SHGetDesktopFolder(&pdsk);
1243 if (SUCCEEDED(hr)) {
1244 /* first look for an icon using the PIDL (if present) */
1245 if (This->pPidl)
1246 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1247 else
1248 hr = E_FAIL;
1250 /* if we couldn't find an icon yet, look for it using the file system path */
1251 if (FAILED(hr) && This->sPath) {
1252 LPITEMIDLIST pidl;
1254 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1256 if (SUCCEEDED(hr)) {
1257 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1259 SHFree(pidl);
1263 IShellFolder_Release(pdsk);
1266 return hr;
1267 } else
1268 return E_FAIL;
1271 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1273 ICOM_THIS(IShellLinkImpl, iface);
1275 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1277 if (This->sIcoPath)
1278 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1279 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1280 if ( !This->sIcoPath )
1281 return E_OUTOFMEMORY;
1283 This->iIcoNdx = iIcon;
1284 This->bDirty = TRUE;
1286 return S_OK;
1289 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1291 ICOM_THIS(IShellLinkImpl, iface);
1293 FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1295 if (This->sPathRel)
1296 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1297 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1298 This->bDirty = TRUE;
1300 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1303 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1305 ICOM_THIS(IShellLinkImpl, iface);
1307 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1308 return S_OK;
1311 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1313 ICOM_THIS(IShellLinkImpl, iface);
1315 TRACE("(%p)->(path=%s)\n",This, pszFile);
1317 if (This->sPath)
1318 HeapFree(GetProcessHeap(), 0, This->sPath);
1319 This->sPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1320 if( !This->sPath )
1321 return E_OUTOFMEMORY;
1323 This->bDirty = TRUE;
1325 return S_OK;
1328 /**************************************************************************
1329 * IShellLink Implementation
1332 static IShellLinkAVtbl slvt =
1334 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1335 IShellLinkA_fnQueryInterface,
1336 IShellLinkA_fnAddRef,
1337 IShellLinkA_fnRelease,
1338 IShellLinkA_fnGetPath,
1339 IShellLinkA_fnGetIDList,
1340 IShellLinkA_fnSetIDList,
1341 IShellLinkA_fnGetDescription,
1342 IShellLinkA_fnSetDescription,
1343 IShellLinkA_fnGetWorkingDirectory,
1344 IShellLinkA_fnSetWorkingDirectory,
1345 IShellLinkA_fnGetArguments,
1346 IShellLinkA_fnSetArguments,
1347 IShellLinkA_fnGetHotkey,
1348 IShellLinkA_fnSetHotkey,
1349 IShellLinkA_fnGetShowCmd,
1350 IShellLinkA_fnSetShowCmd,
1351 IShellLinkA_fnGetIconLocation,
1352 IShellLinkA_fnSetIconLocation,
1353 IShellLinkA_fnSetRelativePath,
1354 IShellLinkA_fnResolve,
1355 IShellLinkA_fnSetPath
1359 /**************************************************************************
1360 * IShellLinkW_fnQueryInterface
1362 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1363 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1365 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1367 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1370 /******************************************************************************
1371 * IShellLinkW_fnAddRef
1373 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1375 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1377 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1379 return IShellLinkA_AddRef((IShellLinkA*)This);
1381 /******************************************************************************
1382 * IShellLinkW_fnRelease
1385 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1387 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1389 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1391 return IShellLinkA_Release((IShellLinkA*)This);
1394 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1396 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1398 FIXME("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",This, pszFile, cchMaxPath, pfd, fFlags);
1399 if( cchMaxPath )
1400 pszFile[0] = 0;
1401 if( This->sPath )
1402 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1404 return NOERROR;
1407 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1409 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1411 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1413 if( This->pPidl)
1414 *ppidl = ILClone( This->pPidl );
1415 else
1416 *ppidl = NULL;
1418 return S_OK;
1421 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1423 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1425 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1427 if( This->pPidl )
1428 ILFree( This->pPidl );
1429 This->pPidl = ILClone( pidl );
1430 if( !This->pPidl )
1431 return E_FAIL;
1433 This->bDirty = TRUE;
1435 return S_OK;
1438 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1440 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1442 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1444 if( cchMaxName )
1445 pszName[0] = 0;
1446 if( This->sDescription )
1447 lstrcpynW( pszName, This->sDescription, cchMaxName );
1449 return S_OK;
1452 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1454 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1456 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1458 if (This->sDescription)
1459 HeapFree(GetProcessHeap(), 0, This->sDescription);
1460 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1461 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1462 if ( !This->sDescription )
1463 return E_OUTOFMEMORY;
1465 lstrcpyW( This->sDescription, pszName );
1466 This->bDirty = TRUE;
1468 return S_OK;
1471 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1473 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1475 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1477 if( cchMaxPath )
1478 pszDir[0] = 0;
1479 if( This->sWorkDir )
1480 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1482 return S_OK;
1485 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1487 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1489 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1491 if (This->sWorkDir)
1492 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1493 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1494 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1495 if ( !This->sWorkDir )
1496 return E_OUTOFMEMORY;
1497 lstrcpyW( This->sWorkDir, pszDir );
1498 This->bDirty = TRUE;
1500 return S_OK;
1503 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1505 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1507 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1509 if( cchMaxPath )
1510 pszArgs[0] = 0;
1511 if( This->sArgs )
1512 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1514 return NOERROR;
1517 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1519 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1521 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1523 if (This->sArgs)
1524 HeapFree(GetProcessHeap(), 0, This->sArgs);
1525 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1526 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1527 if ( !This->sArgs )
1528 return E_OUTOFMEMORY;
1529 lstrcpyW( This->sArgs, pszArgs );
1530 This->bDirty = TRUE;
1532 return S_OK;
1535 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1537 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1539 TRACE("(%p)->(%p)\n",This, pwHotkey);
1541 *pwHotkey=This->wHotKey;
1543 return S_OK;
1546 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1548 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1550 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1552 This->wHotKey = wHotkey;
1553 This->bDirty = TRUE;
1555 return S_OK;
1558 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1560 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1562 TRACE("(%p)->(%p)\n",This, piShowCmd);
1564 *piShowCmd = This->iShowCmd;
1566 return S_OK;
1569 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1571 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1573 This->iShowCmd = iShowCmd;
1574 This->bDirty = TRUE;
1576 return S_OK;
1579 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1581 LPCITEMIDLIST pidlLast;
1583 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1585 if (SUCCEEDED(hr)) {
1586 IExtractIconW* pei;
1588 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1590 if (SUCCEEDED(hr)) {
1591 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1593 IExtractIconW_Release(pei);
1596 IShellFolder_Release(psf);
1599 return hr;
1602 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1604 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1606 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1608 if (cchIconPath)
1609 pszIconPath[0] = 0;
1611 if (This->sIcoPath) {
1612 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1613 *piIcon = This->iIcoNdx;
1614 return S_OK;
1617 if (This->pPidl || This->sPath) {
1618 IShellFolder* pdsk;
1620 HRESULT hr = SHGetDesktopFolder(&pdsk);
1622 if (SUCCEEDED(hr)) {
1623 /* first look for an icon using the PIDL (if present) */
1624 if (This->pPidl)
1625 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1626 else
1627 hr = E_FAIL;
1629 /* if we couldn't find an icon yet, look for it using the file system path */
1630 if (FAILED(hr) && This->sPath) {
1631 LPITEMIDLIST pidl;
1633 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1635 if (SUCCEEDED(hr)) {
1636 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1638 SHFree(pidl);
1642 IShellFolder_Release(pdsk);
1645 return hr;
1646 } else
1647 return E_FAIL;
1650 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1652 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1654 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1656 if (This->sIcoPath)
1657 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1658 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1659 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1660 if ( !This->sIcoPath )
1661 return E_OUTOFMEMORY;
1662 lstrcpyW( This->sIcoPath, pszIconPath );
1664 This->iIcoNdx = iIcon;
1665 This->bDirty = TRUE;
1667 return S_OK;
1670 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1672 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1674 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1676 if (This->sPathRel)
1677 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1678 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1679 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1680 if ( !This->sPathRel )
1681 return E_OUTOFMEMORY;
1682 lstrcpyW( This->sPathRel, pszPathRel );
1683 This->bDirty = TRUE;
1685 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1688 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1690 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1692 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1694 return S_OK;
1697 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1699 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1701 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1703 if (This->sPath)
1704 HeapFree(GetProcessHeap(), 0, This->sPath);
1705 This->sPath = HeapAlloc( GetProcessHeap(), 0,
1706 (lstrlenW( pszFile )+1) * sizeof (WCHAR) );
1707 if ( !This->sPath )
1708 return E_OUTOFMEMORY;
1710 lstrcpyW( This->sPath, pszFile );
1711 This->bDirty = TRUE;
1713 return S_OK;
1716 /**************************************************************************
1717 * IShellLinkW Implementation
1720 static IShellLinkWVtbl slvtw =
1722 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1723 IShellLinkW_fnQueryInterface,
1724 IShellLinkW_fnAddRef,
1725 IShellLinkW_fnRelease,
1726 IShellLinkW_fnGetPath,
1727 IShellLinkW_fnGetIDList,
1728 IShellLinkW_fnSetIDList,
1729 IShellLinkW_fnGetDescription,
1730 IShellLinkW_fnSetDescription,
1731 IShellLinkW_fnGetWorkingDirectory,
1732 IShellLinkW_fnSetWorkingDirectory,
1733 IShellLinkW_fnGetArguments,
1734 IShellLinkW_fnSetArguments,
1735 IShellLinkW_fnGetHotkey,
1736 IShellLinkW_fnSetHotkey,
1737 IShellLinkW_fnGetShowCmd,
1738 IShellLinkW_fnSetShowCmd,
1739 IShellLinkW_fnGetIconLocation,
1740 IShellLinkW_fnSetIconLocation,
1741 IShellLinkW_fnSetRelativePath,
1742 IShellLinkW_fnResolve,
1743 IShellLinkW_fnSetPath