Switch IShellLink to use shlwapi.SHCreateStreamOnFileW.
[wine/multimedia.git] / dlls / shell32 / shelllink.c
blobe983884f5a9f4b4db0a5a46d02fd6ace21be24a3
1 /*
3 * Copyright 1997 Marcus Meissner
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2005 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTES
22 * Nearly complete informations about the binary formats
23 * of .lnk files available at http://www.wotsit.org
25 * You can use winedump to examine the contents of a link file:
26 * winedump lnk sc.lnk
28 * MSI advertised shortcuts are totally undocumented. They provide an
29 * icon for a program that is not yet installed, and invoke MSI to
30 * install the program when the shortcut is clicked on. They are
31 * created by passing a special string to SetPath, and the information
32 * in that string is parsed an stored.
35 #define COBJMACROS
36 #define NONAMELESSUNION
38 #include "wine/debug.h"
39 #include "winerror.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winreg.h"
45 #include "winuser.h"
46 #include "wingdi.h"
47 #include "shlobj.h"
48 #include "undocshell.h"
50 #include "pidl.h"
51 #include "shell32_main.h"
52 #include "shlguid.h"
53 #include "shlwapi.h"
55 #include "initguid.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(shell);
59 DEFINE_GUID( SHELL32_AdvtShortcutProduct,
60 0x9db1186f,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
61 DEFINE_GUID( SHELL32_AdvtShortcutComponent,
62 0x9db1186e,0x40df,0x11d1,0xaa,0x8c,0x00,0xc0,0x4f,0xb6,0x78,0x63);
64 /* link file formats */
66 #include "pshpack1.h"
68 typedef struct _LINK_HEADER
70 DWORD dwSize; /* 0x00 size of the header - 0x4c */
71 GUID MagicGuid; /* 0x04 is CLSID_ShellLink */
72 DWORD dwFlags; /* 0x14 describes elements following */
73 DWORD dwFileAttr; /* 0x18 attributes of the target file */
74 FILETIME Time1; /* 0x1c */
75 FILETIME Time2; /* 0x24 */
76 FILETIME Time3; /* 0x2c */
77 DWORD dwFileLength; /* 0x34 File length */
78 DWORD nIcon; /* 0x38 icon number */
79 DWORD fStartup; /* 0x3c startup type */
80 DWORD wHotKey; /* 0x40 hotkey */
81 DWORD Unknown5; /* 0x44 */
82 DWORD Unknown6; /* 0x48 */
83 } LINK_HEADER, * PLINK_HEADER;
85 #define SHLINK_LOCAL 0
86 #define SHLINK_REMOTE 1
88 typedef struct _LOCATION_INFO
90 DWORD dwTotalSize;
91 DWORD dwHeaderSize;
92 DWORD dwFlags;
93 DWORD dwVolTableOfs;
94 DWORD dwLocalPathOfs;
95 DWORD dwNetworkVolTableOfs;
96 DWORD dwFinalPathOfs;
97 } LOCATION_INFO;
99 typedef struct _LOCAL_VOLUME_INFO
101 DWORD dwSize;
102 DWORD dwType;
103 DWORD dwVolSerial;
104 DWORD dwVolLabelOfs;
105 } LOCAL_VOLUME_INFO;
107 typedef struct volume_info_t
109 DWORD type;
110 DWORD serial;
111 WCHAR label[12]; /* assume 8.3 */
112 } volume_info;
114 #include "poppack.h"
116 static const IShellLinkAVtbl slvt;
117 static const IShellLinkWVtbl slvtw;
118 static const IPersistFileVtbl pfvt;
119 static const IPersistStreamVtbl psvt;
120 static const IShellLinkDataListVtbl dlvt;
121 static const IShellExtInitVtbl eivt;
122 static const IContextMenuVtbl cmvt;
124 /* IShellLink Implementation */
126 typedef struct
128 const IShellLinkAVtbl *lpVtbl;
129 const IShellLinkWVtbl *lpvtblw;
130 const IPersistFileVtbl *lpvtblPersistFile;
131 const IPersistStreamVtbl *lpvtblPersistStream;
132 const IShellLinkDataListVtbl *lpvtblShellLinkDataList;
133 const IShellExtInitVtbl *lpvtblShellExtInit;
134 const IContextMenuVtbl *lpvtblContextMenu;
136 DWORD ref;
138 /* data structures according to the informations in the link */
139 LPITEMIDLIST pPidl;
140 WORD wHotKey;
141 SYSTEMTIME time1;
142 SYSTEMTIME time2;
143 SYSTEMTIME time3;
145 DWORD iShowCmd;
146 LPWSTR sIcoPath;
147 INT iIcoNdx;
148 LPWSTR sPath;
149 LPWSTR sArgs;
150 LPWSTR sWorkDir;
151 LPWSTR sDescription;
152 LPWSTR sPathRel;
153 LPWSTR sProduct;
154 LPWSTR sComponent;
155 volume_info volume;
157 BOOL bDirty;
158 } IShellLinkImpl;
160 static inline IShellLinkImpl *impl_from_IShellLinkW( IShellLinkW *iface )
162 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblw));
164 #define _ICOM_THIS_From_IShellLinkW(class, iface) \
165 class* This = impl_from_IShellLinkW( iface )
167 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
169 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
171 #define _ICOM_THIS_From_IPersistFile(class, iface) \
172 class* This = impl_from_IPersistFile( iface )
174 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
176 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
178 #define _ICOM_THIS_From_IPersistStream(class, iface) \
179 class* This = impl_from_IPersistStream( iface )
181 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
183 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
185 #define _ICOM_THIS_From_IShellLinkDataList(class, iface) \
186 class* This = impl_from_IShellLinkDataList( iface )
188 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
190 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
192 #define _ICOM_THIS_From_IShellExtInit(class, iface) \
193 class* This = impl_from_IShellExtInit( iface )
195 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
197 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
199 #define _ICOM_THIS_From_IContextMenu(class, iface) \
200 class* This = impl_from_IContextMenu( iface )
202 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
204 /* strdup on the process heap */
205 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
207 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
208 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
209 if( !p )
210 return p;
211 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
212 return p;
215 /**************************************************************************
216 * IPersistFile_QueryInterface
218 static HRESULT WINAPI IPersistFile_fnQueryInterface(
219 IPersistFile* iface,
220 REFIID riid,
221 LPVOID *ppvObj)
223 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
224 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
227 /******************************************************************************
228 * IPersistFile_AddRef
230 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
232 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
233 return IShellLinkA_AddRef((IShellLinkA*)This);
236 /******************************************************************************
237 * IPersistFile_Release
239 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
241 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
242 return IShellLinkA_Release((IShellLinkA*)This);
245 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
247 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
248 FIXME("(%p)\n",This);
249 return NOERROR;
252 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
254 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
256 TRACE("(%p)\n",This);
258 if (This->bDirty)
259 return S_OK;
261 return S_FALSE;
264 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
266 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
267 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
268 HRESULT r;
269 IStream *stm;
271 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
273 if( dwMode == 0 )
274 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
275 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
276 if( SUCCEEDED( r ) )
278 r = IPersistStream_Load(StreamThis, stm);
279 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
280 IStream_Release( stm );
281 This->bDirty = FALSE;
283 TRACE("-- returning hr %08lx\n", r);
284 return r;
287 static BOOL StartLinkProcessor( LPCOLESTR szLink )
289 static const WCHAR szFormat[] = {
290 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
291 ' ','-','r',' ','"','%','s','"',0 };
292 LONG len;
293 LPWSTR buffer;
294 STARTUPINFOW si;
295 PROCESS_INFORMATION pi;
297 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
298 buffer = HeapAlloc( GetProcessHeap(), 0, len );
299 if( !buffer )
300 return FALSE;
302 wsprintfW( buffer, szFormat, szLink );
304 TRACE("starting %s\n",debugstr_w(buffer));
306 memset(&si, 0, sizeof(si));
307 si.cb = sizeof(si);
308 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
310 /* wait for a while to throttle the creation of linker processes */
311 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
312 WARN("Timed out waiting for shell linker\n");
314 CloseHandle( pi.hProcess );
315 CloseHandle( pi.hThread );
317 return TRUE;
320 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
322 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
323 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
324 HRESULT r;
325 IStream *stm;
327 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
329 if (!pszFileName)
330 return E_FAIL;
332 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
333 if( SUCCEEDED( r ) )
335 r = IPersistStream_Save(StreamThis, stm, FALSE);
336 IStream_Release( stm );
338 if( SUCCEEDED( r ) )
340 StartLinkProcessor( pszFileName );
342 This->bDirty = FALSE;
344 else
346 DeleteFileW( pszFileName );
347 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
351 return r;
354 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
356 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
357 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
358 return NOERROR;
361 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
363 _ICOM_THIS_From_IPersistFile(IShellLinkImpl, iface);
364 FIXME("(%p)\n",This);
365 return NOERROR;
368 static const IPersistFileVtbl pfvt =
370 IPersistFile_fnQueryInterface,
371 IPersistFile_fnAddRef,
372 IPersistFile_fnRelease,
373 IPersistFile_fnGetClassID,
374 IPersistFile_fnIsDirty,
375 IPersistFile_fnLoad,
376 IPersistFile_fnSave,
377 IPersistFile_fnSaveCompleted,
378 IPersistFile_fnGetCurFile
381 /************************************************************************
382 * IPersistStream_QueryInterface
384 static HRESULT WINAPI IPersistStream_fnQueryInterface(
385 IPersistStream* iface,
386 REFIID riid,
387 VOID** ppvoid)
389 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
390 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvoid);
393 /************************************************************************
394 * IPersistStream_Release
396 static ULONG WINAPI IPersistStream_fnRelease(
397 IPersistStream* iface)
399 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
400 return IShellLinkA_Release((IShellLinkA*)This);
403 /************************************************************************
404 * IPersistStream_AddRef
406 static ULONG WINAPI IPersistStream_fnAddRef(
407 IPersistStream* iface)
409 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
410 return IShellLinkA_AddRef((IShellLinkA*)This);
413 /************************************************************************
414 * IPersistStream_GetClassID
417 static HRESULT WINAPI IPersistStream_fnGetClassID(
418 IPersistStream* iface,
419 CLSID* pClassID)
421 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
423 TRACE("(%p)\n", This);
425 if (pClassID==0)
426 return E_POINTER;
428 /* memcpy(pClassID, &CLSID_???, sizeof(CLSID_???)); */
430 return S_OK;
433 /************************************************************************
434 * IPersistStream_IsDirty (IPersistStream)
436 static HRESULT WINAPI IPersistStream_fnIsDirty(
437 IPersistStream* iface)
439 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
441 TRACE("(%p)\n", This);
443 return S_OK;
447 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
449 DWORD count;
450 USHORT len;
451 LPVOID temp;
452 LPWSTR str;
453 HRESULT r;
455 TRACE("%p\n", stm);
457 count = 0;
458 r = IStream_Read(stm, &len, sizeof(len), &count);
459 if ( FAILED (r) || ( count != sizeof(len) ) )
460 return E_FAIL;
462 if( unicode )
463 len *= sizeof (WCHAR);
465 TRACE("reading %d\n", len);
466 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
467 if( !temp )
468 return E_OUTOFMEMORY;
469 count = 0;
470 r = IStream_Read(stm, temp, len, &count);
471 if( FAILED (r) || ( count != len ) )
473 HeapFree( GetProcessHeap(), 0, temp );
474 return E_FAIL;
477 TRACE("read %s\n", debugstr_an(temp,len));
479 /* convert to unicode if necessary */
480 if( !unicode )
482 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
483 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
484 if( str )
485 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
486 HeapFree( GetProcessHeap(), 0, temp );
488 else
490 count /= 2;
491 str = (LPWSTR) temp;
493 str[count] = 0;
495 *pstr = str;
497 return S_OK;
500 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
502 DWORD size;
503 ULONG count;
504 HRESULT r;
505 struct sized_chunk {
506 DWORD size;
507 unsigned char data[1];
508 } *chunk;
510 TRACE("%p\n",stm);
512 r = IStream_Read( stm, &size, sizeof(size), &count );
513 if( FAILED( r ) || count != sizeof(size) )
514 return E_FAIL;
516 chunk = HeapAlloc( GetProcessHeap(), 0, size );
517 if( !chunk )
518 return E_OUTOFMEMORY;
520 chunk->size = size;
521 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
522 if( FAILED( r ) || count != (size - sizeof(size)) )
524 HeapFree( GetProcessHeap(), 0, chunk );
525 return E_FAIL;
528 TRACE("Read %ld bytes\n",chunk->size);
530 *data = (LPVOID) chunk;
532 return S_OK;
535 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
537 const int label_sz = sizeof volume->label/sizeof volume->label[0];
538 LPSTR label;
539 int len;
541 volume->serial = vol->dwVolSerial;
542 volume->type = vol->dwType;
544 if( !vol->dwVolLabelOfs )
545 return FALSE;
546 if( vol->dwSize <= vol->dwVolLabelOfs )
547 return FALSE;
548 len = vol->dwSize - vol->dwVolLabelOfs;
550 label = (LPSTR) vol;
551 label += vol->dwVolLabelOfs;
552 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
554 return TRUE;
557 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
559 int len = 0, wlen;
560 LPWSTR path;
562 while( p[len] && (len < maxlen) )
563 len++;
565 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
566 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
567 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
568 path[wlen] = 0;
570 return path;
573 static HRESULT Stream_LoadLocation( IStream *stm,
574 volume_info *volume, LPWSTR *path )
576 unsigned char *p = NULL;
577 LOCATION_INFO *loc;
578 HRESULT r;
579 int n;
581 r = Stream_ReadChunk( stm, (LPVOID*) &p );
582 if( FAILED(r) )
583 return r;
585 loc = (LOCATION_INFO*) p;
586 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
588 HeapFree( GetProcessHeap(), 0, p );
589 return E_FAIL;
592 /* if there's valid local volume information, load it */
593 if( loc->dwVolTableOfs &&
594 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
596 LOCAL_VOLUME_INFO *volume_info;
598 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
599 Stream_LoadVolume( volume_info, volume );
602 /* if there's a local path, load it */
603 n = loc->dwLocalPathOfs;
604 if( n && (n < loc->dwTotalSize) )
605 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
607 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
608 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
610 HeapFree( GetProcessHeap(), 0, p );
611 return S_OK;
615 * The format of the advertised shortcut info seems to be:
617 * Offset Description
618 * ------ -----------
620 * 0 Length of the block (4 bytes, usually 0x314)
621 * 4 tag (dword)
622 * 8 string data in ASCII
623 * 8+0x104 string data in UNICODE
625 * In the original Win32 implementation the buffers are not initialized
626 * to zero, so data trailing the string is random garbage.
628 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
630 DWORD size;
631 ULONG count;
632 HRESULT r;
633 EXP_DARWIN_LINK buffer;
635 TRACE("%p\n",stm);
637 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
638 if( FAILED( r ) )
639 return r;
641 /* make sure that we read the size of the structure even on error */
642 size = sizeof buffer - sizeof (DWORD);
643 if( buffer.dbh.cbSize != sizeof buffer )
645 ERR("Ooops. This structure is not as expected...\n");
646 return E_FAIL;
649 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
650 if( FAILED( r ) )
651 return r;
653 if( count != size )
654 return E_FAIL;
656 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
658 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
660 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
661 return E_FAIL;
664 *str = HeapAlloc( GetProcessHeap(), 0,
665 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
666 lstrcpyW( *str, buffer.szwDarwinID );
668 return S_OK;
671 /************************************************************************
672 * IPersistStream_Load (IPersistStream)
674 static HRESULT WINAPI IPersistStream_fnLoad(
675 IPersistStream* iface,
676 IStream* stm)
678 LINK_HEADER hdr;
679 ULONG dwBytesRead;
680 BOOL unicode;
681 HRESULT r;
682 DWORD zero;
684 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
686 TRACE("%p %p\n", This, stm);
688 if( !stm )
689 return STG_E_INVALIDPOINTER;
691 dwBytesRead = 0;
692 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
693 if( FAILED( r ) )
694 return r;
696 if( dwBytesRead != sizeof(hdr))
697 return E_FAIL;
698 if( hdr.dwSize != sizeof(hdr))
699 return E_FAIL;
700 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
701 return E_FAIL;
703 /* free all the old stuff */
704 ILFree(This->pPidl);
705 This->pPidl = NULL;
706 memset( &This->volume, 0, sizeof This->volume );
707 HeapFree(GetProcessHeap(), 0, This->sPath);
708 This->sPath = NULL;
709 HeapFree(GetProcessHeap(), 0, This->sDescription);
710 This->sDescription = NULL;
711 HeapFree(GetProcessHeap(), 0, This->sPathRel);
712 This->sPathRel = NULL;
713 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
714 This->sWorkDir = NULL;
715 HeapFree(GetProcessHeap(), 0, This->sArgs);
716 This->sArgs = NULL;
717 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
718 This->sIcoPath = NULL;
719 HeapFree(GetProcessHeap(), 0, This->sProduct);
720 This->sProduct = NULL;
721 HeapFree(GetProcessHeap(), 0, This->sComponent);
722 This->sComponent = NULL;
724 This->wHotKey = (WORD)hdr.wHotKey;
725 This->iIcoNdx = hdr.nIcon;
726 FileTimeToSystemTime (&hdr.Time1, &This->time1);
727 FileTimeToSystemTime (&hdr.Time2, &This->time2);
728 FileTimeToSystemTime (&hdr.Time3, &This->time3);
729 if (TRACE_ON(shell))
731 WCHAR sTemp[MAX_PATH];
732 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
733 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
734 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
735 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
736 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
737 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
738 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
739 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
740 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
743 /* load all the new stuff */
744 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
746 r = ILLoadFromStream( stm, &This->pPidl );
747 if( FAILED( r ) )
748 return r;
750 pdump(This->pPidl);
752 /* load the location information */
753 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
754 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
755 if( FAILED( r ) )
756 goto end;
758 unicode = hdr.dwFlags & SLDF_UNICODE;
759 if( hdr.dwFlags & SLDF_HAS_NAME )
761 r = Stream_LoadString( stm, unicode, &This->sDescription );
762 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
764 if( FAILED( r ) )
765 goto end;
767 if( hdr.dwFlags & SLDF_HAS_RELPATH )
769 r = Stream_LoadString( stm, unicode, &This->sPathRel );
770 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
772 if( FAILED( r ) )
773 goto end;
775 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
777 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
778 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
780 if( FAILED( r ) )
781 goto end;
783 if( hdr.dwFlags & SLDF_HAS_ARGS )
785 r = Stream_LoadString( stm, unicode, &This->sArgs );
786 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
788 if( FAILED( r ) )
789 goto end;
791 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
793 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
794 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
796 if( FAILED( r ) )
797 goto end;
799 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
801 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
802 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
804 if( FAILED( r ) )
805 goto end;
807 if( hdr.dwFlags & SLDF_HAS_DARWINID )
809 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
810 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
812 if( FAILED( r ) )
813 goto end;
815 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
816 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
817 ERR("Last word was not zero\n");
819 TRACE("OK\n");
821 pdump (This->pPidl);
823 return S_OK;
824 end:
825 return r;
828 /************************************************************************
829 * Stream_WriteString
831 * Helper function for IPersistStream_Save. Writes a unicode string
832 * with terminating nul byte to a stream, preceded by the its length.
834 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
836 USHORT len = lstrlenW( str ) + 1;
837 DWORD count;
838 HRESULT r;
840 r = IStream_Write( stm, &len, sizeof(len), &count );
841 if( FAILED( r ) )
842 return r;
844 len *= sizeof(WCHAR);
846 r = IStream_Write( stm, str, len, &count );
847 if( FAILED( r ) )
848 return r;
850 return S_OK;
853 /************************************************************************
854 * Stream_WriteLocationInfo
856 * Writes the location info to a stream
858 * FIXME: One day we might want to write the network volume information
859 * and the final path.
860 * Figure out how Windows deals with unicode paths here.
862 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
863 volume_info *volume )
865 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
866 LOCAL_VOLUME_INFO *vol;
867 LOCATION_INFO *loc;
868 LPSTR szLabel, szPath, szFinalPath;
869 ULONG count = 0;
871 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
873 /* figure out the size of everything */
874 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
875 NULL, 0, NULL, NULL );
876 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
877 NULL, 0, NULL, NULL );
878 volume_info_size = sizeof *vol + label_size;
879 final_path_size = 1;
880 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
882 /* create pointers to everything */
883 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
884 vol = (LOCAL_VOLUME_INFO*) &loc[1];
885 szLabel = (LPSTR) &vol[1];
886 szPath = &szLabel[label_size];
887 szFinalPath = &szPath[path_size];
889 /* fill in the location information header */
890 loc->dwTotalSize = total_size;
891 loc->dwHeaderSize = sizeof (*loc);
892 loc->dwFlags = 1;
893 loc->dwVolTableOfs = sizeof (*loc);
894 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
895 loc->dwNetworkVolTableOfs = 0;
896 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
898 /* fill in the volume information */
899 vol->dwSize = volume_info_size;
900 vol->dwType = volume->type;
901 vol->dwVolSerial = volume->serial;
902 vol->dwVolLabelOfs = sizeof (*vol);
904 /* copy in the strings */
905 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
906 szLabel, label_size, NULL, NULL );
907 WideCharToMultiByte( CP_ACP, 0, path, -1,
908 szPath, path_size, NULL, NULL );
909 szFinalPath[0] = 0;
911 return IStream_Write( stm, loc, total_size, &count );
914 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
916 ULONG count;
917 EXP_DARWIN_LINK buffer;
919 TRACE("%p\n",stm);
921 memset( &buffer, 0, sizeof buffer );
922 buffer.dbh.cbSize = sizeof buffer;
923 buffer.dbh.dwSignature = magic;
924 lstrcpynW( buffer.szwDarwinID, string, MAX_PATH );
925 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.szDarwinID, MAX_PATH, NULL, NULL );
927 return IStream_Write( stm, &buffer, buffer.dbh.cbSize, &count );
930 /************************************************************************
931 * IPersistStream_Save (IPersistStream)
933 * FIXME: makes assumptions about byte order
935 static HRESULT WINAPI IPersistStream_fnSave(
936 IPersistStream* iface,
937 IStream* stm,
938 BOOL fClearDirty)
940 static const WCHAR wOpen[] = {'o','p','e','n',0};
942 LINK_HEADER header;
943 WCHAR exePath[MAX_PATH];
944 ULONG count;
945 DWORD zero;
946 HRESULT r;
948 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
950 TRACE("%p %p %x\n", This, stm, fClearDirty);
952 *exePath = '\0';
954 if (This->sPath)
956 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
957 NULL, NULL, NULL, NULL);
959 * windows can create lnk files to executables that do not exist yet
960 * so if the executable does not exist the just trust the path they
961 * gave us
963 if (!*exePath) lstrcpyW(exePath,This->sPath);
966 memset(&header, 0, sizeof(header));
967 header.dwSize = sizeof(header);
968 header.fStartup = This->iShowCmd;
969 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
971 header.wHotKey = This->wHotKey;
972 header.nIcon = This->iIcoNdx;
973 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
974 if( This->pPidl )
975 header.dwFlags |= SLDF_HAS_ID_LIST;
976 if( This->sPath )
977 header.dwFlags |= SLDF_HAS_LINK_INFO;
978 if( This->sDescription )
979 header.dwFlags |= SLDF_HAS_NAME;
980 if( This->sWorkDir )
981 header.dwFlags |= SLDF_HAS_WORKINGDIR;
982 if( This->sArgs )
983 header.dwFlags |= SLDF_HAS_ARGS;
984 if( This->sIcoPath )
985 header.dwFlags |= SLDF_HAS_ICONLOCATION;
986 if( This->sProduct )
987 header.dwFlags |= SLDF_HAS_LOGO3ID;
988 if( This->sComponent )
989 header.dwFlags |= SLDF_HAS_DARWINID;
991 SystemTimeToFileTime ( &This->time1, &header.Time1 );
992 SystemTimeToFileTime ( &This->time2, &header.Time2 );
993 SystemTimeToFileTime ( &This->time3, &header.Time3 );
995 /* write the Shortcut header */
996 r = IStream_Write( stm, &header, sizeof(header), &count );
997 if( FAILED( r ) )
999 ERR("Write failed at %d\n",__LINE__);
1000 return r;
1003 TRACE("Writing pidl \n");
1005 /* write the PIDL to the shortcut */
1006 if( This->pPidl )
1008 r = ILSaveToStream( stm, This->pPidl );
1009 if( FAILED( r ) )
1011 ERR("Failed to write PIDL at %d\n",__LINE__);
1012 return r;
1016 if( This->sPath )
1017 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1019 if( This->sDescription )
1020 r = Stream_WriteString( stm, This->sDescription );
1022 if( This->sPathRel )
1023 r = Stream_WriteString( stm, This->sPathRel );
1025 if( This->sWorkDir )
1026 r = Stream_WriteString( stm, This->sWorkDir );
1028 if( This->sArgs )
1029 r = Stream_WriteString( stm, This->sArgs );
1031 if( This->sIcoPath )
1032 r = Stream_WriteString( stm, This->sIcoPath );
1034 if( This->sProduct )
1035 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1037 if( This->sComponent )
1038 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1040 /* the last field is a single zero dword */
1041 zero = 0;
1042 r = IStream_Write( stm, &zero, sizeof zero, &count );
1044 return S_OK;
1047 /************************************************************************
1048 * IPersistStream_GetSizeMax (IPersistStream)
1050 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1051 IPersistStream* iface,
1052 ULARGE_INTEGER* pcbSize)
1054 _ICOM_THIS_From_IPersistStream(IShellLinkImpl, iface);
1056 TRACE("(%p)\n", This);
1058 return E_NOTIMPL;
1061 static const IPersistStreamVtbl psvt =
1063 IPersistStream_fnQueryInterface,
1064 IPersistStream_fnAddRef,
1065 IPersistStream_fnRelease,
1066 IPersistStream_fnGetClassID,
1067 IPersistStream_fnIsDirty,
1068 IPersistStream_fnLoad,
1069 IPersistStream_fnSave,
1070 IPersistStream_fnGetSizeMax
1073 /**************************************************************************
1074 * IShellLink_Constructor
1076 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1077 REFIID riid, LPVOID *ppv )
1079 IShellLinkImpl * sl;
1081 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1083 *ppv = NULL;
1085 if (pUnkOuter)
1086 return CLASS_E_NOAGGREGATION;
1087 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1088 if (!sl)
1089 return E_OUTOFMEMORY;
1091 sl->ref = 1;
1092 sl->lpVtbl = &slvt;
1093 sl->lpvtblw = &slvtw;
1094 sl->lpvtblPersistFile = &pfvt;
1095 sl->lpvtblPersistStream = &psvt;
1096 sl->lpvtblShellLinkDataList = &dlvt;
1097 sl->lpvtblShellExtInit = &eivt;
1098 sl->lpvtblContextMenu = &cmvt;
1099 sl->iShowCmd = SW_SHOWNORMAL;
1100 sl->bDirty = FALSE;
1102 TRACE("(%p)->()\n",sl);
1104 if (IsEqualIID(riid, &IID_IUnknown) ||
1105 IsEqualIID(riid, &IID_IShellLinkA))
1106 *ppv = sl;
1107 else if (IsEqualIID(riid, &IID_IShellLinkW))
1108 *ppv = &(sl->lpvtblw);
1109 else {
1110 LocalFree((HLOCAL)sl);
1111 ERR("E_NOINTERFACE\n");
1112 return E_NOINTERFACE;
1115 return S_OK;
1119 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1121 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1122 return FALSE;
1123 return TRUE;
1126 /**************************************************************************
1127 * ShellLink_UpdatePath
1128 * update absolute path in sPath using relative path in sPathRel
1130 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1132 if (!path || !psPath)
1133 return E_INVALIDARG;
1135 if (!*psPath && sPathRel) {
1136 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1137 LPWSTR final = NULL;
1139 /* first try if [directory of link file] + [relative path] finds an existing file */
1141 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1142 if( !final )
1143 final = buffer;
1144 lstrcpyW(final, sPathRel);
1146 *abs_path = '\0';
1148 if (SHELL_ExistsFileW(buffer)) {
1149 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1150 lstrcpyW(abs_path, buffer);
1151 } else {
1152 /* try if [working directory] + [relative path] finds an existing file */
1153 if (sWorkDir) {
1154 lstrcpyW(buffer, sWorkDir);
1155 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1157 if (SHELL_ExistsFileW(buffer))
1158 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1159 lstrcpyW(abs_path, buffer);
1163 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1164 if (!*abs_path)
1165 lstrcpyW(abs_path, sPathRel);
1167 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1168 if (!*psPath)
1169 return E_OUTOFMEMORY;
1171 lstrcpyW(*psPath, abs_path);
1174 return S_OK;
1177 /**************************************************************************
1178 * IShellLink_ConstructFromFile
1180 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1181 LPCITEMIDLIST pidl, LPVOID* ppv)
1183 IShellLinkW* psl;
1185 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1187 if (SUCCEEDED(hr)) {
1188 IPersistFile* ppf;
1190 *ppv = NULL;
1192 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1194 if (SUCCEEDED(hr)) {
1195 WCHAR path[MAX_PATH];
1197 if (SHGetPathFromIDListW(pidl, path))
1198 hr = IPersistFile_Load(ppf, path, 0);
1199 else
1200 hr = E_FAIL;
1202 if (SUCCEEDED(hr))
1203 *ppv = (IUnknown*) psl;
1205 IPersistFile_Release(ppf);
1208 if (!*ppv)
1209 IShellLinkW_Release(psl);
1212 return hr;
1215 /**************************************************************************
1216 * IShellLinkA_QueryInterface
1218 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1220 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1222 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
1224 *ppvObj = NULL;
1226 if(IsEqualIID(riid, &IID_IUnknown) ||
1227 IsEqualIID(riid, &IID_IShellLinkA))
1229 *ppvObj = This;
1231 else if(IsEqualIID(riid, &IID_IShellLinkW))
1233 *ppvObj = &(This->lpvtblw);
1235 else if(IsEqualIID(riid, &IID_IPersistFile))
1237 *ppvObj = &(This->lpvtblPersistFile);
1239 else if(IsEqualIID(riid, &IID_IPersistStream))
1241 *ppvObj = &(This->lpvtblPersistStream);
1243 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
1245 *ppvObj = &(This->lpvtblShellLinkDataList);
1247 else if(IsEqualIID(riid, &IID_IShellExtInit))
1249 *ppvObj = &(This->lpvtblShellExtInit);
1251 else if(IsEqualIID(riid, &IID_IContextMenu))
1253 *ppvObj = &(This->lpvtblContextMenu);
1256 if(*ppvObj)
1258 IUnknown_AddRef((IUnknown*)(*ppvObj));
1259 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1260 return S_OK;
1262 ERR("-- Interface: E_NOINTERFACE\n");
1263 return E_NOINTERFACE;
1266 /******************************************************************************
1267 * IShellLinkA_AddRef
1269 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1271 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1272 ULONG refCount = InterlockedIncrement(&This->ref);
1274 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
1276 return refCount;
1279 /******************************************************************************
1280 * IShellLinkA_Release
1282 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1284 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1285 ULONG refCount = InterlockedDecrement(&This->ref);
1287 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
1289 if (refCount)
1290 return refCount;
1292 TRACE("-- destroying IShellLink(%p)\n",This);
1294 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1295 HeapFree(GetProcessHeap(), 0, This->sArgs);
1296 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1297 HeapFree(GetProcessHeap(), 0, This->sDescription);
1298 HeapFree(GetProcessHeap(),0,This->sPath);
1300 if (This->pPidl)
1301 ILFree(This->pPidl);
1303 LocalFree((HANDLE)This);
1305 return 0;
1308 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1309 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1311 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1313 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1314 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1316 if (This->sComponent || This->sProduct)
1317 return S_FALSE;
1319 if (cchMaxPath)
1320 pszFile[0] = 0;
1321 if (This->sPath)
1322 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1323 pszFile, cchMaxPath, NULL, NULL);
1325 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1327 return S_OK;
1330 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1332 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1334 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1336 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1339 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1341 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1343 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1345 if (This->pPidl)
1346 ILFree(This->pPidl);
1347 This->pPidl = ILClone (pidl);
1348 This->bDirty = TRUE;
1350 return S_OK;
1353 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1355 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1357 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1359 if( cchMaxName )
1360 pszName[0] = 0;
1361 if( This->sDescription )
1362 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1363 pszName, cchMaxName, NULL, NULL);
1365 return S_OK;
1368 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1370 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1372 TRACE("(%p)->(pName=%s)\n", This, pszName);
1374 HeapFree(GetProcessHeap(), 0, This->sDescription);
1375 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1376 if ( !This->sDescription )
1377 return E_OUTOFMEMORY;
1379 This->bDirty = TRUE;
1381 return S_OK;
1384 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1386 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1388 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1390 if( cchMaxPath )
1391 pszDir[0] = 0;
1392 if( This->sWorkDir )
1393 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1394 pszDir, cchMaxPath, NULL, NULL);
1396 return S_OK;
1399 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1401 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1403 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1405 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1406 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1407 if ( !This->sWorkDir )
1408 return E_OUTOFMEMORY;
1410 This->bDirty = TRUE;
1412 return S_OK;
1415 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1417 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1419 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1421 if( cchMaxPath )
1422 pszArgs[0] = 0;
1423 if( This->sArgs )
1424 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1425 pszArgs, cchMaxPath, NULL, NULL);
1427 return S_OK;
1430 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1432 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1434 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1436 HeapFree(GetProcessHeap(), 0, This->sArgs);
1437 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1438 if( !This->sArgs )
1439 return E_OUTOFMEMORY;
1441 This->bDirty = TRUE;
1443 return S_OK;
1446 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1448 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1450 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1452 *pwHotkey = This->wHotKey;
1454 return S_OK;
1457 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1459 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1461 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1463 This->wHotKey = wHotkey;
1464 This->bDirty = TRUE;
1466 return S_OK;
1469 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1471 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1473 TRACE("(%p)->(%p)\n",This, piShowCmd);
1474 *piShowCmd = This->iShowCmd;
1475 return S_OK;
1478 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1480 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1482 TRACE("(%p) %d\n",This, iShowCmd);
1484 This->iShowCmd = iShowCmd;
1485 This->bDirty = TRUE;
1487 return NOERROR;
1490 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1492 LPCITEMIDLIST pidlLast;
1494 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1496 if (SUCCEEDED(hr)) {
1497 IExtractIconA* pei;
1499 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1501 if (SUCCEEDED(hr)) {
1502 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1504 IExtractIconA_Release(pei);
1507 IShellFolder_Release(psf);
1510 return hr;
1513 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1515 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1517 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1519 pszIconPath[0] = 0;
1520 *piIcon = This->iIcoNdx;
1522 if (This->sIcoPath)
1524 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1525 return S_OK;
1528 if (This->pPidl || This->sPath)
1530 IShellFolder* pdsk;
1532 HRESULT hr = SHGetDesktopFolder(&pdsk);
1534 if (SUCCEEDED(hr))
1536 /* first look for an icon using the PIDL (if present) */
1537 if (This->pPidl)
1538 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1539 else
1540 hr = E_FAIL;
1542 /* if we couldn't find an icon yet, look for it using the file system path */
1543 if (FAILED(hr) && This->sPath)
1545 LPITEMIDLIST pidl;
1547 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1549 if (SUCCEEDED(hr)) {
1550 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1552 SHFree(pidl);
1556 IShellFolder_Release(pdsk);
1559 return hr;
1561 return S_OK;
1564 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1566 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1568 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1570 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1571 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1572 if ( !This->sIcoPath )
1573 return E_OUTOFMEMORY;
1575 This->iIcoNdx = iIcon;
1576 This->bDirty = TRUE;
1578 return S_OK;
1581 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1583 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1585 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1587 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1588 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1589 This->bDirty = TRUE;
1591 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1594 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1596 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1598 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1600 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1603 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1605 HRESULT r;
1606 LPWSTR str;
1607 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1609 TRACE("(%p)->(path=%s)\n",This, pszFile);
1611 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1612 if( !str )
1613 return E_OUTOFMEMORY;
1615 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1616 HeapFree( GetProcessHeap(), 0, str );
1618 return r;
1621 /**************************************************************************
1622 * IShellLink Implementation
1625 static const IShellLinkAVtbl slvt =
1627 IShellLinkA_fnQueryInterface,
1628 IShellLinkA_fnAddRef,
1629 IShellLinkA_fnRelease,
1630 IShellLinkA_fnGetPath,
1631 IShellLinkA_fnGetIDList,
1632 IShellLinkA_fnSetIDList,
1633 IShellLinkA_fnGetDescription,
1634 IShellLinkA_fnSetDescription,
1635 IShellLinkA_fnGetWorkingDirectory,
1636 IShellLinkA_fnSetWorkingDirectory,
1637 IShellLinkA_fnGetArguments,
1638 IShellLinkA_fnSetArguments,
1639 IShellLinkA_fnGetHotkey,
1640 IShellLinkA_fnSetHotkey,
1641 IShellLinkA_fnGetShowCmd,
1642 IShellLinkA_fnSetShowCmd,
1643 IShellLinkA_fnGetIconLocation,
1644 IShellLinkA_fnSetIconLocation,
1645 IShellLinkA_fnSetRelativePath,
1646 IShellLinkA_fnResolve,
1647 IShellLinkA_fnSetPath
1651 /**************************************************************************
1652 * IShellLinkW_fnQueryInterface
1654 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1655 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1657 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1658 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObj);
1661 /******************************************************************************
1662 * IShellLinkW_fnAddRef
1664 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1666 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1667 return IShellLinkA_AddRef((IShellLinkA*)This);
1669 /******************************************************************************
1670 * IShellLinkW_fnRelease
1673 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1675 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1676 return IShellLinkA_Release((IShellLinkA*)This);
1679 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1681 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1683 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1684 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1686 if (This->sComponent || This->sProduct)
1687 return S_FALSE;
1689 if (cchMaxPath)
1690 pszFile[0] = 0;
1691 if (This->sPath)
1692 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1694 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1696 return S_OK;
1699 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1701 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1703 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1705 if (!This->pPidl)
1706 return S_FALSE;
1707 *ppidl = ILClone(This->pPidl);
1708 return S_OK;
1711 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1713 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1715 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1717 if( This->pPidl )
1718 ILFree( This->pPidl );
1719 This->pPidl = ILClone( pidl );
1720 if( !This->pPidl )
1721 return E_FAIL;
1723 This->bDirty = TRUE;
1725 return S_OK;
1728 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1730 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1732 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1734 pszName[0] = 0;
1735 if( This->sDescription )
1736 lstrcpynW( pszName, This->sDescription, cchMaxName );
1738 return S_OK;
1741 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1743 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1745 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1747 HeapFree(GetProcessHeap(), 0, This->sDescription);
1748 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1749 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1750 if ( !This->sDescription )
1751 return E_OUTOFMEMORY;
1753 lstrcpyW( This->sDescription, pszName );
1754 This->bDirty = TRUE;
1756 return S_OK;
1759 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1761 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1763 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1765 if( cchMaxPath )
1766 pszDir[0] = 0;
1767 if( This->sWorkDir )
1768 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1770 return S_OK;
1773 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1775 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1777 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1779 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1780 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1781 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1782 if ( !This->sWorkDir )
1783 return E_OUTOFMEMORY;
1784 lstrcpyW( This->sWorkDir, pszDir );
1785 This->bDirty = TRUE;
1787 return S_OK;
1790 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1792 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1794 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1796 if( cchMaxPath )
1797 pszArgs[0] = 0;
1798 if( This->sArgs )
1799 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1801 return NOERROR;
1804 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1806 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1808 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1810 HeapFree(GetProcessHeap(), 0, This->sArgs);
1811 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1812 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1813 if ( !This->sArgs )
1814 return E_OUTOFMEMORY;
1815 lstrcpyW( This->sArgs, pszArgs );
1816 This->bDirty = TRUE;
1818 return S_OK;
1821 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1823 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1825 TRACE("(%p)->(%p)\n",This, pwHotkey);
1827 *pwHotkey=This->wHotKey;
1829 return S_OK;
1832 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1834 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1836 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1838 This->wHotKey = wHotkey;
1839 This->bDirty = TRUE;
1841 return S_OK;
1844 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1846 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1848 TRACE("(%p)->(%p)\n",This, piShowCmd);
1850 *piShowCmd = This->iShowCmd;
1852 return S_OK;
1855 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1857 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1859 This->iShowCmd = iShowCmd;
1860 This->bDirty = TRUE;
1862 return S_OK;
1865 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1867 LPCITEMIDLIST pidlLast;
1869 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1871 if (SUCCEEDED(hr)) {
1872 IExtractIconW* pei;
1874 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1876 if (SUCCEEDED(hr)) {
1877 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1879 IExtractIconW_Release(pei);
1882 IShellFolder_Release(psf);
1885 return hr;
1888 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1890 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1892 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1894 pszIconPath[0] = 0;
1895 *piIcon = This->iIcoNdx;
1897 if (This->sIcoPath)
1899 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1900 return S_OK;
1903 if (This->pPidl || This->sPath)
1905 IShellFolder* pdsk;
1907 HRESULT hr = SHGetDesktopFolder(&pdsk);
1909 if (SUCCEEDED(hr))
1911 /* first look for an icon using the PIDL (if present) */
1912 if (This->pPidl)
1913 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1914 else
1915 hr = E_FAIL;
1917 /* if we couldn't find an icon yet, look for it using the file system path */
1918 if (FAILED(hr) && This->sPath)
1920 LPITEMIDLIST pidl;
1922 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1924 if (SUCCEEDED(hr))
1926 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1928 SHFree(pidl);
1932 IShellFolder_Release(pdsk);
1934 return hr;
1936 return S_OK;
1939 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1941 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1943 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1945 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1946 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1947 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1948 if ( !This->sIcoPath )
1949 return E_OUTOFMEMORY;
1950 lstrcpyW( This->sIcoPath, pszIconPath );
1952 This->iIcoNdx = iIcon;
1953 This->bDirty = TRUE;
1955 return S_OK;
1958 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1960 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1962 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1964 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1965 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1966 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1967 if ( !This->sPathRel )
1968 return E_OUTOFMEMORY;
1969 lstrcpyW( This->sPathRel, pszPathRel );
1970 This->bDirty = TRUE;
1972 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1975 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1977 HRESULT hr = S_OK;
1978 BOOL bSuccess;
1980 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
1982 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1984 /*FIXME: use IResolveShellLink interface */
1986 if (!This->sPath && This->pPidl) {
1987 WCHAR buffer[MAX_PATH];
1989 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
1991 if (bSuccess && *buffer) {
1992 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
1993 if (!This->sPath)
1994 return E_OUTOFMEMORY;
1996 lstrcpyW(This->sPath, buffer);
1998 This->bDirty = TRUE;
1999 } else
2000 hr = S_OK; /* don't report an error occurred while just caching information */
2003 if (!This->sIcoPath && This->sPath) {
2004 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2005 if (!This->sIcoPath)
2006 return E_OUTOFMEMORY;
2008 lstrcpyW(This->sIcoPath, This->sPath);
2009 This->iIcoNdx = 0;
2011 This->bDirty = TRUE;
2014 return hr;
2017 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2019 LPWSTR ret;
2020 LPCWSTR p;
2021 DWORD len;
2023 if( !str )
2024 return NULL;
2026 p = strchrW( str, ':' );
2027 if( !p )
2028 return NULL;
2029 len = p - str;
2030 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2031 if( !ret )
2032 return ret;
2033 memcpy( ret, str, sizeof(WCHAR)*len );
2034 ret[len] = 0;
2035 return ret;
2038 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2040 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2041 WCHAR szGuid[39];
2042 HRESULT r;
2043 GUID guid;
2044 int len;
2046 while( str[0] )
2048 /* each segment must start with two colons */
2049 if( str[0] != ':' || str[1] != ':' )
2050 return E_FAIL;
2052 /* the last segment is just two colons */
2053 if( !str[2] )
2054 break;
2055 str += 2;
2057 /* there must be a colon straight after a guid */
2058 p = strchrW( str, ':' );
2059 if( !p )
2060 return E_FAIL;
2061 len = p - str;
2062 if( len != 38 )
2063 return E_FAIL;
2065 /* get the guid, and check it's validly formatted */
2066 memcpy( szGuid, str, sizeof(WCHAR)*len );
2067 szGuid[len] = 0;
2068 r = CLSIDFromString( szGuid, &guid );
2069 if( r != S_OK )
2070 return r;
2071 str = p + 1;
2073 /* match it up to a guid that we care about */
2074 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2075 szComponent = str;
2076 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2077 szProduct = str;
2078 else
2079 return E_FAIL;
2081 /* skip to the next field */
2082 str = strchrW( str, ':' );
2083 if( !str )
2084 return E_FAIL;
2087 /* we have to have a component for an advertised shortcut */
2088 if( !szComponent )
2089 return E_FAIL;
2091 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2092 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2094 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2095 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2097 return S_OK;
2100 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2102 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2103 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2104 BOOL r;
2106 volume->type = GetDriveTypeW(drive);
2107 r = GetVolumeInformationW(drive, volume->label, label_sz,
2108 &volume->serial, NULL, NULL, NULL, 0);
2109 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2110 volume->type, volume->serial, debugstr_w(volume->label));
2111 return r;
2114 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2116 _ICOM_THIS_From_IShellLinkW(IShellLinkImpl, iface);
2117 WCHAR buffer[MAX_PATH];
2118 LPWSTR fname;
2119 HRESULT hr = S_OK;
2121 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2123 HeapFree(GetProcessHeap(), 0, This->sPath);
2124 This->sPath = NULL;
2126 HeapFree(GetProcessHeap(), 0, This->sComponent);
2127 This->sComponent = NULL;
2129 if (This->pPidl)
2130 ILFree(This->pPidl);
2131 This->pPidl = NULL;
2133 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2135 if (*pszFile == '\0')
2136 *buffer = '\0';
2137 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2138 return E_FAIL;
2139 else if(!PathFileExistsW(buffer))
2140 hr = S_FALSE;
2142 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2143 ShellLink_GetVolumeInfo(buffer, &This->volume);
2145 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2146 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2147 if (!This->sPath)
2148 return E_OUTOFMEMORY;
2150 lstrcpyW(This->sPath, buffer);
2152 This->bDirty = TRUE;
2154 return hr;
2157 /**************************************************************************
2158 * IShellLinkW Implementation
2161 static const IShellLinkWVtbl slvtw =
2163 IShellLinkW_fnQueryInterface,
2164 IShellLinkW_fnAddRef,
2165 IShellLinkW_fnRelease,
2166 IShellLinkW_fnGetPath,
2167 IShellLinkW_fnGetIDList,
2168 IShellLinkW_fnSetIDList,
2169 IShellLinkW_fnGetDescription,
2170 IShellLinkW_fnSetDescription,
2171 IShellLinkW_fnGetWorkingDirectory,
2172 IShellLinkW_fnSetWorkingDirectory,
2173 IShellLinkW_fnGetArguments,
2174 IShellLinkW_fnSetArguments,
2175 IShellLinkW_fnGetHotkey,
2176 IShellLinkW_fnSetHotkey,
2177 IShellLinkW_fnGetShowCmd,
2178 IShellLinkW_fnSetShowCmd,
2179 IShellLinkW_fnGetIconLocation,
2180 IShellLinkW_fnSetIconLocation,
2181 IShellLinkW_fnSetRelativePath,
2182 IShellLinkW_fnResolve,
2183 IShellLinkW_fnSetPath
2186 static HRESULT WINAPI
2187 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2189 _ICOM_THIS_From_IShellLinkDataList(IShellLinkImpl, iface);
2190 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2193 static ULONG WINAPI
2194 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2196 _ICOM_THIS_From_IShellLinkDataList(IShellLinkImpl, iface);
2197 return IShellLinkA_AddRef((IShellLinkA*)This);
2200 static ULONG WINAPI
2201 ShellLink_DataList_Release( IShellLinkDataList* iface )
2203 _ICOM_THIS_From_IShellLinkDataList(IShellLinkImpl, iface);
2204 return IShellLinkA_Release((IShellLinkA*)This);
2207 static HRESULT WINAPI
2208 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2210 FIXME("\n");
2211 return E_NOTIMPL;
2214 static HRESULT WINAPI
2215 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2217 FIXME("\n");
2218 return E_NOTIMPL;
2221 static HRESULT WINAPI
2222 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2224 FIXME("\n");
2225 return E_NOTIMPL;
2228 static HRESULT WINAPI
2229 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2231 FIXME("\n");
2232 return E_NOTIMPL;
2235 static HRESULT WINAPI
2236 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2238 FIXME("\n");
2239 return E_NOTIMPL;
2242 static const IShellLinkDataListVtbl dlvt =
2244 ShellLink_DataList_QueryInterface,
2245 ShellLink_DataList_AddRef,
2246 ShellLink_DataList_Release,
2247 ShellLink_AddDataBlock,
2248 ShellLink_CopyDataBlock,
2249 ShellLink_RemoveDataBlock,
2250 ShellLink_GetFlags,
2251 ShellLink_SetFlags
2254 static HRESULT WINAPI
2255 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2257 _ICOM_THIS_From_IShellExtInit(IShellLinkImpl, iface);
2258 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2261 static ULONG WINAPI
2262 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2264 _ICOM_THIS_From_IShellExtInit(IShellLinkImpl, iface);
2265 return IShellLinkA_AddRef((IShellLinkA*)This);
2268 static ULONG WINAPI
2269 ShellLink_ExtInit_Release( IShellExtInit* iface )
2271 _ICOM_THIS_From_IShellExtInit(IShellLinkImpl, iface);
2272 return IShellLinkA_Release((IShellLinkA*)This);
2275 /**************************************************************************
2276 * ShellLink implementation of IShellExtInit::Initialize()
2278 * Loads the shelllink from the dataobject the shell is pointing to.
2280 static HRESULT WINAPI
2281 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2282 IDataObject *pdtobj, HKEY hkeyProgID )
2284 _ICOM_THIS_From_IShellExtInit(IShellLinkImpl, iface);
2285 FORMATETC format;
2286 STGMEDIUM stgm;
2287 UINT count;
2288 HRESULT r = E_FAIL;
2290 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2292 if( !pdtobj )
2293 return r;
2295 format.cfFormat = CF_HDROP;
2296 format.ptd = NULL;
2297 format.dwAspect = DVASPECT_CONTENT;
2298 format.lindex = -1;
2299 format.tymed = TYMED_HGLOBAL;
2301 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2302 return r;
2304 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2305 if( count == 1 )
2307 LPWSTR path;
2309 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2310 count++;
2311 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2312 if( path )
2314 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2316 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2317 r = IPersistFile_Load( pf, path, 0 );
2318 HeapFree( GetProcessHeap(), 0, path );
2321 ReleaseStgMedium( &stgm );
2323 return r;
2326 static const IShellExtInitVtbl eivt =
2328 ShellLink_ExtInit_QueryInterface,
2329 ShellLink_ExtInit_AddRef,
2330 ShellLink_ExtInit_Release,
2331 ShellLink_ExtInit_Initialize
2334 static HRESULT WINAPI
2335 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2337 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2338 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2341 static ULONG WINAPI
2342 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2344 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2345 return IShellLinkA_AddRef((IShellLinkA*)This);
2348 static ULONG WINAPI
2349 ShellLink_ContextMenu_Release( IContextMenu* iface )
2351 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2352 return IShellLinkA_Release((IShellLinkA*)This);
2355 static HRESULT WINAPI
2356 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2357 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2359 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2361 FIXME("%p %p %u %u %u %u\n", This,
2362 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2364 return E_NOTIMPL;
2367 static HRESULT WINAPI
2368 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2370 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2372 FIXME("%p %p\n", This, lpici );
2374 return E_NOTIMPL;
2377 static HRESULT WINAPI
2378 ShellLink_GetCommandString( IContextMenu* iface, UINT idCmd, UINT uType,
2379 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2381 _ICOM_THIS_From_IContextMenu(IShellLinkImpl, iface);
2383 FIXME("%p %u %u %p %p %u\n", This,
2384 idCmd, uType, pwReserved, pszName, cchMax );
2386 return E_NOTIMPL;
2389 static const IContextMenuVtbl cmvt =
2391 ShellLink_ContextMenu_QueryInterface,
2392 ShellLink_ContextMenu_AddRef,
2393 ShellLink_ContextMenu_Release,
2394 ShellLink_QueryContextMenu,
2395 ShellLink_InvokeCommand,
2396 ShellLink_GetCommandString