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