FIXME -> TRACE
[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_UNICODE 0x80
71
72 #include "pshpack1.h"
73
74 typedef struct _LINK_HEADER
75 {
76 DWORD dwSize; /* 0x00 size of the header - 0x4c */
77 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
78 DWORD dwFlags; /* 0x14 describes elements following */
79 DWORD dwFileAttr; /* 0x18 attributes of the target file */
80 FILETIME Time1; /* 0x1c */
81 FILETIME Time2; /* 0x24 */
82 FILETIME Time3; /* 0x2c */
83 DWORD dwFileLength; /* 0x34 File length */
84 DWORD nIcon; /* 0x38 icon number */
85 DWORD fStartup; /* 0x3c startup type */
86 DWORD wHotKey; /* 0x40 hotkey */
87 DWORD Unknown5; /* 0x44 */
88 DWORD Unknown6; /* 0x48 */
89 } LINK_HEADER, * PLINK_HEADER;
90
91 #define SHLINK_LOCAL 0
92 #define SHLINK_REMOTE 1
93
94 typedef struct _LOCATION_INFO
95 {
96 DWORD dwTotalSize;
97 DWORD dwHeaderSize;
98 DWORD dwFlags;
99 DWORD dwVolTableOfs;
100 DWORD dwLocalPathOfs;
101 DWORD dwNetworkVolTableOfs;
102 DWORD dwFinalPathOfs;
103 } LOCATION_INFO;
104
105 typedef struct _LOCAL_VOLUME_INFO
106 {
107 DWORD dwSize;
108 DWORD dwType;
109 DWORD dwVolSerial;
110 DWORD dwVolLabelOfs;
111 } LOCAL_VOLUME_INFO;
112
113 #include "poppack.h"
114
115 static ICOM_VTABLE(IShellLinkA) slvt;
116 static ICOM_VTABLE(IShellLinkW) slvtw;
117 static ICOM_VTABLE(IPersistFile) pfvt;
118 static ICOM_VTABLE(IPersistStream) psvt;
119
120 /* IShellLink Implementation */
121
122 typedef struct
123 {
124 ICOM_VFIELD(IShellLinkA);
125 DWORD ref;
126
127 ICOM_VTABLE(IShellLinkW)* lpvtblw;
128 ICOM_VTABLE(IPersistFile)* lpvtblPersistFile;
129 ICOM_VTABLE(IPersistStream)* lpvtblPersistStream;
130
131 /* data structures according to the informations in the link */
132 LPITEMIDLIST pPidl;
133 WORD wHotKey;
134 SYSTEMTIME time1;
135 SYSTEMTIME time2;
136 SYSTEMTIME time3;
137
138 DWORD iShowCmd;
139 LPWSTR sIcoPath;
140 INT iIcoNdx;
141 LPWSTR sPath;
142 LPWSTR sArgs;
143 LPWSTR sWorkDir;
144 LPWSTR sDescription;
145 LPWSTR sPathRel;
146
147 BOOL bDirty;
148 } IShellLinkImpl;
149
150 #define _IShellLinkW_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblw)))
151 #define _ICOM_THIS_From_IShellLinkW(class, name) class* This = (class*)(((char*)name)-_IShellLinkW_Offset)
152
153 #define _IPersistFile_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistFile)))
154 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset)
155
156 #define _IPersistStream_Offset ((int)(&(((IShellLinkImpl*)0)->lpvtblPersistStream)))
157 #define _ICOM_THIS_From_IPersistStream(class, name) class* This = (class*)(((char*)name)-_IPersistStream_Offset)
158 #define _IPersistStream_From_ICOM_THIS(class, name) class* StreamThis = (class*)(((char*)name)+_IPersistStream_Offset)
159
160 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
161
162 /* strdup on the process heap */
163 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
164 {
165 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
166 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
167 if( !p )
168 return p;
169 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
170 return p;
171 }
172
173
174 /**************************************************************************
175 * IPersistFile_QueryInterface
176 */
177 static HRESULT WINAPI IPersistFile_fnQueryInterface(
178 IPersistFile* iface,
179 REFIID riid,
180 LPVOID *ppvObj)
181 {
182 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
183
184 TRACE("(%p)\n",This);
185
186 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
187 }
188
189 /******************************************************************************
190 * IPersistFile_AddRef
191 */
192 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
193 {
194 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
195
196 TRACE("(%p)->(count=%lu)\n",This,This->ref);
197
198 return IShellLinkA_AddRef((IShellLinkA*)This);
199 }
200 /******************************************************************************
201 * IPersistFile_Release
202 */
203 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
204 {
205 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
206
207 TRACE("(%p)->(count=%lu)\n",This,This->ref);
208
209 return IShellLinkA_Release((IShellLinkA*)This);
210 }
211
212 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
213 {
214 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
215 FIXME("(%p)\n",This);
216 return NOERROR;
217 }
218 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
219 {
220 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
221
222 TRACE("(%p)\n",This);
223
224 if (This->bDirty)
225 return S_OK;
226
227 return S_FALSE;
228 }
229 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
230 {
231 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
232 _IPersistStream_From_ICOM_THIS(IPersistStream, This);
233 HRESULT r;
234 IStream *stm;
235
236 TRACE("(%p, %s)\n",This, debugstr_w(pszFileName));
237
238 r = CreateStreamOnFile(pszFileName, dwMode, &stm);
239 if( SUCCEEDED( r ) )
240 {
241 r = IPersistStream_Load(StreamThis, stm);
242 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
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, MAX_PATH, 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 * ShellLink_UpdatePath
834 * update absolute path in sPath using relative path in sPathRel
835 */
836 static HRESULT 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 hr = SHELL_GetPathFromIDListW(pidl, path, MAX_PATH);
916
917 if (SUCCEEDED(hr)) {
918 hr = IPersistFile_Load(ppf, path, 0);
919
920 if (SUCCEEDED(hr))
921 *ppv = (IUnknown*) psl;
922 }
923
924 IPersistFile_Release(ppf);
925 }
926
927 if (!*ppv)
928 IShellLinkW_Release(psl);
929 }
930
931 return hr;
932 }
933
934 /**************************************************************************
935 * IShellLinkA_QueryInterface
936 */
937 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
938 {
939 ICOM_THIS(IShellLinkImpl, iface);
940
941 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
942
943 *ppvObj = NULL;
944
945 if(IsEqualIID(riid, &IID_IUnknown) ||
946 IsEqualIID(riid, &IID_IShellLinkA))
947 {
948 *ppvObj = This;
949 }
950 else if(IsEqualIID(riid, &IID_IShellLinkW))
951 {
952 *ppvObj = (IShellLinkW *)&(This->lpvtblw);
953 }
954 else if(IsEqualIID(riid, &IID_IPersistFile))
955 {
956 *ppvObj = (IPersistFile *)&(This->lpvtblPersistFile);
957 }
958 else if(IsEqualIID(riid, &IID_IPersistStream))
959 {
960 *ppvObj = (IPersistStream *)&(This->lpvtblPersistStream);
961 }
962
963 if(*ppvObj)
964 {
965 IUnknown_AddRef((IUnknown*)(*ppvObj));
966 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
967 return S_OK;
968 }
969 TRACE("-- Interface: E_NOINTERFACE\n");
970 return E_NOINTERFACE;
971 }
972
973 /******************************************************************************
974 * IShellLinkA_AddRef
975 */
976 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
977 {
978 ICOM_THIS(IShellLinkImpl, iface);
979
980 TRACE("(%p)->(count=%lu)\n",This,This->ref);
981
982 return ++(This->ref);
983 }
984
985 /******************************************************************************
986 * IShellLinkA_Release
987 */
988 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
989 {
990 ICOM_THIS(IShellLinkImpl, iface);
991
992 TRACE("(%p)->(count=%lu)\n",This,This->ref);
993
994 if (--(This->ref))
995 return This->ref;
996
997 TRACE("-- destroying IShellLink(%p)\n",This);
998
999 if (This->sIcoPath)
1000 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1001
1002 if (This->sArgs)
1003 HeapFree(GetProcessHeap(), 0, This->sArgs);
1004
1005 if (This->sWorkDir)
1006 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1007
1008 if (This->sDescription)
1009 HeapFree(GetProcessHeap(), 0, This->sDescription);
1010
1011 if (This->sPath)
1012 HeapFree(GetProcessHeap(), 0, This->sPath);
1013
1014 if (This->pPidl)
1015 ILFree(This->pPidl);
1016
1017 LocalFree((HANDLE)This);
1018
1019 return 0;
1020 }
1021
1022 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1023 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1024 {
1025 ICOM_THIS(IShellLinkImpl, iface);
1026
1027 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1028 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1029
1030 if( cchMaxPath )
1031 pszFile[0] = 0;
1032 if (This->sPath)
1033 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1034 pszFile, cchMaxPath, NULL, NULL);
1035
1036 if (pfd) {
1037
1038 FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1039
1040 }
1041
1042 return NOERROR;
1043 }
1044
1045 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1046 {
1047 ICOM_THIS(IShellLinkImpl, iface);
1048
1049 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1050
1051 *ppidl = ILClone(This->pPidl);
1052
1053 return NOERROR;
1054 }
1055
1056 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1057 {
1058 ICOM_THIS(IShellLinkImpl, iface);
1059
1060 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1061
1062 if (This->pPidl)
1063 ILFree(This->pPidl);
1064
1065 This->pPidl = ILClone (pidl);
1066 This->bDirty = TRUE;
1067
1068 return S_OK;
1069 }
1070
1071 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1072 {
1073 ICOM_THIS(IShellLinkImpl, iface);
1074
1075 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1076
1077 if( cchMaxName )
1078 pszName[0] = 0;
1079 if( This->sDescription )
1080 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1081 pszName, cchMaxName, NULL, NULL);
1082
1083 return S_OK;
1084 }
1085 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1086 {
1087 ICOM_THIS(IShellLinkImpl, iface);
1088
1089 TRACE("(%p)->(pName=%s)\n", This, pszName);
1090
1091 if (This->sDescription)
1092 HeapFree(GetProcessHeap(), 0, This->sDescription);
1093 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1094 if ( !This->sDescription )
1095 return E_OUTOFMEMORY;
1096
1097 This->bDirty = TRUE;
1098
1099 return S_OK;
1100 }
1101
1102 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1103 {
1104 ICOM_THIS(IShellLinkImpl, iface);
1105
1106 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1107
1108 if( cchMaxPath )
1109 pszDir[0] = 0;
1110 if( This->sWorkDir )
1111 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1112 pszDir, cchMaxPath, NULL, NULL);
1113
1114 return S_OK;
1115 }
1116
1117 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1118 {
1119 ICOM_THIS(IShellLinkImpl, iface);
1120
1121 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1122
1123 if (This->sWorkDir)
1124 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1125 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1126 if ( !This->sWorkDir )
1127 return E_OUTOFMEMORY;
1128
1129 This->bDirty = TRUE;
1130
1131 return S_OK;
1132 }
1133
1134 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1135 {
1136 ICOM_THIS(IShellLinkImpl, iface);
1137
1138 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1139
1140 if( cchMaxPath )
1141 pszArgs[0] = 0;
1142 if( This->sArgs )
1143 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1144 pszArgs, cchMaxPath, NULL, NULL);
1145
1146 return S_OK;
1147 }
1148
1149 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1150 {
1151 ICOM_THIS(IShellLinkImpl, iface);
1152
1153 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1154
1155 if (This->sArgs)
1156 HeapFree(GetProcessHeap(), 0, This->sArgs);
1157 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1158 if( !This->sArgs )
1159 return E_OUTOFMEMORY;
1160
1161 This->bDirty = TRUE;
1162
1163 return S_OK;
1164 }
1165
1166 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1167 {
1168 ICOM_THIS(IShellLinkImpl, iface);
1169
1170 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1171
1172 *pwHotkey = This->wHotKey;
1173
1174 return S_OK;
1175 }
1176
1177 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1178 {
1179 ICOM_THIS(IShellLinkImpl, iface);
1180
1181 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1182
1183 This->wHotKey = wHotkey;
1184 This->bDirty = TRUE;
1185
1186 return S_OK;
1187 }
1188
1189 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1190 {
1191 ICOM_THIS(IShellLinkImpl, iface);
1192
1193 TRACE("(%p)->(%p)\n",This, piShowCmd);
1194 *piShowCmd = This->iShowCmd;
1195 return S_OK;
1196 }
1197
1198 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1199 {
1200 ICOM_THIS(IShellLinkImpl, iface);
1201
1202 TRACE("(%p) %d\n",This, iShowCmd);
1203
1204 This->iShowCmd = iShowCmd;
1205 This->bDirty = TRUE;
1206
1207 return NOERROR;
1208 }
1209
1210 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1211 {
1212 LPCITEMIDLIST pidlLast;
1213
1214 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1215
1216 if (SUCCEEDED(hr)) {
1217 IExtractIconA* pei;
1218
1219 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1220
1221 if (SUCCEEDED(hr)) {
1222 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1223
1224 IExtractIconA_Release(pei);
1225 }
1226
1227 IShellFolder_Release(psf);
1228 }
1229
1230 return hr;
1231 }
1232
1233 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath, INT cchIconPath, INT *piIcon)
1234 {
1235 ICOM_THIS(IShellLinkImpl, iface);
1236
1237 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1238
1239 if (cchIconPath)
1240 pszIconPath[0] = 0;
1241
1242 if (This->sIcoPath) {
1243 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1244 *piIcon = This->iIcoNdx;
1245 return S_OK;
1246 }
1247
1248 if (This->pPidl || This->sPath) {
1249 IShellFolder* pdsk;
1250
1251 HRESULT hr = SHGetDesktopFolder(&pdsk);
1252
1253 if (SUCCEEDED(hr)) {
1254 /* first look for an icon using the PIDL (if present) */
1255 if (This->pPidl)
1256 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1257 else
1258 hr = E_FAIL;
1259
1260 /* if we couldn't find an icon yet, look for it using the file system path */
1261 if (FAILED(hr) && This->sPath) {
1262 LPITEMIDLIST pidl;
1263
1264 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1265
1266 if (SUCCEEDED(hr)) {
1267 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1268
1269 SHFree(pidl);
1270 }
1271 }
1272
1273 IShellFolder_Release(pdsk);
1274 }
1275
1276 return hr;
1277 } else
1278 return E_FAIL;
1279 }
1280
1281 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath, INT iIcon)
1282 {
1283 ICOM_THIS(IShellLinkImpl, iface);
1284
1285 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1286
1287 if (This->sIcoPath)
1288 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1289 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1290 if ( !This->sIcoPath )
1291 return E_OUTOFMEMORY;
1292
1293 This->iIcoNdx = iIcon;
1294 This->bDirty = TRUE;
1295
1296 return S_OK;
1297 }
1298
1299 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1300 {
1301 ICOM_THIS(IShellLinkImpl, iface);
1302
1303 FIXME("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1304
1305 if (This->sPathRel)
1306 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1307
1308 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1309 This->bDirty = TRUE;
1310
1311 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1312 }
1313
1314 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1315 {
1316 HRESULT hr = S_OK;
1317
1318 ICOM_THIS(IShellLinkImpl, iface);
1319
1320 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1321
1322 /*FIXME: use IResolveShellLink interface */
1323
1324 if (!This->sPath && This->pPidl) {
1325 WCHAR buffer[MAX_PATH];
1326
1327 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1328
1329 if (SUCCEEDED(hr) && *buffer) {
1330 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1331 if (!This->sPath)
1332 return E_OUTOFMEMORY;
1333
1334 lstrcpyW(This->sPath, buffer);
1335
1336 This->bDirty = TRUE;
1337 } else
1338 hr = S_OK; /* don't report any error occured while just caching information */
1339 }
1340
1341 if (!This->sIcoPath && This->sPath) {
1342 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1343 if (!This->sIcoPath)
1344 return E_OUTOFMEMORY;
1345
1346 lstrcpyW(This->sIcoPath, This->sPath);
1347 This->iIcoNdx = 0;
1348
1349 This->bDirty = TRUE;
1350 }
1351
1352 return hr;
1353 }
1354
1355 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1356 {
1357 ICOM_THIS(IShellLinkImpl, iface);
1358 char buffer[MAX_PATH];
1359 LPSTR fname;
1360
1361 TRACE("(%p)->(path=%s)\n",This, pszFile);
1362
1363 if (!GetFullPathNameA(pszFile, MAX_PATH, buffer, &fname))
1364 return E_FAIL;
1365
1366 if (This->sPath)
1367 HeapFree(GetProcessHeap(), 0, This->sPath);
1368
1369 This->sPath = HEAP_strdupAtoW(GetProcessHeap(), 0, buffer);
1370 if( !This->sPath )
1371 return E_OUTOFMEMORY;
1372
1373 This->bDirty = TRUE;
1374
1375 return S_OK;
1376 }
1377
1378 /**************************************************************************
1379 * IShellLink Implementation
1380 */
1381
1382 static ICOM_VTABLE(IShellLinkA) slvt =
1383 {
1384 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1385 IShellLinkA_fnQueryInterface,
1386 IShellLinkA_fnAddRef,
1387 IShellLinkA_fnRelease,
1388 IShellLinkA_fnGetPath,
1389 IShellLinkA_fnGetIDList,
1390 IShellLinkA_fnSetIDList,
1391 IShellLinkA_fnGetDescription,
1392 IShellLinkA_fnSetDescription,
1393 IShellLinkA_fnGetWorkingDirectory,
1394 IShellLinkA_fnSetWorkingDirectory,
1395 IShellLinkA_fnGetArguments,
1396 IShellLinkA_fnSetArguments,
1397 IShellLinkA_fnGetHotkey,
1398 IShellLinkA_fnSetHotkey,
1399 IShellLinkA_fnGetShowCmd,
1400 IShellLinkA_fnSetShowCmd,
1401 IShellLinkA_fnGetIconLocation,
1402 IShellLinkA_fnSetIconLocation,
1403 IShellLinkA_fnSetRelativePath,
1404 IShellLinkA_fnResolve,
1405 IShellLinkA_fnSetPath
1406 };
1407
1408
1409 /**************************************************************************
1410 * IShellLinkW_fnQueryInterface
1411 */
1412 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1413 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1414 {
1415 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1416
1417 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1418 }
1419
1420 /******************************************************************************
1421 * IShellLinkW_fnAddRef
1422 */
1423 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1424 {
1425 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1426
1427 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1428
1429 return IShellLinkA_AddRef((IShellLinkA*)This);
1430 }
1431 /******************************************************************************
1432 * IShellLinkW_fnRelease
1433 */
1434
1435 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1436 {
1437 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1438
1439 TRACE("(%p)->(count=%lu)\n",This,This->ref);
1440
1441 return IShellLinkA_Release((IShellLinkA*)This);
1442 }
1443
1444 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1445 {
1446 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1447
1448 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)\n",
1449 This, pszFile, cchMaxPath, pfd, fFlags);
1450
1451 if( cchMaxPath )
1452 pszFile[0] = 0;
1453 if( This->sPath )
1454 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1455
1456 if (pfd) {
1457
1458 FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1459
1460 }
1461
1462 return NOERROR;
1463 }
1464
1465 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1466 {
1467 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1468
1469 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1470
1471 if( This->pPidl)
1472 *ppidl = ILClone( This->pPidl );
1473 else
1474 *ppidl = NULL;
1475
1476 return S_OK;
1477 }
1478
1479 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1480 {
1481 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1482
1483 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1484
1485 if( This->pPidl )
1486 ILFree( This->pPidl );
1487 This->pPidl = ILClone( pidl );
1488 if( !This->pPidl )
1489 return E_FAIL;
1490
1491 This->bDirty = TRUE;
1492
1493 return S_OK;
1494 }
1495
1496 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1497 {
1498 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1499
1500 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1501
1502 if( cchMaxName )
1503 pszName[0] = 0;
1504 if( This->sDescription )
1505 lstrcpynW( pszName, This->sDescription, cchMaxName );
1506
1507 return S_OK;
1508 }
1509
1510 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1511 {
1512 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1513
1514 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1515
1516 if (This->sDescription)
1517 HeapFree(GetProcessHeap(), 0, This->sDescription);
1518 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1519 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1520 if ( !This->sDescription )
1521 return E_OUTOFMEMORY;
1522
1523 lstrcpyW( This->sDescription, pszName );
1524 This->bDirty = TRUE;
1525
1526 return S_OK;
1527 }
1528
1529 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1530 {
1531 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1532
1533 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1534
1535 if( cchMaxPath )
1536 pszDir[0] = 0;
1537 if( This->sWorkDir )
1538 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1539
1540 return S_OK;
1541 }
1542
1543 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1544 {
1545 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1546
1547 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1548
1549 if (This->sWorkDir)
1550 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1551 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1552 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1553 if ( !This->sWorkDir )
1554 return E_OUTOFMEMORY;
1555
1556 lstrcpyW( This->sWorkDir, pszDir );
1557 This->bDirty = TRUE;
1558
1559 return S_OK;
1560 }
1561
1562 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1563 {
1564 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1565
1566 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1567
1568 if( cchMaxPath )
1569 pszArgs[0] = 0;
1570 if( This->sArgs )
1571 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1572
1573 return NOERROR;
1574 }
1575
1576 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1577 {
1578 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1579
1580 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1581
1582 if (This->sArgs)
1583 HeapFree(GetProcessHeap(), 0, This->sArgs);
1584 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1585 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1586 if ( !This->sArgs )
1587 return E_OUTOFMEMORY;
1588
1589 lstrcpyW( This->sArgs, pszArgs );
1590 This->bDirty = TRUE;
1591
1592 return S_OK;
1593 }
1594
1595 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1596 {
1597 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1598
1599 TRACE("(%p)->(%p)\n",This, pwHotkey);
1600
1601 *pwHotkey=This->wHotKey;
1602
1603 return S_OK;
1604 }
1605
1606 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1607 {
1608 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1609
1610 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1611
1612 This->wHotKey = wHotkey;
1613 This->bDirty = TRUE;
1614
1615 return S_OK;
1616 }
1617
1618 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1619 {
1620 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1621
1622 TRACE("(%p)->(%p)\n",This, piShowCmd);
1623
1624 *piShowCmd = This->iShowCmd;
1625
1626 return S_OK;
1627 }
1628
1629 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1630 {
1631 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1632
1633 This->iShowCmd = iShowCmd;
1634 This->bDirty = TRUE;
1635
1636 return S_OK;
1637 }
1638
1639 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1640 {
1641 LPCITEMIDLIST pidlLast;
1642
1643 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1644
1645 if (SUCCEEDED(hr)) {
1646 IExtractIconW* pei;
1647
1648 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1649
1650 if (SUCCEEDED(hr)) {
1651 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1652
1653 IExtractIconW_Release(pei);
1654 }
1655
1656 IShellFolder_Release(psf);
1657 }
1658
1659 return hr;
1660 }
1661
1662 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath, INT cchIconPath, INT *piIcon)
1663 {
1664 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1665
1666 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1667
1668 if (cchIconPath)
1669 pszIconPath[0] = 0;
1670
1671 if (This->sIcoPath) {
1672 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1673 *piIcon = This->iIcoNdx;
1674 return S_OK;
1675 }
1676
1677 if (This->pPidl || This->sPath) {
1678 IShellFolder* pdsk;
1679
1680 HRESULT hr = SHGetDesktopFolder(&pdsk);
1681
1682 if (SUCCEEDED(hr)) {
1683 /* first look for an icon using the PIDL (if present) */
1684 if (This->pPidl)
1685 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1686 else
1687 hr = E_FAIL;
1688
1689 /* if we couldn't find an icon yet, look for it using the file system path */
1690 if (FAILED(hr) && This->sPath) {
1691 LPITEMIDLIST pidl;
1692
1693 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1694
1695 if (SUCCEEDED(hr)) {
1696 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1697
1698 SHFree(pidl);
1699 }
1700 }
1701
1702 IShellFolder_Release(pdsk);
1703 }
1704
1705 return hr;
1706 } else
1707 return E_FAIL;
1708 }
1709
1710 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath, INT iIcon)
1711 {
1712 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1713
1714 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1715
1716 if (This->sIcoPath)
1717 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1718 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1719 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1720 if ( !This->sIcoPath )
1721 return E_OUTOFMEMORY;
1722 lstrcpyW( This->sIcoPath, pszIconPath );
1723
1724 This->iIcoNdx = iIcon;
1725 This->bDirty = TRUE;
1726
1727 return S_OK;
1728 }
1729
1730 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1731 {
1732 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1733
1734 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1735
1736 if (This->sPathRel)
1737 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1738 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1739 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1740 if ( !This->sPathRel )
1741 return E_OUTOFMEMORY;
1742
1743 lstrcpyW( This->sPathRel, pszPathRel );
1744 This->bDirty = TRUE;
1745
1746 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1747 }
1748
1749 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1750 {
1751 HRESULT hr = S_OK;
1752
1753 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1754
1755 FIXME("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1756
1757 /*FIXME: use IResolveShellLink interface */
1758
1759 if (!This->sPath && This->pPidl) {
1760 WCHAR buffer[MAX_PATH];
1761
1762 hr = SHELL_GetPathFromIDListW(This->pPidl, buffer, MAX_PATH);
1763
1764 if (SUCCEEDED(hr) && *buffer) {
1765 This->sPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1766 if (!This->sPath)
1767 return E_OUTOFMEMORY;
1768
1769 lstrcpyW(This->sPath, buffer);
1770
1771 This->bDirty = TRUE;
1772 } else
1773 hr = S_OK; /* don't report any error occured while just caching information */
1774 }
1775
1776 if (!This->sIcoPath && This->sPath) {
1777 This->sIcoPath = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
1778 if (!This->sIcoPath)
1779 return E_OUTOFMEMORY;
1780
1781 lstrcpyW(This->sIcoPath, This->sPath);
1782 This->iIcoNdx = 0;
1783
1784 This->bDirty = TRUE;
1785 }
1786
1787 return hr;
1788 }
1789
1790 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
1791 {
1792 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1793 WCHAR buffer[MAX_PATH];
1794 LPWSTR fname;
1795
1796 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
1797
1798 if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
1799 return E_FAIL;
1800
1801 if (This->sPath)
1802 HeapFree(GetProcessHeap(), 0, This->sPath);
1803
1804 This->sPath = HeapAlloc( GetProcessHeap(), 0,
1805 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
1806 if ( !This->sPath )
1807 return E_OUTOFMEMORY;
1808
1809 lstrcpyW( This->sPath, buffer );
1810 This->bDirty = TRUE;
1811
1812 return S_OK;
1813 }
1814
1815 /**************************************************************************
1816 * IShellLinkW Implementation
1817 */
1818
1819 static ICOM_VTABLE(IShellLinkW) slvtw =
1820 {
1821 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1822 IShellLinkW_fnQueryInterface,
1823 IShellLinkW_fnAddRef,
1824 IShellLinkW_fnRelease,
1825 IShellLinkW_fnGetPath,
1826 IShellLinkW_fnGetIDList,
1827 IShellLinkW_fnSetIDList,
1828 IShellLinkW_fnGetDescription,
1829 IShellLinkW_fnSetDescription,
1830 IShellLinkW_fnGetWorkingDirectory,
1831 IShellLinkW_fnSetWorkingDirectory,
1832 IShellLinkW_fnGetArguments,
1833 IShellLinkW_fnSetArguments,
1834 IShellLinkW_fnGetHotkey,
1835 IShellLinkW_fnSetHotkey,
1836 IShellLinkW_fnGetShowCmd,
1837 IShellLinkW_fnSetShowCmd,
1838 IShellLinkW_fnGetIconLocation,
1839 IShellLinkW_fnSetIconLocation,
1840 IShellLinkW_fnSetRelativePath,
1841 IShellLinkW_fnResolve,
1842 IShellLinkW_fnSetPath
1843 };