added missing PIDL parameter for SHELL_FindExecutable()
[reactos.git] / reactos / lib / shell32 / shelllink.c
1 /*
2 *
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTES
21 * Nearly complete informations about the binary formats
22 * of .lnk files available at http://www.wotsit.org
23 *
24 */
25
26 #include "config.h"
27
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <errno.h>
36 #include <limits.h>
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
39 #endif
40 #include "wine/debug.h"
41 #include "wine/port.h"
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "winreg.h"
47
48 #include "winuser.h"
49 #include "wingdi.h"
50 #include "shlobj.h"
51 #include "undocshell.h"
52
53 #include "pidl.h"
54 #include "shell32_main.h"
55 #include "shlguid.h"
56 #include "shlwapi.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(shell);
59
60 /* link file formats */
61
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_UNC 0x80
71 //#define SCF_UNICODE 0x1000
72 #define SCF_UNICODE 0x80
73
74 #include "pshpack1.h"
75
76 typedef struct _LINK_HEADER
77 {
78 DWORD dwSize; /* 0x00 size of the header - 0x4c */
79 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
80 DWORD dwFlags; /* 0x14 describes elements following */
81 DWORD dwFileAttr; /* 0x18 attributes of the target file */
82 FILETIME Time1; /* 0x1c */
83 FILETIME Time2; /* 0x24 */
84 FILETIME Time3; /* 0x2c */
85 DWORD dwFileLength; /* 0x34 File length */
86 DWORD nIcon; /* 0x38 icon number */
87 DWORD fStartup; /* 0x3c startup type */
88 DWORD wHotKey; /* 0x40 hotkey */
89 DWORD Unknown5; /* 0x44 */
90 DWORD Unknown6; /* 0x48 */
91 } LINK_HEADER, * PLINK_HEADER;
92
93 #define SHLINK_LOCAL 0
94 #define SHLINK_REMOTE 1
95
96 typedef struct _LOCATION_INFO
97 {
98 DWORD dwTotalSize;
99 DWORD dwHeaderSize;
100 DWORD dwFlags;
101 DWORD dwVolTableOfs;
102 DWORD dwLocalPathOfs;
103 DWORD dwNetworkVolTableOfs;
104 DWORD dwFinalPathOfs;
105 } LOCATION_INFO;
106
107 typedef struct _LOCAL_VOLUME_INFO
108 {
109 DWORD dwSize;
110 DWORD dwType;
111 DWORD dwVolSerial;
112 DWORD dwVolLabelOfs;
113 } LOCAL_VOLUME_INFO;
114
115 #include "poppack.h"
116
117 static ICOM_VTABLE(IShellLinkA) slvt;
118 static ICOM_VTABLE(IShellLinkW) slvtw;
119 static ICOM_VTABLE(IPersistFile) pfvt;
120 static ICOM_VTABLE(IPersistStream) psvt;
121
122 /* IShellLink Implementation */
123
124 typedef struct
125 {
126 ICOM_VFIELD(IShellLinkA);
127 DWORD ref;
128
129 ICOM_VTABLE(IShellLinkW)* lpvtblw;
130 ICOM_VTABLE(IPersistFile)* lpvtblPersistFile;
131 ICOM_VTABLE(IPersistStream)* lpvtblPersistStream;
132
133 /* data structures according to the informations in the link */
134 LPITEMIDLIST pPidl;
135 WORD wHotKey;
136 SYSTEMTIME time1;
137 SYSTEMTIME time2;
138 SYSTEMTIME time3;
139
140 DWORD iShowCmd;
141 LPWSTR sIcoPath;
142 INT iIcoNdx;
143 LPWSTR sPath;
144 LPWSTR sArgs;
145 LPWSTR sWorkDir;
146 LPWSTR sDescription;
147 LPWSTR sPathRel;
148
149 BOOL bDirty;
150 } IShellLinkImpl;
151
152 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
153 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
154
155 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
156 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
157
158 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
159 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
160 #define _IPersistStream_From_ICOM_THIS(class, name) class* StreamThis = (class*)(((char*)name)+_IPersistStream_Offset)
161
162
163 /* strdup on the process heap */
164 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
165 {
166 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
167 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
168 if( !p )
169 return p;
170 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
171 return p;
172 }
173
174
175 /**************************************************************************
176 * IPersistFile_QueryInterface
177 */
178 static HRESULT WINAPI IPersistFile_fnQueryInterface(
179 IPersistFile* iface,
180 REFIID riid,
181 LPVOID *ppvObj)
182 {
183 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
184
185 TRACE("(%p)\n",This);
186
187 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
188 }
189
190 /******************************************************************************
191 * IPersistFile_AddRef
192 */
193 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
194 {
195 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
196
197 TRACE("(%p)->(count=%lu)\n",This,This->ref);
198
199 return IShellLinkA_AddRef((IShellLinkA*)This);
200 }
201 /******************************************************************************
202 * IPersistFile_Release
203 */
204 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
205 {
206 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
207
208 TRACE("(%p)->(count=%lu)\n",This,This->ref);
209
210 return IShellLinkA_Release((IShellLinkA*)This);
211 }
212
213 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
214 {
215 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
216 FIXME("(%p)\n",This);
217 return NOERROR;
218 }
219 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
220 {
221 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
222
223 FIXME("(%p)\n",This);
224
225 if (This->bDirty)
226 return S_OK;
227
228 return S_FALSE;
229 }
230 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
231 {
232 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
233 _IPersistStream_From_ICOM_THIS(IPersistStream, This);
234 HRESULT r;
235 IStream *stm;
236
237 TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
238
239 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
240 if( SUCCEEDED( r ) )
241 {
242 r = IPersistStream_Load(StreamThis, stm);
243 IStream_Release( stm );
244 }
245
246 return r;
247 }
248
249 static BOOL StartLinkProcessor( LPCOLESTR szLink )
250 {
251 const WCHAR szFormat[] = {'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
252 ' ','-','r',' ','"','%','s','"',0 };
253 LONG len;
254 LPWSTR buffer;
255 STARTUPINFOW si;
256 PROCESS_INFORMATION pi;
257
258 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
259 buffer = HeapAlloc( GetProcessHeap(), 0, len );
260 if( !buffer )
261 return FALSE;
262
263 wsprintfW( buffer, szFormat, szLink );
264
265 TRACE("starting %s\n",debugstr_w(buffer));
266
267 memset(&si, 0, sizeof(si));
268 si.cb = sizeof(si);
269 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
270
271 /* wait for a while to throttle the creation of linker processes */
272 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
273 WARN("Timed out waiting for shell linker\n");
274
275 CloseHandle( pi.hProcess );
276 CloseHandle( pi.hThread );
277
278 return TRUE;
279 }
280
281 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
282 {
283 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
284 _IPersistStream_From_ICOM_THIS(IPersistStream, This);
285 HRESULT r;
286 IStream *stm;
287
288 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
289
290 if (!pszFileName || !This->sPath)
291 return ERROR_UNKNOWN;
292
293 r = CreateStreamOnFile(pszFileName, STGM_READWRITE | STGM_CREATE, &stm);
294 if( SUCCEEDED( r ) )
295 {
296 r = IPersistStream_Save(StreamThis, stm, FALSE);
297 IStream_Release( stm );
298
299 if( SUCCEEDED( r ) )
300 StartLinkProcessor( pszFileName );
301 else
302 {
303 DeleteFileW( pszFileName );
304 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
305 }
306 }
307
308 return r;
309 }
310
311 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
312 {
313 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
314 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
315 return NOERROR;
316 }
317 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
318 {
319 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
320 FIXME("(%p)\n",This);
321 return NOERROR;
322 }
323
324 static ICOM_VTABLE(IPersistFile) pfvt =
325 {
326 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
327 IPersistFile_fnQueryInterface,
328 IPersistFile_fnAddRef,
329 IPersistFile_fnRelease,
330 IPersistFile_fnGetClassID,
331 IPersistFile_fnIsDirty,
332 IPersistFile_fnLoad,
333 IPersistFile_fnSave,
334 IPersistFile_fnSaveCompleted,
335 IPersistFile_fnGetCurFile
336 };
337
338 /************************************************************************
339 * IPersistStream_QueryInterface
340 */
341 static HRESULT WINAPI IPersistStream_fnQueryInterface(
342 IPersistStream* iface,
343 REFIID riid,
344 VOID** ppvoid)
345 {
346 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
347
348 TRACE("(%p)\n",This);
349
350 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
351 }
352
353 /************************************************************************
354 * IPersistStream_Release
355 */
356 static ULONG WINAPI IPersistStream_fnRelease(
357 IPersistStream* iface)
358 {
359 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
360
361 TRACE("(%p)\n",This);
362
363 return IShellLinkA_Release((IShellLinkA*)This);
364 }
365
366 /************************************************************************
367 * IPersistStream_AddRef
368 */
369 static ULONG WINAPI IPersistStream_fnAddRef(
370 IPersistStream* iface)
371 {
372 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
373
374 TRACE("(%p)\n",This);
375
376 return IShellLinkA_AddRef((IShellLinkA*)This);
377 }
378
379 /************************************************************************
380 * IPersistStream_GetClassID
381 *
382 */
383 static HRESULT WINAPI IPersistStream_fnGetClassID(
384 IPersistStream* iface,
385 CLSID* pClassID)
386 {
387 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
388
389 TRACE("(%p)\n", This);
390
391 if (pClassID==0)
392 return E_POINTER;
393
394 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
395
396 return S_OK;
397 }
398
399 /************************************************************************
400 * IPersistStream_IsDirty (IPersistStream)
401 */
402 static HRESULT WINAPI IPersistStream_fnIsDirty(
403 IPersistStream* iface)
404 {
405 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
406
407 TRACE("(%p)\n", This);
408
409 return S_OK;
410 }
411
412
413 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
414 {
415 DWORD count;
416 USHORT len;
417 LPVOID temp;
418 LPWSTR str;
419 HRESULT r;
420
421 TRACE("%p\n", stm);
422
423 count = 0;
424 r = IStream_Read(stm, &len, sizeof(len), &count);
425 if ( FAILED (r) || ( count != sizeof(len) ) )
426 return E_FAIL;
427
428 if( unicode )
429 len *= sizeof (WCHAR);
430
431 TRACE("reading %d\n", len);
432 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
433 if( !temp )
434 return E_OUTOFMEMORY;
435 count = 0;
436 r = IStream_Read(stm, temp, len, &count);
437 if( FAILED (r) || ( count != len ) )
438 {
439 HeapFree( GetProcessHeap(), 0, temp );
440 return E_FAIL;
441 }
442
443 TRACE("read %s\n", debugstr_an(temp,len));
444
445 /* convert to unicode if necessary */
446 if( !unicode )
447 {
448 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
449 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
450 if( str )
451 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
452 HeapFree( GetProcessHeap(), 0, temp );
453 }
454 else
455 {
456 count /= 2;
457 str = (LPWSTR) temp;
458 }
459 str[count] = 0;
460
461 *pstr = str;
462
463 return S_OK;
464 }
465
466 static HRESULT Stream_LoadLocation( IStream* stm )
467 {
468 DWORD size;
469 ULONG count;
470 HRESULT r;
471 LOCATION_INFO *loc;
472
473 TRACE("%p\n",stm);
474
475 r = IStream_Read( stm, &size, sizeof(size), &count );
476 if( FAILED( r ) )
477 return r;
478 if( count != sizeof(loc->dwTotalSize) )
479 return E_FAIL;
480
481 loc = HeapAlloc( GetProcessHeap(), 0, size );
482 if( ! loc )
483 return E_OUTOFMEMORY;
484
485 r = IStream_Read( stm, &loc->dwHeaderSize, size-sizeof(size), &count );
486 if( FAILED( r ) )
487 goto end;
488 if( count != (size - sizeof(size)) )
489 {
490 r = E_FAIL;
491 goto end;
492 }
493 loc->dwTotalSize = size;
494
495 TRACE("Read %ld bytes\n",count);
496
497 /* FIXME: do something useful with it */
498 HeapFree( GetProcessHeap(), 0, loc );
499
500 return S_OK;
501 end:
502 HeapFree( GetProcessHeap(), 0, loc );
503 return r;
504 }
505
506 /************************************************************************
507 * IPersistStream_Load (IPersistStream)
508 */
509 static HRESULT WINAPI IPersistStream_fnLoad(
510 IPersistStream* iface,
511 IStream* stm)
512 {
513 LINK_HEADER hdr;
514 ULONG dwBytesRead;
515 BOOL unicode;
516 WCHAR sTemp[MAX_PATH];
517 HRESULT r;
518
519 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
520
521 TRACE("(%p)(%p)\n", This, stm);
522
523 if( !stm )
524 return STG_E_INVALIDPOINTER;
525
526 dwBytesRead = 0;
527 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
528 if( FAILED( r ) )
529 return r;
530
531 if( dwBytesRead != sizeof(hdr))
532 return E_FAIL;
533 if( hdr.dwSize != sizeof(hdr))
534 return E_FAIL;
535 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
536 return E_FAIL;
537
538 if( hdr.dwFlags & SCF_PIDL )
539 {
540 r = ILLoadFromStream( stm, &This->pPidl );
541 if( FAILED( r ) )
542 return r;
543 }
544 This->wHotKey = (WORD)hdr.wHotKey;
545 This->iIcoNdx = hdr.nIcon;
546 FileTimeToSystemTime (&hdr.Time1, &This->time1);
547 FileTimeToSystemTime (&hdr.Time2, &This->time2);
548 FileTimeToSystemTime (&hdr.Time3, &This->time3);
549 #if 1
550 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time1, NULL, sTemp, 256);
551 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
552 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time2, NULL, sTemp, 256);
553 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
554 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&This->time3, NULL, sTemp, 256);
555 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
556 pdump (This->pPidl);
557 #endif
558 if( hdr.dwFlags & SCF_NORMAL )
559 r = Stream_LoadLocation( stm );
560 if( FAILED( r ) )
561 goto end;
562 unicode = hdr.dwFlags & SCF_UNICODE;
563 if( hdr.dwFlags & SCF_DESCRIPTION )
564 {
565 r = Stream_LoadString( stm, unicode, &This->sDescription );
566 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
567 }
568 if( FAILED( r ) )
569 goto end;
570
571 if( hdr.dwFlags & SCF_RELATIVE )
572 {
573 r = Stream_LoadString( stm, unicode, &This->sPathRel );
574 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
575 }
576 if( FAILED( r ) )
577 goto end;
578
579 if( hdr.dwFlags & SCF_WORKDIR )
580 {
581 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
582 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
583 }
584 if( FAILED( r ) )
585 goto end;
586
587 if( hdr.dwFlags & SCF_ARGS )
588 {
589 r = Stream_LoadString( stm, unicode, &This->sArgs );
590 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
591 }
592 if( FAILED( r ) )
593 goto end;
594
595 if( hdr.dwFlags & SCF_CUSTOMICON )
596 {
597 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
598 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
599 }
600 if( FAILED( r ) )
601 goto end;
602
603 TRACE("OK\n");
604
605 pdump (This->pPidl);
606
607 return S_OK;
608 end:
609 return r;
610 }
611
612 /************************************************************************
613 * Stream_WriteString
614 *
615 * Helper function for IPersistStream_Save. Writes a unicode string
616 * with terminating nul byte to a stream, preceded by the its length.
617 */
618 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
619 {
620 USHORT len = lstrlenW( str ) + 1;
621 DWORD count;
622 HRESULT r;
623
624 r = IStream_Write( stm, &len, sizeof(len), &count );
625 if( FAILED( r ) )
626 return r;
627
628 len *= sizeof(WCHAR);
629
630 r = IStream_Write( stm, str, len, &count );
631 if( FAILED( r ) )
632 return r;
633
634 return S_OK;
635 }
636
637 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR filename )
638 {
639 LOCATION_INFO loc;
640 ULONG count;
641
642 FIXME("writing empty location info\n");
643
644 memset( &loc, 0, sizeof(loc) );
645 loc.dwTotalSize = sizeof(loc) - sizeof(loc.dwTotalSize);
646
647 /* FIXME: fill this in */
648
649 return IStream_Write( stm, &loc, loc.dwTotalSize, &count );
650 }
651
652 /************************************************************************
653 * IPersistStream_Save (IPersistStream)
654 *
655 * FIXME: makes assumptions about byte order
656 */
657 static HRESULT WINAPI IPersistStream_fnSave(
658 IPersistStream* iface,
659 IStream* stm,
660 BOOL fClearDirty)
661 {
662 static const WCHAR wOpen[] = {'o','p','e','n',0};
663
664 LINK_HEADER header;
665 WCHAR exePath[MAX_PATH];
666 ULONG count;
667 HRESULT r;
668
669 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
670
671 TRACE("(%p) %p %x\n", This, stm, fClearDirty);
672
673 *exePath = '\0';
674
675 if (This->sPath)
676 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, NULL, NULL, NULL, NULL);
677
678 /* if there's no PIDL, generate one */
679 if( ! This->pPidl )
680 {
681 if( !*exePath )
682 return E_FAIL;
683
684 This->pPidl = ILCreateFromPathW(exePath);
685 }
686
687 memset(&header, 0, sizeof(header));
688 header.dwSize = sizeof(header);
689 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
690
691 header.wHotKey = This->wHotKey;
692 header.nIcon = This->iIcoNdx;
693 header.dwFlags = SCF_UNICODE; /* strings are in unicode */
694 header.dwFlags |= SCF_NORMAL; /* how do we determine this ? */
695 if( This->pPidl )
696 header.dwFlags |= SCF_PIDL;
697 if( This->sDescription )
698 header.dwFlags |= SCF_DESCRIPTION;
699 if( This->sWorkDir )
700 header.dwFlags |= SCF_WORKDIR;
701 if( This->sArgs )
702 header.dwFlags |= SCF_ARGS;
703 if( This->sIcoPath )
704 header.dwFlags |= SCF_CUSTOMICON;
705
706 SystemTimeToFileTime ( &This->time1, &header.Time1 );
707 SystemTimeToFileTime ( &This->time2, &header.Time2 );
708 SystemTimeToFileTime ( &This->time3, &header.Time3 );
709
710 /* write the Shortcut header */
711 r = IStream_Write( stm, &header, sizeof(header), &count );
712 if( FAILED( r ) )
713 {
714 ERR("Write failed at %d\n",__LINE__);
715 return r;
716 }
717
718 TRACE("Writing pidl \n");
719
720 /* write the PIDL to the shortcut */
721 if( This->pPidl )
722 {
723 r = ILSaveToStream( stm, This->pPidl );
724 if( FAILED( r ) )
725 {
726 ERR("Failed to write PIDL at %d\n",__LINE__);
727 return r;
728 }
729 }
730
731 Stream_WriteLocationInfo( stm, exePath );
732
733 TRACE("Description = %s\n", debugstr_w(This->sDescription));
734 if( This->sDescription )
735 r = Stream_WriteString( stm, This->sDescription );
736
737 if( This->sPathRel )
738 r = Stream_WriteString( stm, This->sPathRel );
739
740 if( This->sWorkDir )
741 r = Stream_WriteString( stm, This->sWorkDir );
742
743 if( This->sArgs )
744 r = Stream_WriteString( stm, This->sArgs );
745
746 if( This->sIcoPath )
747 r = Stream_WriteString( stm, This->sIcoPath );
748
749 return S_OK;
750 }
751
752 /************************************************************************
753 * IPersistStream_GetSizeMax (IPersistStream)
754 */
755 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
756 IPersistStream* iface,
757 ULARGE_INTEGER* pcbSize)
758 {
759 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
760
761 TRACE("(%p)\n", This);
762
763 return E_NOTIMPL;
764 }
765
766 static ICOM_VTABLE(IPersistStream) psvt =
767 {
768 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
769 IPersistStream_fnQueryInterface,
770 IPersistStream_fnAddRef,
771 IPersistStream_fnRelease,
772 IPersistStream_fnGetClassID,
773 IPersistStream_fnIsDirty,
774 IPersistStream_fnLoad,
775 IPersistStream_fnSave,
776 IPersistStream_fnGetSizeMax
777 };
778
779 /**************************************************************************
780 * IShellLink_Constructor
781 */
782 HRESULT WINAPI IShellLink_Constructor (
783 IUnknown * pUnkOuter,
784 REFIID riid,
785 LPVOID * ppv)
786 {
787 IShellLinkImpl * sl;
788
789 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
790
791 *ppv = NULL;
792
793 if(pUnkOuter) return CLASS_E_NOAGGREGATION;
794 sl = (IShellLinkImpl *) LocalAlloc(GMEM_ZEROINIT,sizeof(IShellLinkImpl));
795 if (!sl) return E_OUTOFMEMORY;
796
797 sl->ref = 1;
798 sl->lpVtbl = &slvt;
799 sl->lpvtblw = &slvtw;
800 sl->lpvtblPersistFile = &pfvt;
801 sl->lpvtblPersistStream = &psvt;
802 sl->iShowCmd = SW_SHOWNORMAL;
803 sl->bDirty = FALSE;
804
805 TRACE("(%p)->()\n",sl);
806
807 if (IsEqualIID(riid, &IID_IUnknown) ||
808 IsEqualIID(riid, &IID_IShellLinkA))
809 *ppv = sl;
810 else if (IsEqualIID(riid, &IID_IShellLinkW))
811 *ppv = &(sl->lpvtblw);
812 else {
813 LocalFree((HLOCAL)sl);
814 ERR("E_NOINTERFACE\n");
815 return E_NOINTERFACE;
816 }
817
818 return S_OK;
819 }
820
821 static BOOL SHELL_ExistsFileW(LPCWSTR path)
822 {
823 HANDLE hfile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
824
825 if (hfile != INVALID_HANDLE_VALUE) {
826 CloseHandle(hfile);
827 return TRUE;
828 } else
829 return FALSE;
830 }
831
832 /**************************************************************************
833 * SHELL_ShellLink_UpdatePath
834 * update absolute path in sPath using relative path in sPathRel
835 */
836 static HRESULT SHELL_ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
837 {
838 if (!path || !psPath)
839 return E_INVALIDARG;
840
841 if (!*psPath && sPathRel) {
842 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
843
844 /* first try if [directory of link file] + [relative path] finds an existing file */
845 LPCWSTR src = path;
846 LPWSTR last_slash = NULL;
847 LPWSTR dest = buffer;
848 LPWSTR final;
849
850 /* copy path without file name to buffer */
851 while(*src) {
852 if (*src=='/' || *src=='\\')
853 last_slash = dest;
854
855 *dest++ = *src++;
856 }
857
858 lstrcpyW(last_slash? last_slash+1: buffer, sPathRel);
859
860 *abs_path = '\0';
861
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);
870
871 if (SHELL_ExistsFileW(buffer))
872 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
873 lstrcpyW(abs_path, buffer);
874 }
875 }
876
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);
880
881 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
882 if (!*psPath)
883 return E_OUTOFMEMORY;
884
885 lstrcpyW(*psPath, abs_path);
886 }
887
888 return S_OK;
889 }
890
891 /**************************************************************************
892 * IShellLink_ConstructFromFile
893 */
894 HRESULT WINAPI IShellLink_ConstructFromFile (
895 IUnknown* pUnkOuter,
896 REFIID riid,
897 LPCITEMIDLIST pidl,
898 LPVOID* ppv
899 )
900 {
901 IShellLinkW* psl;
902
903 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
904
905 if (SUCCEEDED(hr)) {
906 IPersistFile* ppf;
907
908 *ppv = NULL;
909
910 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
911
912 if (SUCCEEDED(hr)) {
913 WCHAR path[MAX_PATH];
914
915 if (SHGetPathFromIDListW(pidl, path)) {
916 hr = IPersistFile_Load(ppf, path, 0);
917
918 if (SUCCEEDED(hr)) {
919 *ppv = (IUnknown*) psl;
920
921 /*
922 The following code is here, not in IPersistStream_fnLoad() because
923 to be able to convert the relative path into the absolute path,
924 we need to know the path of the shell link file.
925 */
926 if (IsEqualIID(riid, &IID_IShellLinkW)) {
927 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, psl);
928
929 hr = SHELL_ShellLink_UpdatePath(This->sPathRel, path, This->sWorkDir, &This->sPath);
930 } else {
931 ICOM_THIS(IShellLinkImpl, psl);
932
933 hr = SHELL_ShellLink_UpdatePath(This->sPathRel, path, This->sWorkDir, &This->sPath);
934 }
935 }
936 }
937
938 IPersistFile_Release(ppf);
939 }
940
941 if (!*ppv)
942 IShellLinkW_Release(psl);
943 }
944
945 return hr;
946 }
947
948 /**************************************************************************
949 * IShellLinkA_QueryInterface
950 */
951 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
952 {
953 ICOM_THIS(IShellLinkImpl, iface);
954
955 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
956
957 *ppvObj = NULL;
958
959 if(IsEqualIID(riid, &IID_IUnknown) ||
960 IsEqualIID(riid, &IID_IShellLinkA))
961 {
962 *ppvObj = This;
963 }
964 else if(IsEqualIID(riid, &IID_IShellLinkW))
965 {
966 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
967 }
968 else if(IsEqualIID(riid, &IID_IPersistFile))
969 {
970 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
971 }
972 else if(IsEqualIID(riid, &IID_IPersistStream))
973 {
974 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
975 }
976
977 if(*ppvObj)
978 {
979 IUnknown_AddRef((IUnknown*)(*ppvObj));
980 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
981 return S_OK;
982 }
983 TRACE("-- Interface: E_NOINTERFACE\n");
984 return E_NOINTERFACE;
985 }
986
987 /******************************************************************************
988 * IShellLinkA_AddRef
989 */
990 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
991 {
992 ICOM_THIS(IShellLinkImpl, iface);
993
994 TRACE("(%p)->(count=%lu)\n",This,This->ref);
995
996 return ++(This->ref);
997 }
998
999 /******************************************************************************
1000 * IShellLinkA_Release
1001 */
1002 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1003 {
1004 ICOM_THIS(IShellLinkImpl, iface);
1005
1006 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1007
1008 if (--(This->ref))
1009 return This->ref;
1010
1011 TRACE("-- destroying IShellLink(%p)\n",This);
1012
1013 if (This->sIcoPath)
1014 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1015
1016 if (This->sArgs)
1017 HeapFree(GetProcessHeap(), 0, This->sArgs);
1018
1019 if (This->sWorkDir)
1020 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1021
1022 if (This->sDescription)
1023 HeapFree(GetProcessHeap(), 0, This->sDescription);
1024
1025 if (This->sPath)
1026 HeapFree(GetProcessHeap(), 0, This->sPath);
1027
1028 if (This->pPidl)
1029 ILFree(This->pPidl);
1030
1031 LocalFree((HANDLE)This);
1032
1033 return 0;
1034 }
1035
1036 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1037 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1038 {
1039 ICOM_THIS(IShellLinkImpl, iface);
1040
1041 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1042 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1043
1044 if( cchMaxPath )
1045 pszFile[0] = 0;
1046 if (This->sPath)
1047 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1048 pszFile, cchMaxPath, NULL, NULL);
1049
1050 if (pfd) {
1051
1052 FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1053
1054 }
1055
1056 return NOERROR;
1057 }
1058
1059 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1060 {
1061 ICOM_THIS(IShellLinkImpl, iface);
1062
1063 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1064
1065 *ppidl = ILClone(This->pPidl);
1066
1067 return NOERROR;
1068 }
1069
1070 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1071 {
1072 ICOM_THIS(IShellLinkImpl, iface);
1073
1074 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1075
1076 if (This->pPidl)
1077 ILFree(This->pPidl);
1078
1079 This->pPidl = ILClone (pidl);
1080 This->bDirty = TRUE;
1081
1082 return S_OK;
1083 }
1084
1085 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1086 {
1087 ICOM_THIS(IShellLinkImpl, iface);
1088
1089 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1090
1091 if( cchMaxName )
1092 pszName[0] = 0;
1093 if( This->sDescription )
1094 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1095 pszName, cchMaxName, NULL, NULL);
1096
1097 return S_OK;
1098 }
1099 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1100 {
1101 ICOM_THIS(IShellLinkImpl, iface);
1102
1103 TRACE("(%p)->(pName=%s)\n", This, pszName);
1104
1105 if (This->sDescription)
1106 HeapFree(GetProcessHeap(), 0, This->sDescription);
1107 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1108 if ( !This->sDescription )
1109 return E_OUTOFMEMORY;
1110
1111 This->bDirty = TRUE;
1112
1113 return S_OK;
1114 }
1115
1116 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1117 {
1118 ICOM_THIS(IShellLinkImpl, iface);
1119
1120 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1121
1122 if( cchMaxPath )
1123 pszDir[0] = 0;
1124 if( This->sWorkDir )
1125 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1126 pszDir, cchMaxPath, NULL, NULL);
1127
1128 return S_OK;
1129 }
1130
1131 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1132 {
1133 ICOM_THIS(IShellLinkImpl, iface);
1134
1135 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1136
1137 if (This->sWorkDir)
1138 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1139 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1140 if ( !This->sWorkDir )
1141 return E_OUTOFMEMORY;
1142
1143 This->bDirty = TRUE;
1144
1145 return S_OK;
1146 }
1147
1148 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1149 {
1150 ICOM_THIS(IShellLinkImpl, iface);
1151
1152 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1153
1154 if( cchMaxPath )
1155 pszArgs[0] = 0;
1156 if( This->sArgs )
1157 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1158 pszArgs, cchMaxPath, NULL, NULL);
1159
1160 return S_OK;
1161 }
1162
1163 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1164 {
1165 ICOM_THIS(IShellLinkImpl, iface);
1166
1167 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1168
1169 if (This->sArgs)
1170 HeapFree(GetProcessHeap(), 0, This->sArgs);
1171 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1172 if( !This->sArgs )
1173 return E_OUTOFMEMORY;
1174
1175 This->bDirty = TRUE;
1176
1177 return S_OK;
1178 }
1179
1180 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1181 {
1182 ICOM_THIS(IShellLinkImpl, iface);
1183
1184 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1185
1186 *pwHotkey = This->wHotKey;
1187
1188 return S_OK;
1189 }
1190
1191 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1192 {
1193 ICOM_THIS(IShellLinkImpl, iface);
1194
1195 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1196
1197 This->wHotKey = wHotkey;
1198 This->bDirty = TRUE;
1199
1200 return S_OK;
1201 }
1202
1203 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1204 {
1205 ICOM_THIS(IShellLinkImpl, iface);
1206
1207 TRACE("(%p)->(%p)\n",This, piShowCmd);
1208 *piShowCmd = This->iShowCmd;
1209 return S_OK;
1210 }
1211
1212 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1213 {
1214 ICOM_THIS(IShellLinkImpl, iface);
1215
1216 TRACE("(%p) %d\n",This, iShowCmd);
1217
1218 This->iShowCmd = iShowCmd;
1219 This->bDirty = TRUE;
1220
1221 return NOERROR;
1222 }
1223
1224 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1225 {
1226 LPCITEMIDLIST pidlLast;
1227
1228 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1229
1230 if (SUCCEEDED(hr)) {
1231 IExtractIconA* pei;
1232
1233 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1234
1235 if (SUCCEEDED(hr)) {
1236 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1237
1238 IExtractIconA_Release(pei);
1239 }
1240
1241 IShellFolder_Release(psf);
1242 }
1243
1244 return hr;
1245 }
1246
1247 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath, INT cchIconPath, INT *piIcon)
1248 {
1249 ICOM_THIS(IShellLinkImpl, iface);
1250
1251 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1252
1253 if (cchIconPath)
1254 pszIconPath[0] = 0;
1255
1256 if (This->sIcoPath) {
1257 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1258 *piIcon = This->iIcoNdx;
1259 return S_OK;
1260 }
1261
1262 if (This->pPidl || This->sPath) {
1263 IShellFolder* pdsk;
1264
1265 HRESULT hr = SHGetDesktopFolder(&pdsk);
1266
1267 if (SUCCEEDED(hr)) {
1268 /* first look for an icon using the PIDL (if present) */
1269 if (This->pPidl)
1270 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1271 else
1272 hr = E_FAIL;
1273
1274 /* if we couldn't find an icon yet, look for it using the file system path */
1275 if (FAILED(hr) && This->sPath) {
1276 LPITEMIDLIST pidl;
1277
1278 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1279
1280 if (SUCCEEDED(hr)) {
1281 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1282
1283 SHFree(pidl);
1284 }
1285 }
1286
1287 IShellFolder_Release(pdsk);
1288 }
1289
1290 return hr;
1291 } else
1292 return E_FAIL;
1293 }
1294
1295 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath, INT iIcon)
1296 {
1297 ICOM_THIS(IShellLinkImpl, iface);
1298
1299 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1300
1301 if (This->sIcoPath)
1302 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1303 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1304 if ( !This->sIcoPath )
1305 return E_OUTOFMEMORY;
1306
1307 This->iIcoNdx = iIcon;
1308 This->bDirty = TRUE;
1309
1310 return S_OK;
1311 }
1312
1313 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1314 {
1315 ICOM_THIS(IShellLinkImpl, iface);
1316
1317 FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1318
1319 if (This->sPathRel)
1320 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1321
1322 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1323 This->bDirty = TRUE;
1324
1325 return SHELL_ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1326 }
1327
1328 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1329 {
1330 HRESULT hr = S_OK;
1331
1332 ICOM_THIS(IShellLinkImpl, iface);
1333
1334 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1335
1336 /*FIXME: use IResolveShellLink interface */
1337
1338 if (!This->sPath && This->pPidl) {
1339 WCHAR buffer[MAX_PATH];
1340
1341 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1342
1343 if (SUCCEEDED(hr) && *buffer) {
1344 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1345 if (!This->sPath)
1346 return E_OUTOFMEMORY;
1347
1348 lstrcpyW(This->sPath, buffer);
1349
1350 This->bDirty = TRUE;
1351 } else
1352 hr = S_OK; /* don't report any error occured while just caching information */
1353 }
1354
1355 if (!This->sIcoPath && This->sPath) {
1356 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1357 if (!This->sIcoPath)
1358 return E_OUTOFMEMORY;
1359
1360 lstrcpyW(This->sIcoPath, This->sPath);
1361 This->iIcoNdx = 0;
1362
1363 This->bDirty = TRUE;
1364 }
1365
1366 return hr;
1367 }
1368
1369 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1370 {
1371 ICOM_THIS(IShellLinkImpl, iface);
1372
1373 TRACE("(%p)->(path=%s)\n",This, pszFile);
1374
1375 if (This->sPath)
1376 HeapFree(GetProcessHeap(), 0, This->sPath);
1377 This->sPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1378 if( !This->sPath )
1379 return E_OUTOFMEMORY;
1380
1381 This->bDirty = TRUE;
1382
1383 return S_OK;
1384 }
1385
1386 /**************************************************************************
1387 * IShellLink Implementation
1388 */
1389
1390 static ICOM_VTABLE(IShellLinkA) slvt =
1391 {
1392 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1393 IShellLinkA_fnQueryInterface,
1394 IShellLinkA_fnAddRef,
1395 IShellLinkA_fnRelease,
1396 IShellLinkA_fnGetPath,
1397 IShellLinkA_fnGetIDList,
1398 IShellLinkA_fnSetIDList,
1399 IShellLinkA_fnGetDescription,
1400 IShellLinkA_fnSetDescription,
1401 IShellLinkA_fnGetWorkingDirectory,
1402 IShellLinkA_fnSetWorkingDirectory,
1403 IShellLinkA_fnGetArguments,
1404 IShellLinkA_fnSetArguments,
1405 IShellLinkA_fnGetHotkey,
1406 IShellLinkA_fnSetHotkey,
1407 IShellLinkA_fnGetShowCmd,
1408 IShellLinkA_fnSetShowCmd,
1409 IShellLinkA_fnGetIconLocation,
1410 IShellLinkA_fnSetIconLocation,
1411 IShellLinkA_fnSetRelativePath,
1412 IShellLinkA_fnResolve,
1413 IShellLinkA_fnSetPath
1414 };
1415
1416
1417 /**************************************************************************
1418 * IShellLinkW_fnQueryInterface
1419 */
1420 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1421 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1422 {
1423 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1424
1425 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1426 }
1427
1428 /******************************************************************************
1429 * IShellLinkW_fnAddRef
1430 */
1431 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1432 {
1433 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1434
1435 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1436
1437 return IShellLinkA_AddRef((IShellLinkA*)This);
1438 }
1439 /******************************************************************************
1440 * IShellLinkW_fnRelease
1441 */
1442
1443 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1444 {
1445 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1446
1447 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1448
1449 return IShellLinkA_Release((IShellLinkA*)This);
1450 }
1451
1452 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1453 {
1454 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1455
1456 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",
1457 This, pszFile, cchMaxPath, pfd, fFlags);
1458
1459 if( cchMaxPath )
1460 pszFile[0] = 0;
1461 if( This->sPath )
1462 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1463
1464 if (pfd) {
1465
1466 FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1467
1468 }
1469
1470 return NOERROR;
1471 }
1472
1473 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1474 {
1475 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1476
1477 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1478
1479 if( This->pPidl)
1480 *ppidl = ILClone( This->pPidl );
1481 else
1482 *ppidl = NULL;
1483
1484 return S_OK;
1485 }
1486
1487 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1488 {
1489 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1490
1491 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1492
1493 if( This->pPidl )
1494 ILFree( This->pPidl );
1495 This->pPidl = ILClone( pidl );
1496 if( !This->pPidl )
1497 return E_FAIL;
1498
1499 This->bDirty = TRUE;
1500
1501 return S_OK;
1502 }
1503
1504 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1505 {
1506 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1507
1508 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1509
1510 if( cchMaxName )
1511 pszName[0] = 0;
1512 if( This->sDescription )
1513 lstrcpynW( pszName, This->sDescription, cchMaxName );
1514
1515 return S_OK;
1516 }
1517
1518 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1519 {
1520 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1521
1522 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1523
1524 if (This->sDescription)
1525 HeapFree(GetProcessHeap(), 0, This->sDescription);
1526 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1527 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1528 if ( !This->sDescription )
1529 return E_OUTOFMEMORY;
1530
1531 lstrcpyW( This->sDescription, pszName );
1532 This->bDirty = TRUE;
1533
1534 return S_OK;
1535 }
1536
1537 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1538 {
1539 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1540
1541 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1542
1543 if( cchMaxPath )
1544 pszDir[0] = 0;
1545 if( This->sWorkDir )
1546 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1547
1548 return S_OK;
1549 }
1550
1551 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1552 {
1553 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1554
1555 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1556
1557 if (This->sWorkDir)
1558 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1559 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1560 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1561 if ( !This->sWorkDir )
1562 return E_OUTOFMEMORY;
1563
1564 lstrcpyW( This->sWorkDir, pszDir );
1565 This->bDirty = TRUE;
1566
1567 return S_OK;
1568 }
1569
1570 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1571 {
1572 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1573
1574 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1575
1576 if( cchMaxPath )
1577 pszArgs[0] = 0;
1578 if( This->sArgs )
1579 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1580
1581 return NOERROR;
1582 }
1583
1584 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1585 {
1586 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1587
1588 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1589
1590 if (This->sArgs)
1591 HeapFree(GetProcessHeap(), 0, This->sArgs);
1592 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1593 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1594 if ( !This->sArgs )
1595 return E_OUTOFMEMORY;
1596
1597 lstrcpyW( This->sArgs, pszArgs );
1598 This->bDirty = TRUE;
1599
1600 return S_OK;
1601 }
1602
1603 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1604 {
1605 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1606
1607 TRACE("(%p)->(%p)\n",This, pwHotkey);
1608
1609 *pwHotkey=This->wHotKey;
1610
1611 return S_OK;
1612 }
1613
1614 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1615 {
1616 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1617
1618 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1619
1620 This->wHotKey = wHotkey;
1621 This->bDirty = TRUE;
1622
1623 return S_OK;
1624 }
1625
1626 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1627 {
1628 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1629
1630 TRACE("(%p)->(%p)\n",This, piShowCmd);
1631
1632 *piShowCmd = This->iShowCmd;
1633
1634 return S_OK;
1635 }
1636
1637 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1638 {
1639 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1640
1641 This->iShowCmd = iShowCmd;
1642 This->bDirty = TRUE;
1643
1644 return S_OK;
1645 }
1646
1647 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1648 {
1649 LPCITEMIDLIST pidlLast;
1650
1651 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1652
1653 if (SUCCEEDED(hr)) {
1654 IExtractIconW* pei;
1655
1656 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1657
1658 if (SUCCEEDED(hr)) {
1659 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1660
1661 IExtractIconW_Release(pei);
1662 }
1663
1664 IShellFolder_Release(psf);
1665 }
1666
1667 return hr;
1668 }
1669
1670 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath, INT cchIconPath, INT *piIcon)
1671 {
1672 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1673
1674 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1675
1676 if (cchIconPath)
1677 pszIconPath[0] = 0;
1678
1679 if (This->sIcoPath) {
1680 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1681 *piIcon = This->iIcoNdx;
1682 return S_OK;
1683 }
1684
1685 if (This->pPidl || This->sPath) {
1686 IShellFolder* pdsk;
1687
1688 HRESULT hr = SHGetDesktopFolder(&pdsk);
1689
1690 if (SUCCEEDED(hr)) {
1691 /* first look for an icon using the PIDL (if present) */
1692 if (This->pPidl)
1693 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1694 else
1695 hr = E_FAIL;
1696
1697 /* if we couldn't find an icon yet, look for it using the file system path */
1698 if (FAILED(hr) && This->sPath) {
1699 LPITEMIDLIST pidl;
1700
1701 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1702
1703 if (SUCCEEDED(hr)) {
1704 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1705
1706 SHFree(pidl);
1707 }
1708 }
1709
1710 IShellFolder_Release(pdsk);
1711 }
1712
1713 return hr;
1714 } else
1715 return E_FAIL;
1716 }
1717
1718 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath, INT iIcon)
1719 {
1720 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1721
1722 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1723
1724 if (This->sIcoPath)
1725 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1726 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1727 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1728 if ( !This->sIcoPath )
1729 return E_OUTOFMEMORY;
1730 lstrcpyW( This->sIcoPath, pszIconPath );
1731
1732 This->iIcoNdx = iIcon;
1733 This->bDirty = TRUE;
1734
1735 return S_OK;
1736 }
1737
1738 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1739 {
1740 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1741
1742 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1743
1744 if (This->sPathRel)
1745 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1746 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1747 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1748 if ( !This->sPathRel )
1749 return E_OUTOFMEMORY;
1750
1751 lstrcpyW( This->sPathRel, pszPathRel );
1752 This->bDirty = TRUE;
1753
1754 return SHELL_ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1755 }
1756
1757 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1758 {
1759 HRESULT hr = S_OK;
1760
1761 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1762
1763 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1764
1765 /*FIXME: use IResolveShellLink interface */
1766
1767 if (!This->sPath && This->pPidl) {
1768 WCHAR buffer[MAX_PATH];
1769
1770 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1771
1772 if (SUCCEEDED(hr) && *buffer) {
1773 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1774 if (!This->sPath)
1775 return E_OUTOFMEMORY;
1776
1777 lstrcpyW(This->sPath, buffer);
1778
1779 This->bDirty = TRUE;
1780 } else
1781 hr = S_OK; /* don't report any error occured while just caching information */
1782 }
1783
1784 if (!This->sIcoPath && This->sPath) {
1785 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1786 if (!This->sIcoPath)
1787 return E_OUTOFMEMORY;
1788
1789 lstrcpyW(This->sIcoPath, This->sPath);
1790 This->iIcoNdx = 0;
1791
1792 This->bDirty = TRUE;
1793 }
1794
1795 return hr;
1796 }
1797
1798 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1799 {
1800 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1801
1802 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1803
1804 if (This->sPath)
1805 HeapFree(GetProcessHeap(), 0, This->sPath);
1806 This->sPath = HeapAlloc( GetProcessHeap(), 0,
1807 (lstrlenW( pszFile )+1) * sizeof (WCHAR) );
1808 if ( !This->sPath )
1809 return E_OUTOFMEMORY;
1810
1811 lstrcpyW( This->sPath, pszFile );
1812 This->bDirty = TRUE;
1813
1814 return S_OK;
1815 }
1816
1817 /**************************************************************************
1818 * IShellLinkW Implementation
1819 */
1820
1821 static ICOM_VTABLE(IShellLinkW) slvtw =
1822 {
1823 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1824 IShellLinkW_fnQueryInterface,
1825 IShellLinkW_fnAddRef,
1826 IShellLinkW_fnRelease,
1827 IShellLinkW_fnGetPath,
1828 IShellLinkW_fnGetIDList,
1829 IShellLinkW_fnSetIDList,
1830 IShellLinkW_fnGetDescription,
1831 IShellLinkW_fnSetDescription,
1832 IShellLinkW_fnGetWorkingDirectory,
1833 IShellLinkW_fnSetWorkingDirectory,
1834 IShellLinkW_fnGetArguments,
1835 IShellLinkW_fnSetArguments,
1836 IShellLinkW_fnGetHotkey,
1837 IShellLinkW_fnSetHotkey,
1838 IShellLinkW_fnGetShowCmd,
1839 IShellLinkW_fnSetShowCmd,
1840 IShellLinkW_fnGetIconLocation,
1841 IShellLinkW_fnSetIconLocation,
1842 IShellLinkW_fnSetRelativePath,
1843 IShellLinkW_fnResolve,
1844 IShellLinkW_fnSetPath
1845 };