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