Use the LVCFMT_{LEFT,RIGHT,CENTER} enumeration flags properly.
[wine/dcerpc.git] / dlls / shell32 / shelllink.c
blobb7b379f81c5905b4f818ef5004409df36f2e90c5
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
42 #define COBJMACROS
44 #include "wine/debug.h"
45 #include "winerror.h"
46 #include "windef.h"
47 #include "winbase.h"
48 #include "winnls.h"
49 #include "winreg.h"
51 #include "winuser.h"
52 #include "wingdi.h"
53 #include "shlobj.h"
54 #include "undocshell.h"
56 #include "pidl.h"
57 #include "shell32_main.h"
58 #include "shlguid.h"
59 #include "shlwapi.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(shell);
63 /* link file formats */
65 /* flag1: lnk elements: simple link has 0x0B */
66 #define SCF_PIDL 1
67 #define SCF_NORMAL 2
68 #define SCF_DESCRIPTION 4
69 #define SCF_RELATIVE 8
70 #define SCF_WORKDIR 0x10
71 #define SCF_ARGS 0x20
72 #define SCF_CUSTOMICON 0x40
73 #define SCF_UNICODE 0x80
75 #include "pshpack1.h"
77 typedef struct _LINK_HEADER
79 DWORD dwSize; /* 0x00 size of the header - 0x4c */
80 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
81 DWORD dwFlags; /* 0x14 describes elements following */
82 DWORD dwFileAttr; /* 0x18 attributes of the target file */
83 FILETIME Time1; /* 0x1c */
84 FILETIME Time2; /* 0x24 */
85 FILETIME Time3; /* 0x2c */
86 DWORD dwFileLength; /* 0x34 File length */
87 DWORD nIcon; /* 0x38 icon number */
88 DWORD fStartup; /* 0x3c startup type */
89 DWORD wHotKey; /* 0x40 hotkey */
90 DWORD Unknown5; /* 0x44 */
91 DWORD Unknown6; /* 0x48 */
92 } LINK_HEADER, * PLINK_HEADER;
94 #define SHLINK_LOCAL 0
95 #define SHLINK_REMOTE 1
97 typedef struct _LOCATION_INFO
99 DWORD dwTotalSize;
100 DWORD dwHeaderSize;
101 DWORD dwFlags;
102 DWORD dwVolTableOfs;
103 DWORD dwLocalPathOfs;
104 DWORD dwNetworkVolTableOfs;
105 DWORD dwFinalPathOfs;
106 } LOCATION_INFO;
108 typedef struct _LOCAL_VOLUME_INFO
110 DWORD dwSize;
111 DWORD dwType;
112 DWORD dwVolSerial;
113 DWORD dwVolLabelOfs;
114 } LOCAL_VOLUME_INFO;
116 #include "poppack.h"
118 static IShellLinkAVtbl slvt;
119 static IShellLinkWVtbl slvtw;
120 static IPersistFileVtbl pfvt;
121 static IPersistStreamVtbl psvt;
123 /* IShellLink Implementation */
125 typedef struct
127 IShellLinkAVtbl *lpVtbl;
128 DWORD ref;
130 IShellLinkWVtbl *lpvtblw;
131 IPersistFileVtbl *lpvtblPersistFile;
132 IPersistStreamVtbl *lpvtblPersistStream;
134 /* data structures according to the informations in the link */
135 LPITEMIDLIST pPidl;
136 WORD wHotKey;
137 SYSTEMTIME time1;
138 SYSTEMTIME time2;
139 SYSTEMTIME time3;
141 DWORD iShowCmd;
142 LPWSTR sIcoPath;
143 INT iIcoNdx;
144 LPWSTR sPath;
145 LPWSTR sArgs;
146 LPWSTR sWorkDir;
147 LPWSTR sDescription;
148 LPWSTR sPathRel;
150 BOOL bDirty;
151 } IShellLinkImpl;
153 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
154 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
156 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
157 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
159 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
160 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
162 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
164 /* strdup on the process heap */
165 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
167 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
168 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
169 if( !p )
170 return p;
171 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
172 return p;
176 /**************************************************************************
177 * IPersistFile_QueryInterface
179 static HRESULT WINAPI IPersistFile_fnQueryInterface(
180 IPersistFile* iface,
181 REFIID riid,
182 LPVOID *ppvObj)
184 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
186 TRACE("(%p)\n",This);
188 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
191 /******************************************************************************
192 * IPersistFile_AddRef
194 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
196 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
198 TRACE("(%p)->(count=%lu)\n",This,This->ref);
200 return IShellLinkA_AddRef((IShellLinkA*)This);
202 /******************************************************************************
203 * IPersistFile_Release
205 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
207 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
209 TRACE("(%p)->(count=%lu)\n",This,This->ref);
211 return IShellLinkA_Release((IShellLinkA*)This);
214 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
216 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
217 FIXME("(%p)\n",This);
218 return NOERROR;
220 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
222 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
224 TRACE("(%p)\n",This);
226 if (This->bDirty)
227 return S_OK;
229 return S_FALSE;
231 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
233 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
234 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
235 HRESULT r;
236 IStream *stm;
238 TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
240 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
241 if( SUCCEEDED( r ) )
243 r = IPersistStream_Load(StreamThis, stm);
244 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
245 IStream_Release( stm );
246 This->bDirty = FALSE;
249 return r;
252 static BOOL StartLinkProcessor( LPCOLESTR szLink )
254 static const WCHAR szFormat[] = {'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
255 ' ','-','r',' ','"','%','s','"',0 };
256 LONG len;
257 LPWSTR buffer;
258 STARTUPINFOW si;
259 PROCESS_INFORMATION pi;
261 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
262 buffer = HeapAlloc( GetProcessHeap(), 0, len );
263 if( !buffer )
264 return FALSE;
266 wsprintfW( buffer, szFormat, szLink );
268 TRACE("starting %s\n",debugstr_w(buffer));
270 memset(&si, 0, sizeof(si));
271 si.cb = sizeof(si);
272 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
274 /* wait for a while to throttle the creation of linker processes */
275 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
276 WARN("Timed out waiting for shell linker\n");
278 CloseHandle( pi.hProcess );
279 CloseHandle( pi.hThread );
281 return TRUE;
284 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
286 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
287 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
288 HRESULT r;
289 IStream *stm;
291 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
293 if (!pszFileName || !This->sPath)
294 return E_FAIL;
296 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
297 if( SUCCEEDED( r ) )
299 r = IPersistStream_Save(StreamThis, stm, FALSE);
300 IStream_Release( stm );
302 if( SUCCEEDED( r ) )
304 StartLinkProcessor( pszFileName );
306 This->bDirty = FALSE;
308 else
310 DeleteFileW( pszFileName );
311 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
315 return r;
318 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
320 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
321 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
322 return NOERROR;
324 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
326 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
327 FIXME("(%p)\n",This);
328 return NOERROR;
331 static IPersistFileVtbl pfvt =
333 IPersistFile_fnQueryInterface,
334 IPersistFile_fnAddRef,
335 IPersistFile_fnRelease,
336 IPersistFile_fnGetClassID,
337 IPersistFile_fnIsDirty,
338 IPersistFile_fnLoad,
339 IPersistFile_fnSave,
340 IPersistFile_fnSaveCompleted,
341 IPersistFile_fnGetCurFile
344 /************************************************************************
345 * IPersistStream_QueryInterface
347 static HRESULT WINAPI IPersistStream_fnQueryInterface(
348 IPersistStream* iface,
349 REFIID riid,
350 VOID** ppvoid)
352 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
354 TRACE("(%p)\n",This);
356 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
359 /************************************************************************
360 * IPersistStream_Release
362 static ULONG WINAPI IPersistStream_fnRelease(
363 IPersistStream* iface)
365 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
367 TRACE("(%p)\n",This);
369 return IShellLinkA_Release((IShellLinkA*)This);
372 /************************************************************************
373 * IPersistStream_AddRef
375 static ULONG WINAPI IPersistStream_fnAddRef(
376 IPersistStream* iface)
378 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
380 TRACE("(%p)\n",This);
382 return IShellLinkA_AddRef((IShellLinkA*)This);
385 /************************************************************************
386 * IPersistStream_GetClassID
389 static HRESULT WINAPI IPersistStream_fnGetClassID(
390 IPersistStream* iface,
391 CLSID* pClassID)
393 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
395 TRACE("(%p)\n", This);
397 if (pClassID==0)
398 return E_POINTER;
400 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
402 return S_OK;
405 /************************************************************************
406 * IPersistStream_IsDirty (IPersistStream)
408 static HRESULT WINAPI IPersistStream_fnIsDirty(
409 IPersistStream* iface)
411 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
413 TRACE("(%p)\n", This);
415 return S_OK;
419 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
421 DWORD count;
422 USHORT len;
423 LPVOID temp;
424 LPWSTR str;
425 HRESULT r;
427 TRACE("%p\n", stm);
429 count = 0;
430 r = IStream_Read(stm, &len, sizeof(len), &count);
431 if ( FAILED (r) || ( count != sizeof(len) ) )
432 return E_FAIL;
434 if( unicode )
435 len *= sizeof (WCHAR);
437 TRACE("reading %d\n", len);
438 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
439 if( !temp )
440 return E_OUTOFMEMORY;
441 count = 0;
442 r = IStream_Read(stm, temp, len, &count);
443 if( FAILED (r) || ( count != len ) )
445 HeapFree( GetProcessHeap(), 0, temp );
446 return E_FAIL;
449 TRACE("read %s\n", debugstr_an(temp,len));
451 /* convert to unicode if necessary */
452 if( !unicode )
454 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
455 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
456 if( str )
457 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
458 HeapFree( GetProcessHeap(), 0, temp );
460 else
462 count /= 2;
463 str = (LPWSTR) temp;
465 str[count] = 0;
467 *pstr = str;
469 return S_OK;
472 static HRESULT Stream_LoadLocation( IStream* stm )
474 DWORD size;
475 ULONG count;
476 HRESULT r;
477 LOCATION_INFO *loc;
479 TRACE("%p\n",stm);
481 r = IStream_Read( stm, &size, sizeof(size), &count );
482 if( FAILED( r ) )
483 return r;
484 if( count != sizeof(loc->dwTotalSize) )
485 return E_FAIL;
487 loc = HeapAlloc( GetProcessHeap(), 0, size );
488 if( ! loc )
489 return E_OUTOFMEMORY;
491 r = IStream_Read( stm, &loc->dwHeaderSize, size-sizeof(size), &count );
492 if( FAILED( r ) )
493 goto end;
494 if( count != (size - sizeof(size)) )
496 r = E_FAIL;
497 goto end;
499 loc->dwTotalSize = size;
501 TRACE("Read %ld bytes\n",count);
503 /* FIXME: do something useful with it */
504 HeapFree( GetProcessHeap(), 0, loc );
506 return S_OK;
507 end:
508 HeapFree( GetProcessHeap(), 0, loc );
509 return r;
512 /************************************************************************
513 * IPersistStream_Load (IPersistStream)
515 static HRESULT WINAPI IPersistStream_fnLoad(
516 IPersistStream* iface,
517 IStream* stm)
519 LINK_HEADER hdr;
520 ULONG dwBytesRead;
521 BOOL unicode;
522 WCHAR sTemp[MAX_PATH];
523 HRESULT r;
525 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
527 TRACE("(%p)(%p)\n", This, stm);
529 if( !stm )
530 return STG_E_INVALIDPOINTER;
532 dwBytesRead = 0;
533 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
534 if( FAILED( r ) )
535 return r;
537 if( dwBytesRead != sizeof(hdr))
538 return E_FAIL;
539 if( hdr.dwSize != sizeof(hdr))
540 return E_FAIL;
541 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
542 return E_FAIL;
544 /* if( hdr.dwFlags & SCF_PIDL ) */ /* FIXME: seems to always have a PIDL */
546 r = ILLoadFromStream( stm, &This->pPidl );
547 if( FAILED( r ) )
548 return r;
550 This->wHotKey = (WORD)hdr.wHotKey;
551 This->iIcoNdx = hdr.nIcon;
552 FileTimeToSystemTime (&hdr.Time1, &This->time1);
553 FileTimeToSystemTime (&hdr.Time2, &This->time2);
554 FileTimeToSystemTime (&hdr.Time3, &This->time3);
555 #if 1
556 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time1, NULL, sTemp, 256);
557 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
558 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time2, NULL, sTemp, 256);
559 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
560 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time3, NULL, sTemp, 256);
561 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
562 pdump (This->pPidl);
563 #endif
564 if( hdr.dwFlags & SCF_NORMAL )
565 r = Stream_LoadLocation( stm );
566 if( FAILED( r ) )
567 goto end;
568 unicode = hdr.dwFlags & SCF_UNICODE;
569 if( hdr.dwFlags & SCF_DESCRIPTION )
571 r = Stream_LoadString( stm, unicode, &This->sDescription );
572 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
574 if( FAILED( r ) )
575 goto end;
577 if( hdr.dwFlags & SCF_RELATIVE )
579 r = Stream_LoadString( stm, unicode, &This->sPathRel );
580 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
582 if( FAILED( r ) )
583 goto end;
585 if( hdr.dwFlags & SCF_WORKDIR )
587 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
588 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
590 if( FAILED( r ) )
591 goto end;
593 if( hdr.dwFlags & SCF_ARGS )
595 r = Stream_LoadString( stm, unicode, &This->sArgs );
596 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
598 if( FAILED( r ) )
599 goto end;
601 if( hdr.dwFlags & SCF_CUSTOMICON )
603 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
604 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
606 if( FAILED( r ) )
607 goto end;
609 TRACE("OK\n");
611 pdump (This->pPidl);
613 return S_OK;
614 end:
615 return r;
618 /************************************************************************
619 * Stream_WriteString
621 * Helper function for IPersistStream_Save. Writes a unicode string
622 * with terminating nul byte to a stream, preceded by the its length.
624 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
626 USHORT len = lstrlenW( str ) + 1;
627 DWORD count;
628 HRESULT r;
630 r = IStream_Write( stm, &len, sizeof(len), &count );
631 if( FAILED( r ) )
632 return r;
634 len *= sizeof(WCHAR);
636 r = IStream_Write( stm, str, len, &count );
637 if( FAILED( r ) )
638 return r;
640 return S_OK;
643 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR filename )
645 LOCATION_INFO loc;
646 ULONG count;
648 FIXME("writing empty location info\n");
650 memset( &loc, 0, sizeof(loc) );
651 loc.dwTotalSize = sizeof(loc) - sizeof(loc.dwTotalSize);
653 /* FIXME: fill this in */
655 return IStream_Write( stm, &loc, loc.dwTotalSize, &count );
658 /************************************************************************
659 * IPersistStream_Save (IPersistStream)
661 * FIXME: makes assumptions about byte order
663 static HRESULT WINAPI IPersistStream_fnSave(
664 IPersistStream* iface,
665 IStream* stm,
666 BOOL fClearDirty)
668 static const WCHAR wOpen[] = {'o','p','e','n',0};
670 LINK_HEADER header;
671 WCHAR exePath[MAX_PATH];
672 ULONG count;
673 HRESULT r;
675 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
677 TRACE("(%p) %p %x\n", This, stm, fClearDirty);
679 *exePath = '\0';
681 if (This->sPath)
683 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH, NULL, NULL, NULL, NULL);
685 * windows can create lnk files to executables that do not exist yet
686 * so if the executable does not exist the just trust the path they
687 * gave us
689 if( !*exePath ) strcpyW(exePath,This->sPath);
692 /* if there's no PIDL, generate one */
693 if( ! This->pPidl ) This->pPidl = ILCreateFromPathW(exePath);
695 memset(&header, 0, sizeof(header));
696 header.dwSize = sizeof(header);
697 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
699 header.wHotKey = This->wHotKey;
700 header.nIcon = This->iIcoNdx;
701 header.dwFlags = SCF_UNICODE; /* strings are in unicode */
702 header.dwFlags |= SCF_NORMAL; /* how do we determine this ? */
703 if( This->pPidl )
704 header.dwFlags |= SCF_PIDL;
705 if( This->sDescription )
706 header.dwFlags |= SCF_DESCRIPTION;
707 if( This->sWorkDir )
708 header.dwFlags |= SCF_WORKDIR;
709 if( This->sArgs )
710 header.dwFlags |= SCF_ARGS;
711 if( This->sIcoPath )
712 header.dwFlags |= SCF_CUSTOMICON;
714 SystemTimeToFileTime ( &This->time1, &header.Time1 );
715 SystemTimeToFileTime ( &This->time2, &header.Time2 );
716 SystemTimeToFileTime ( &This->time3, &header.Time3 );
718 /* write the Shortcut header */
719 r = IStream_Write( stm, &header, sizeof(header), &count );
720 if( FAILED( r ) )
722 ERR("Write failed at %d\n",__LINE__);
723 return r;
726 TRACE("Writing pidl \n");
728 /* write the PIDL to the shortcut */
729 if( This->pPidl )
731 r = ILSaveToStream( stm, This->pPidl );
732 if( FAILED( r ) )
734 ERR("Failed to write PIDL at %d\n",__LINE__);
735 return r;
739 Stream_WriteLocationInfo( stm, exePath );
741 TRACE("Description = %s\n", debugstr_w(This->sDescription));
742 if( This->sDescription )
743 r = Stream_WriteString( stm, This->sDescription );
745 if( This->sPathRel )
746 r = Stream_WriteString( stm, This->sPathRel );
748 if( This->sWorkDir )
749 r = Stream_WriteString( stm, This->sWorkDir );
751 if( This->sArgs )
752 r = Stream_WriteString( stm, This->sArgs );
754 if( This->sIcoPath )
755 r = Stream_WriteString( stm, This->sIcoPath );
757 return S_OK;
760 /************************************************************************
761 * IPersistStream_GetSizeMax (IPersistStream)
763 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
764 IPersistStream* iface,
765 ULARGE_INTEGER* pcbSize)
767 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
769 TRACE("(%p)\n", This);
771 return E_NOTIMPL;
774 static IPersistStreamVtbl psvt =
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 IShellLinkImpl *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 IShellLinkImpl *This = (IShellLinkImpl *)iface;
977 ULONG refCount = InterlockedIncrement(&This->ref);
979 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
981 return refCount;
983 /******************************************************************************
984 * IShellLinkA_Release
986 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
988 IShellLinkImpl *This = (IShellLinkImpl *)iface;
989 ULONG refCount = InterlockedDecrement(&This->ref);
991 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
993 if (refCount)
994 return refCount;
996 TRACE("-- destroying IShellLink(%p)\n",This);
998 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
999 HeapFree(GetProcessHeap(), 0, This->sArgs);
1000 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1001 HeapFree(GetProcessHeap(), 0, This->sDescription);
1002 HeapFree(GetProcessHeap(),0,This->sPath);
1004 if (This->pPidl)
1005 ILFree(This->pPidl);
1007 LocalFree((HANDLE)This);
1009 return 0;
1012 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1013 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1015 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1017 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1018 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1020 if( cchMaxPath )
1021 pszFile[0] = 0;
1022 if (This->sPath)
1023 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1024 pszFile, cchMaxPath, NULL, NULL);
1026 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1028 return NOERROR;
1031 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1033 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1035 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1037 *ppidl = ILClone(This->pPidl);
1039 return NOERROR;
1042 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1044 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1046 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1048 if (This->pPidl)
1049 ILFree(This->pPidl);
1050 This->pPidl = ILClone (pidl);
1051 This->bDirty = TRUE;
1053 return S_OK;
1056 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1058 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1060 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1062 if( cchMaxName )
1063 pszName[0] = 0;
1064 if( This->sDescription )
1065 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1066 pszName, cchMaxName, NULL, NULL);
1068 return S_OK;
1070 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1072 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1074 TRACE("(%p)->(pName=%s)\n", This, pszName);
1076 HeapFree(GetProcessHeap(), 0, This->sDescription);
1077 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1078 if ( !This->sDescription )
1079 return E_OUTOFMEMORY;
1081 This->bDirty = TRUE;
1083 return S_OK;
1086 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1088 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1090 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1092 if( cchMaxPath )
1093 pszDir[0] = 0;
1094 if( This->sWorkDir )
1095 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1096 pszDir, cchMaxPath, NULL, NULL);
1098 return S_OK;
1101 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1103 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1105 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1107 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1108 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1109 if ( !This->sWorkDir )
1110 return E_OUTOFMEMORY;
1112 This->bDirty = TRUE;
1114 return S_OK;
1117 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1119 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1121 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1123 if( cchMaxPath )
1124 pszArgs[0] = 0;
1125 if( This->sArgs )
1126 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1127 pszArgs, cchMaxPath, NULL, NULL);
1129 return S_OK;
1132 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1134 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1136 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1138 HeapFree(GetProcessHeap(), 0, This->sArgs);
1139 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1140 if( !This->sArgs )
1141 return E_OUTOFMEMORY;
1143 This->bDirty = TRUE;
1145 return S_OK;
1148 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1150 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1152 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1154 *pwHotkey = This->wHotKey;
1156 return S_OK;
1159 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1161 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1163 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1165 This->wHotKey = wHotkey;
1166 This->bDirty = TRUE;
1168 return S_OK;
1171 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1173 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1175 TRACE("(%p)->(%p)\n",This, piShowCmd);
1176 *piShowCmd = This->iShowCmd;
1177 return S_OK;
1180 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1182 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1184 TRACE("(%p) %d\n",This, iShowCmd);
1186 This->iShowCmd = iShowCmd;
1187 This->bDirty = TRUE;
1189 return NOERROR;
1192 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1194 LPCITEMIDLIST pidlLast;
1196 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1198 if (SUCCEEDED(hr)) {
1199 IExtractIconA* pei;
1201 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1203 if (SUCCEEDED(hr)) {
1204 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1206 IExtractIconA_Release(pei);
1209 IShellFolder_Release(psf);
1212 return hr;
1215 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1217 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1219 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1221 if (cchIconPath)
1222 pszIconPath[0] = 0;
1224 if (This->sIcoPath) {
1225 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1226 *piIcon = This->iIcoNdx;
1227 return S_OK;
1230 if (This->pPidl || This->sPath) {
1231 IShellFolder* pdsk;
1233 HRESULT hr = SHGetDesktopFolder(&pdsk);
1235 if (SUCCEEDED(hr)) {
1236 /* first look for an icon using the PIDL (if present) */
1237 if (This->pPidl)
1238 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1239 else
1240 hr = E_FAIL;
1242 /* if we couldn't find an icon yet, look for it using the file system path */
1243 if (FAILED(hr) && This->sPath) {
1244 LPITEMIDLIST pidl;
1246 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1248 if (SUCCEEDED(hr)) {
1249 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1251 SHFree(pidl);
1255 IShellFolder_Release(pdsk);
1258 return hr;
1259 } else
1260 return E_FAIL;
1263 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1265 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1267 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1269 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1270 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1271 if ( !This->sIcoPath )
1272 return E_OUTOFMEMORY;
1274 This->iIcoNdx = iIcon;
1275 This->bDirty = TRUE;
1277 return S_OK;
1280 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1282 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1284 FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1286 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1287 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1288 This->bDirty = TRUE;
1290 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1293 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1295 HRESULT hr = S_OK;
1297 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1299 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1301 /*FIXME: use IResolveShellLink interface */
1303 if (!This->sPath && This->pPidl) {
1304 WCHAR buffer[MAX_PATH];
1306 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1308 if (SUCCEEDED(hr) && *buffer) {
1309 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1310 if (!This->sPath)
1311 return E_OUTOFMEMORY;
1313 lstrcpyW(This->sPath, buffer);
1315 This->bDirty = TRUE;
1316 } else
1317 hr = S_OK; /* don't report an error occurred while just caching information */
1320 if (!This->sIcoPath && This->sPath) {
1321 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1322 if (!This->sIcoPath)
1323 return E_OUTOFMEMORY;
1325 lstrcpyW(This->sIcoPath, This->sPath);
1326 This->iIcoNdx = 0;
1328 This->bDirty = TRUE;
1331 return hr;
1334 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1336 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1337 char buffer[MAX_PATH];
1338 LPSTR fname;
1340 TRACE("(%p)->(path=%s)\n",This, pszFile);
1342 if (!GetFullPathNameA(pszFile, MAX_PATH, buffer, &fname))
1343 return E_FAIL;
1345 HeapFree(GetProcessHeap(), 0, This->sPath);
1346 This->sPath = HEAP_strdupAtoW(GetProcessHeap(), 0, buffer);
1347 if( !This->sPath )
1348 return E_OUTOFMEMORY;
1350 This->bDirty = TRUE;
1352 return S_OK;
1355 /**************************************************************************
1356 * IShellLink Implementation
1359 static IShellLinkAVtbl slvt =
1361 IShellLinkA_fnQueryInterface,
1362 IShellLinkA_fnAddRef,
1363 IShellLinkA_fnRelease,
1364 IShellLinkA_fnGetPath,
1365 IShellLinkA_fnGetIDList,
1366 IShellLinkA_fnSetIDList,
1367 IShellLinkA_fnGetDescription,
1368 IShellLinkA_fnSetDescription,
1369 IShellLinkA_fnGetWorkingDirectory,
1370 IShellLinkA_fnSetWorkingDirectory,
1371 IShellLinkA_fnGetArguments,
1372 IShellLinkA_fnSetArguments,
1373 IShellLinkA_fnGetHotkey,
1374 IShellLinkA_fnSetHotkey,
1375 IShellLinkA_fnGetShowCmd,
1376 IShellLinkA_fnSetShowCmd,
1377 IShellLinkA_fnGetIconLocation,
1378 IShellLinkA_fnSetIconLocation,
1379 IShellLinkA_fnSetRelativePath,
1380 IShellLinkA_fnResolve,
1381 IShellLinkA_fnSetPath
1385 /**************************************************************************
1386 * IShellLinkW_fnQueryInterface
1388 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1389 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1391 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1393 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1396 /******************************************************************************
1397 * IShellLinkW_fnAddRef
1399 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1401 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1403 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1405 return IShellLinkA_AddRef((IShellLinkA*)This);
1407 /******************************************************************************
1408 * IShellLinkW_fnRelease
1411 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1413 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1415 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1417 return IShellLinkA_Release((IShellLinkA*)This);
1420 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1422 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1424 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",
1425 This, pszFile, cchMaxPath, pfd, fFlags);
1427 if( cchMaxPath )
1428 pszFile[0] = 0;
1429 if( This->sPath )
1430 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1432 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1434 return NOERROR;
1437 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1439 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1441 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1443 if( This->pPidl)
1444 *ppidl = ILClone( This->pPidl );
1445 else
1446 *ppidl = NULL;
1448 return S_OK;
1451 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1453 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1455 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1457 if( This->pPidl )
1458 ILFree( This->pPidl );
1459 This->pPidl = ILClone( pidl );
1460 if( !This->pPidl )
1461 return E_FAIL;
1463 This->bDirty = TRUE;
1465 return S_OK;
1468 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1470 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1472 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1474 if( cchMaxName )
1475 pszName[0] = 0;
1476 if( This->sDescription )
1477 lstrcpynW( pszName, This->sDescription, cchMaxName );
1479 return S_OK;
1482 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1484 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1486 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1488 HeapFree(GetProcessHeap(), 0, This->sDescription);
1489 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1490 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1491 if ( !This->sDescription )
1492 return E_OUTOFMEMORY;
1494 lstrcpyW( This->sDescription, pszName );
1495 This->bDirty = TRUE;
1497 return S_OK;
1500 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1502 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1504 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1506 if( cchMaxPath )
1507 pszDir[0] = 0;
1508 if( This->sWorkDir )
1509 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1511 return S_OK;
1514 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1516 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1518 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1520 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1521 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1522 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1523 if ( !This->sWorkDir )
1524 return E_OUTOFMEMORY;
1525 lstrcpyW( This->sWorkDir, pszDir );
1526 This->bDirty = TRUE;
1528 return S_OK;
1531 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1533 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1535 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1537 if( cchMaxPath )
1538 pszArgs[0] = 0;
1539 if( This->sArgs )
1540 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1542 return NOERROR;
1545 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1547 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1549 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1551 HeapFree(GetProcessHeap(), 0, This->sArgs);
1552 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1553 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1554 if ( !This->sArgs )
1555 return E_OUTOFMEMORY;
1556 lstrcpyW( This->sArgs, pszArgs );
1557 This->bDirty = TRUE;
1559 return S_OK;
1562 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1564 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1566 TRACE("(%p)->(%p)\n",This, pwHotkey);
1568 *pwHotkey=This->wHotKey;
1570 return S_OK;
1573 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1575 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1577 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1579 This->wHotKey = wHotkey;
1580 This->bDirty = TRUE;
1582 return S_OK;
1585 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1587 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1589 TRACE("(%p)->(%p)\n",This, piShowCmd);
1591 *piShowCmd = This->iShowCmd;
1593 return S_OK;
1596 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1598 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1600 This->iShowCmd = iShowCmd;
1601 This->bDirty = TRUE;
1603 return S_OK;
1606 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1608 LPCITEMIDLIST pidlLast;
1610 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1612 if (SUCCEEDED(hr)) {
1613 IExtractIconW* pei;
1615 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1617 if (SUCCEEDED(hr)) {
1618 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1620 IExtractIconW_Release(pei);
1623 IShellFolder_Release(psf);
1626 return hr;
1629 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1631 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1633 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1635 if (cchIconPath)
1636 pszIconPath[0] = 0;
1638 if (This->sIcoPath) {
1639 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1640 *piIcon = This->iIcoNdx;
1641 return S_OK;
1644 if (This->pPidl || This->sPath) {
1645 IShellFolder* pdsk;
1647 HRESULT hr = SHGetDesktopFolder(&pdsk);
1649 if (SUCCEEDED(hr)) {
1650 /* first look for an icon using the PIDL (if present) */
1651 if (This->pPidl)
1652 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1653 else
1654 hr = E_FAIL;
1656 /* if we couldn't find an icon yet, look for it using the file system path */
1657 if (FAILED(hr) && This->sPath) {
1658 LPITEMIDLIST pidl;
1660 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1662 if (SUCCEEDED(hr)) {
1663 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1665 SHFree(pidl);
1669 IShellFolder_Release(pdsk);
1672 return hr;
1673 } else
1674 return E_FAIL;
1677 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1679 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1681 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1683 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1684 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1685 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1686 if ( !This->sIcoPath )
1687 return E_OUTOFMEMORY;
1688 lstrcpyW( This->sIcoPath, pszIconPath );
1690 This->iIcoNdx = iIcon;
1691 This->bDirty = TRUE;
1693 return S_OK;
1696 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1698 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1700 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1702 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1703 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1704 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1705 if ( !This->sPathRel )
1706 return E_OUTOFMEMORY;
1707 lstrcpyW( This->sPathRel, pszPathRel );
1708 This->bDirty = TRUE;
1710 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1713 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1715 HRESULT hr = S_OK;
1717 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1719 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1721 /*FIXME: use IResolveShellLink interface */
1723 if (!This->sPath && This->pPidl) {
1724 WCHAR buffer[MAX_PATH];
1726 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1728 if (SUCCEEDED(hr) && *buffer) {
1729 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1730 if (!This->sPath)
1731 return E_OUTOFMEMORY;
1733 lstrcpyW(This->sPath, buffer);
1735 This->bDirty = TRUE;
1736 } else
1737 hr = S_OK; /* don't report an error occurred while just caching information */
1740 if (!This->sIcoPath && This->sPath) {
1741 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1742 if (!This->sIcoPath)
1743 return E_OUTOFMEMORY;
1745 lstrcpyW(This->sIcoPath, This->sPath);
1746 This->iIcoNdx = 0;
1748 This->bDirty = TRUE;
1751 return hr;
1754 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1756 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1757 WCHAR buffer[MAX_PATH];
1758 LPWSTR fname;
1760 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1762 if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
1763 return E_FAIL;
1765 HeapFree(GetProcessHeap(), 0, This->sPath);
1766 This->sPath = HeapAlloc( GetProcessHeap(), 0,
1767 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
1768 if (!This->sPath)
1769 return E_OUTOFMEMORY;
1771 lstrcpyW(This->sPath, buffer);
1772 This->bDirty = TRUE;
1774 return S_OK;
1777 /**************************************************************************
1778 * IShellLinkW Implementation
1781 static IShellLinkWVtbl slvtw =
1783 IShellLinkW_fnQueryInterface,
1784 IShellLinkW_fnAddRef,
1785 IShellLinkW_fnRelease,
1786 IShellLinkW_fnGetPath,
1787 IShellLinkW_fnGetIDList,
1788 IShellLinkW_fnSetIDList,
1789 IShellLinkW_fnGetDescription,
1790 IShellLinkW_fnSetDescription,
1791 IShellLinkW_fnGetWorkingDirectory,
1792 IShellLinkW_fnSetWorkingDirectory,
1793 IShellLinkW_fnGetArguments,
1794 IShellLinkW_fnSetArguments,
1795 IShellLinkW_fnGetHotkey,
1796 IShellLinkW_fnSetHotkey,
1797 IShellLinkW_fnGetShowCmd,
1798 IShellLinkW_fnSetShowCmd,
1799 IShellLinkW_fnGetIconLocation,
1800 IShellLinkW_fnSetIconLocation,
1801 IShellLinkW_fnSetRelativePath,
1802 IShellLinkW_fnResolve,
1803 IShellLinkW_fnSetPath