Use the proper WOW functions everywhere instead of the K32WOW variant.
[wine/testsucceed.git] / dlls / shell32 / shelllink.c
blob8d57805677144cd870e53464aa3508c766aa80a6
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 LONG 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));
165 static inline IShellLinkImpl *impl_from_IPersistFile( IPersistFile *iface )
167 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistFile));
170 static inline IShellLinkImpl *impl_from_IPersistStream( IPersistStream *iface )
172 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblPersistStream));
175 static inline IShellLinkImpl *impl_from_IShellLinkDataList( IShellLinkDataList *iface )
177 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellLinkDataList));
180 static inline IShellLinkImpl *impl_from_IShellExtInit( IShellExtInit *iface )
182 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblShellExtInit));
185 static inline IShellLinkImpl *impl_from_IContextMenu( IContextMenu *iface )
187 return (IShellLinkImpl *)((char*)iface - FIELD_OFFSET(IShellLinkImpl, lpvtblContextMenu));
190 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath);
192 /* strdup on the process heap */
193 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str)
195 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
196 LPWSTR p = HeapAlloc( heap, flags, len*sizeof (WCHAR) );
197 if( !p )
198 return p;
199 MultiByteToWideChar( CP_ACP, 0, str, -1, p, len );
200 return p;
203 /**************************************************************************
204 * ShellLink::QueryInterface implementation
206 static HRESULT ShellLink_QueryInterface( IShellLinkImpl *This, REFIID riid, LPVOID *ppvObj)
208 TRACE("(%p)->(\n\tIID:\t%s)\n",This,debugstr_guid(riid));
210 *ppvObj = NULL;
212 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IShellLinkA))
214 *ppvObj = This;
216 else if(IsEqualIID(riid, &IID_IShellLinkW))
218 *ppvObj = &(This->lpvtblw);
220 else if(IsEqualIID(riid, &IID_IPersistFile))
222 *ppvObj = &(This->lpvtblPersistFile);
224 else if(IsEqualIID(riid, &IID_IPersistStream))
226 *ppvObj = &(This->lpvtblPersistStream);
228 else if(IsEqualIID(riid, &IID_IShellLinkDataList))
230 *ppvObj = &(This->lpvtblShellLinkDataList);
232 else if(IsEqualIID(riid, &IID_IShellExtInit))
234 *ppvObj = &(This->lpvtblShellExtInit);
236 else if(IsEqualIID(riid, &IID_IContextMenu))
238 *ppvObj = &(This->lpvtblContextMenu);
241 if(*ppvObj)
243 IUnknown_AddRef((IUnknown*)(*ppvObj));
244 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
245 return S_OK;
247 ERR("-- Interface: E_NOINTERFACE\n");
248 return E_NOINTERFACE;
251 /**************************************************************************
252 * ShellLink::AddRef implementation
254 static ULONG ShellLink_AddRef( IShellLinkImpl *This )
256 ULONG refCount = InterlockedIncrement(&This->ref);
258 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
260 return refCount;
263 /**************************************************************************
264 * ShellLink::Release implementation
266 static ULONG ShellLink_Release( IShellLinkImpl *This )
268 ULONG refCount = InterlockedDecrement(&This->ref);
270 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
272 if (refCount)
273 return refCount;
275 TRACE("-- destroying IShellLink(%p)\n",This);
277 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
278 HeapFree(GetProcessHeap(), 0, This->sArgs);
279 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
280 HeapFree(GetProcessHeap(), 0, This->sDescription);
281 HeapFree(GetProcessHeap(),0,This->sPath);
283 if (This->pPidl)
284 ILFree(This->pPidl);
286 LocalFree((HANDLE)This);
288 return 0;
291 static HRESULT ShellLink_GetClassID( IShellLinkImpl *This, CLSID *pclsid )
293 TRACE("%p %p\n", This, pclsid);
295 memcpy( pclsid, &CLSID_ShellLink, sizeof (CLSID) );
296 return S_OK;
299 /**************************************************************************
300 * IPersistFile_QueryInterface
302 static HRESULT WINAPI IPersistFile_fnQueryInterface(
303 IPersistFile* iface,
304 REFIID riid,
305 LPVOID *ppvObj)
307 IShellLinkImpl *This = impl_from_IPersistFile(iface);
308 return ShellLink_QueryInterface( This, riid, ppvObj );
311 /******************************************************************************
312 * IPersistFile_AddRef
314 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile* iface)
316 IShellLinkImpl *This = impl_from_IPersistFile(iface);
317 return ShellLink_AddRef( This );
320 /******************************************************************************
321 * IPersistFile_Release
323 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile* iface)
325 IShellLinkImpl *This = impl_from_IPersistFile(iface);
326 return IShellLinkA_Release((IShellLinkA*)This);
329 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile* iface, CLSID *pClassID)
331 IShellLinkImpl *This = impl_from_IPersistFile(iface);
332 return ShellLink_GetClassID( This, pClassID );
335 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile* iface)
337 IShellLinkImpl *This = impl_from_IPersistFile(iface);
339 TRACE("(%p)\n",This);
341 if (This->bDirty)
342 return S_OK;
344 return S_FALSE;
347 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
349 IShellLinkImpl *This = impl_from_IPersistFile(iface);
350 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
351 HRESULT r;
352 IStream *stm;
354 TRACE("(%p, %s, %lx)\n",This, debugstr_w(pszFileName), dwMode);
356 if( dwMode == 0 )
357 dwMode = STGM_READ | STGM_SHARE_DENY_WRITE;
358 r = SHCreateStreamOnFileW(pszFileName, dwMode, &stm);
359 if( SUCCEEDED( r ) )
361 r = IPersistStream_Load(StreamThis, stm);
362 ShellLink_UpdatePath(This->sPathRel, pszFileName, This->sWorkDir, &This->sPath);
363 IStream_Release( stm );
364 This->bDirty = FALSE;
366 TRACE("-- returning hr %08lx\n", r);
367 return r;
370 static BOOL StartLinkProcessor( LPCOLESTR szLink )
372 static const WCHAR szFormat[] = {
373 'w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',
374 ' ','-','r',' ','"','%','s','"',0 };
375 LONG len;
376 LPWSTR buffer;
377 STARTUPINFOW si;
378 PROCESS_INFORMATION pi;
380 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
381 buffer = HeapAlloc( GetProcessHeap(), 0, len );
382 if( !buffer )
383 return FALSE;
385 wsprintfW( buffer, szFormat, szLink );
387 TRACE("starting %s\n",debugstr_w(buffer));
389 memset(&si, 0, sizeof(si));
390 si.cb = sizeof(si);
391 if (!CreateProcessW( NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE;
393 /* wait for a while to throttle the creation of linker processes */
394 if( WAIT_OBJECT_0 != WaitForSingleObject( pi.hProcess, 10000 ) )
395 WARN("Timed out waiting for shell linker\n");
397 CloseHandle( pi.hProcess );
398 CloseHandle( pi.hThread );
400 return TRUE;
403 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile* iface, LPCOLESTR pszFileName, BOOL fRemember)
405 IShellLinkImpl *This = impl_from_IPersistFile(iface);
406 IPersistStream *StreamThis = (IPersistStream *)&This->lpvtblPersistStream;
407 HRESULT r;
408 IStream *stm;
410 TRACE("(%p)->(%s)\n",This,debugstr_w(pszFileName));
412 if (!pszFileName)
413 return E_FAIL;
415 r = SHCreateStreamOnFileW( pszFileName, STGM_READWRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, &stm );
416 if( SUCCEEDED( r ) )
418 r = IPersistStream_Save(StreamThis, stm, FALSE);
419 IStream_Release( stm );
421 if( SUCCEEDED( r ) )
423 StartLinkProcessor( pszFileName );
425 This->bDirty = FALSE;
427 else
429 DeleteFileW( pszFileName );
430 WARN("Failed to create shortcut %s\n", debugstr_w(pszFileName) );
434 return r;
437 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile* iface, LPCOLESTR pszFileName)
439 IShellLinkImpl *This = impl_from_IPersistFile(iface);
440 FIXME("(%p)->(%s)\n",This,debugstr_w(pszFileName));
441 return NOERROR;
444 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile* iface, LPOLESTR *ppszFileName)
446 IShellLinkImpl *This = impl_from_IPersistFile(iface);
447 FIXME("(%p)\n",This);
448 return NOERROR;
451 static const IPersistFileVtbl pfvt =
453 IPersistFile_fnQueryInterface,
454 IPersistFile_fnAddRef,
455 IPersistFile_fnRelease,
456 IPersistFile_fnGetClassID,
457 IPersistFile_fnIsDirty,
458 IPersistFile_fnLoad,
459 IPersistFile_fnSave,
460 IPersistFile_fnSaveCompleted,
461 IPersistFile_fnGetCurFile
464 /************************************************************************
465 * IPersistStream_QueryInterface
467 static HRESULT WINAPI IPersistStream_fnQueryInterface(
468 IPersistStream* iface,
469 REFIID riid,
470 VOID** ppvObj)
472 IShellLinkImpl *This = impl_from_IPersistStream(iface);
473 return ShellLink_QueryInterface( This, riid, ppvObj );
476 /************************************************************************
477 * IPersistStream_Release
479 static ULONG WINAPI IPersistStream_fnRelease(
480 IPersistStream* iface)
482 IShellLinkImpl *This = impl_from_IPersistStream(iface);
483 return IShellLinkA_Release((IShellLinkA*)This);
486 /************************************************************************
487 * IPersistStream_AddRef
489 static ULONG WINAPI IPersistStream_fnAddRef(
490 IPersistStream* iface)
492 IShellLinkImpl *This = impl_from_IPersistStream(iface);
493 return ShellLink_AddRef( This );
496 /************************************************************************
497 * IPersistStream_GetClassID
500 static HRESULT WINAPI IPersistStream_fnGetClassID(
501 IPersistStream* iface,
502 CLSID* pClassID)
504 IShellLinkImpl *This = impl_from_IPersistStream(iface);
505 return ShellLink_GetClassID( This, pClassID );
508 /************************************************************************
509 * IPersistStream_IsDirty (IPersistStream)
511 static HRESULT WINAPI IPersistStream_fnIsDirty(
512 IPersistStream* iface)
514 IShellLinkImpl *This = impl_from_IPersistStream(iface);
516 TRACE("(%p)\n", This);
518 return S_OK;
522 static HRESULT Stream_LoadString( IStream* stm, BOOL unicode, LPWSTR *pstr )
524 DWORD count;
525 USHORT len;
526 LPVOID temp;
527 LPWSTR str;
528 HRESULT r;
530 TRACE("%p\n", stm);
532 count = 0;
533 r = IStream_Read(stm, &len, sizeof(len), &count);
534 if ( FAILED (r) || ( count != sizeof(len) ) )
535 return E_FAIL;
537 if( unicode )
538 len *= sizeof (WCHAR);
540 TRACE("reading %d\n", len);
541 temp = HeapAlloc(GetProcessHeap(), 0, len+sizeof(WCHAR));
542 if( !temp )
543 return E_OUTOFMEMORY;
544 count = 0;
545 r = IStream_Read(stm, temp, len, &count);
546 if( FAILED (r) || ( count != len ) )
548 HeapFree( GetProcessHeap(), 0, temp );
549 return E_FAIL;
552 TRACE("read %s\n", debugstr_an(temp,len));
554 /* convert to unicode if necessary */
555 if( !unicode )
557 count = MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, NULL, 0 );
558 str = HeapAlloc( GetProcessHeap(), 0, (count+1)*sizeof (WCHAR) );
559 if( str )
560 MultiByteToWideChar( CP_ACP, 0, (LPSTR) temp, len, str, count );
561 HeapFree( GetProcessHeap(), 0, temp );
563 else
565 count /= 2;
566 str = (LPWSTR) temp;
568 str[count] = 0;
570 *pstr = str;
572 return S_OK;
575 static HRESULT Stream_ReadChunk( IStream* stm, LPVOID *data )
577 DWORD size;
578 ULONG count;
579 HRESULT r;
580 struct sized_chunk {
581 DWORD size;
582 unsigned char data[1];
583 } *chunk;
585 TRACE("%p\n",stm);
587 r = IStream_Read( stm, &size, sizeof(size), &count );
588 if( FAILED( r ) || count != sizeof(size) )
589 return E_FAIL;
591 chunk = HeapAlloc( GetProcessHeap(), 0, size );
592 if( !chunk )
593 return E_OUTOFMEMORY;
595 chunk->size = size;
596 r = IStream_Read( stm, chunk->data, size - sizeof(size), &count );
597 if( FAILED( r ) || count != (size - sizeof(size)) )
599 HeapFree( GetProcessHeap(), 0, chunk );
600 return E_FAIL;
603 TRACE("Read %ld bytes\n",chunk->size);
605 *data = (LPVOID) chunk;
607 return S_OK;
610 static BOOL Stream_LoadVolume( LOCAL_VOLUME_INFO *vol, volume_info *volume )
612 const int label_sz = sizeof volume->label/sizeof volume->label[0];
613 LPSTR label;
614 int len;
616 volume->serial = vol->dwVolSerial;
617 volume->type = vol->dwType;
619 if( !vol->dwVolLabelOfs )
620 return FALSE;
621 if( vol->dwSize <= vol->dwVolLabelOfs )
622 return FALSE;
623 len = vol->dwSize - vol->dwVolLabelOfs;
625 label = (LPSTR) vol;
626 label += vol->dwVolLabelOfs;
627 MultiByteToWideChar( CP_ACP, 0, label, len, volume->label, label_sz-1);
629 return TRUE;
632 static LPWSTR Stream_LoadPath( LPSTR p, DWORD maxlen )
634 int len = 0, wlen;
635 LPWSTR path;
637 while( p[len] && (len < maxlen) )
638 len++;
640 wlen = MultiByteToWideChar(CP_ACP, 0, p, len, NULL, 0);
641 path = HeapAlloc(GetProcessHeap(), 0, (wlen+1)*sizeof(WCHAR));
642 MultiByteToWideChar(CP_ACP, 0, p, len, path, wlen);
643 path[wlen] = 0;
645 return path;
648 static HRESULT Stream_LoadLocation( IStream *stm,
649 volume_info *volume, LPWSTR *path )
651 char *p = NULL;
652 LOCATION_INFO *loc;
653 HRESULT r;
654 int n;
656 r = Stream_ReadChunk( stm, (LPVOID*) &p );
657 if( FAILED(r) )
658 return r;
660 loc = (LOCATION_INFO*) p;
661 if (loc->dwTotalSize < sizeof(LOCATION_INFO))
663 HeapFree( GetProcessHeap(), 0, p );
664 return E_FAIL;
667 /* if there's valid local volume information, load it */
668 if( loc->dwVolTableOfs &&
669 ((loc->dwVolTableOfs + sizeof(LOCAL_VOLUME_INFO)) <= loc->dwTotalSize) )
671 LOCAL_VOLUME_INFO *volume_info;
673 volume_info = (LOCAL_VOLUME_INFO*) &p[loc->dwVolTableOfs];
674 Stream_LoadVolume( volume_info, volume );
677 /* if there's a local path, load it */
678 n = loc->dwLocalPathOfs;
679 if( n && (n < loc->dwTotalSize) )
680 *path = Stream_LoadPath( &p[n], loc->dwTotalSize - n );
682 TRACE("type %ld serial %08lx name %s path %s\n", volume->type,
683 volume->serial, debugstr_w(volume->label), debugstr_w(*path));
685 HeapFree( GetProcessHeap(), 0, p );
686 return S_OK;
690 * The format of the advertised shortcut info seems to be:
692 * Offset Description
693 * ------ -----------
695 * 0 Length of the block (4 bytes, usually 0x314)
696 * 4 tag (dword)
697 * 8 string data in ASCII
698 * 8+0x104 string data in UNICODE
700 * In the original Win32 implementation the buffers are not initialized
701 * to zero, so data trailing the string is random garbage.
703 static HRESULT Stream_LoadAdvertiseInfo( IStream* stm, LPWSTR *str )
705 DWORD size;
706 ULONG count;
707 HRESULT r;
708 EXP_DARWIN_LINK buffer;
710 TRACE("%p\n",stm);
712 r = IStream_Read( stm, &buffer.dbh.cbSize, sizeof (DWORD), &count );
713 if( FAILED( r ) )
714 return r;
716 /* make sure that we read the size of the structure even on error */
717 size = sizeof buffer - sizeof (DWORD);
718 if( buffer.dbh.cbSize != sizeof buffer )
720 ERR("Ooops. This structure is not as expected...\n");
721 return E_FAIL;
724 r = IStream_Read( stm, &buffer.dbh.dwSignature, size, &count );
725 if( FAILED( r ) )
726 return r;
728 if( count != size )
729 return E_FAIL;
731 TRACE("magic %08lx string = %s\n", buffer.dbh.dwSignature, debugstr_w(buffer.szwDarwinID));
733 if( (buffer.dbh.dwSignature&0xffff0000) != 0xa0000000 )
735 ERR("Unknown magic number %08lx in advertised shortcut\n", buffer.dbh.dwSignature);
736 return E_FAIL;
739 *str = HeapAlloc( GetProcessHeap(), 0,
740 (lstrlenW(buffer.szwDarwinID)+1) * sizeof(WCHAR) );
741 lstrcpyW( *str, buffer.szwDarwinID );
743 return S_OK;
746 /************************************************************************
747 * IPersistStream_Load (IPersistStream)
749 static HRESULT WINAPI IPersistStream_fnLoad(
750 IPersistStream* iface,
751 IStream* stm)
753 LINK_HEADER hdr;
754 ULONG dwBytesRead;
755 BOOL unicode;
756 HRESULT r;
757 DWORD zero;
759 IShellLinkImpl *This = impl_from_IPersistStream(iface);
761 TRACE("%p %p\n", This, stm);
763 if( !stm )
764 return STG_E_INVALIDPOINTER;
766 dwBytesRead = 0;
767 r = IStream_Read(stm, &hdr, sizeof(hdr), &dwBytesRead);
768 if( FAILED( r ) )
769 return r;
771 if( dwBytesRead != sizeof(hdr))
772 return E_FAIL;
773 if( hdr.dwSize != sizeof(hdr))
774 return E_FAIL;
775 if( !IsEqualIID(&hdr.MagicGuid, &CLSID_ShellLink) )
776 return E_FAIL;
778 /* free all the old stuff */
779 ILFree(This->pPidl);
780 This->pPidl = NULL;
781 memset( &This->volume, 0, sizeof This->volume );
782 HeapFree(GetProcessHeap(), 0, This->sPath);
783 This->sPath = NULL;
784 HeapFree(GetProcessHeap(), 0, This->sDescription);
785 This->sDescription = NULL;
786 HeapFree(GetProcessHeap(), 0, This->sPathRel);
787 This->sPathRel = NULL;
788 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
789 This->sWorkDir = NULL;
790 HeapFree(GetProcessHeap(), 0, This->sArgs);
791 This->sArgs = NULL;
792 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
793 This->sIcoPath = NULL;
794 HeapFree(GetProcessHeap(), 0, This->sProduct);
795 This->sProduct = NULL;
796 HeapFree(GetProcessHeap(), 0, This->sComponent);
797 This->sComponent = NULL;
799 This->wHotKey = (WORD)hdr.wHotKey;
800 This->iIcoNdx = hdr.nIcon;
801 FileTimeToSystemTime (&hdr.Time1, &This->time1);
802 FileTimeToSystemTime (&hdr.Time2, &This->time2);
803 FileTimeToSystemTime (&hdr.Time3, &This->time3);
804 if (TRACE_ON(shell))
806 WCHAR sTemp[MAX_PATH];
807 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time1,
808 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
809 TRACE("-- time1: %s\n", debugstr_w(sTemp) );
810 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time2,
811 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
812 TRACE("-- time2: %s\n", debugstr_w(sTemp) );
813 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE, &This->time3,
814 NULL, sTemp, sizeof(sTemp)/sizeof(*sTemp));
815 TRACE("-- time3: %s\n", debugstr_w(sTemp) );
818 /* load all the new stuff */
819 if( hdr.dwFlags & SLDF_HAS_ID_LIST )
821 r = ILLoadFromStream( stm, &This->pPidl );
822 if( FAILED( r ) )
823 return r;
825 pdump(This->pPidl);
827 /* load the location information */
828 if( hdr.dwFlags & SLDF_HAS_LINK_INFO )
829 r = Stream_LoadLocation( stm, &This->volume, &This->sPath );
830 if( FAILED( r ) )
831 goto end;
833 unicode = hdr.dwFlags & SLDF_UNICODE;
834 if( hdr.dwFlags & SLDF_HAS_NAME )
836 r = Stream_LoadString( stm, unicode, &This->sDescription );
837 TRACE("Description -> %s\n",debugstr_w(This->sDescription));
839 if( FAILED( r ) )
840 goto end;
842 if( hdr.dwFlags & SLDF_HAS_RELPATH )
844 r = Stream_LoadString( stm, unicode, &This->sPathRel );
845 TRACE("Relative Path-> %s\n",debugstr_w(This->sPathRel));
847 if( FAILED( r ) )
848 goto end;
850 if( hdr.dwFlags & SLDF_HAS_WORKINGDIR )
852 r = Stream_LoadString( stm, unicode, &This->sWorkDir );
853 TRACE("Working Dir -> %s\n",debugstr_w(This->sWorkDir));
855 if( FAILED( r ) )
856 goto end;
858 if( hdr.dwFlags & SLDF_HAS_ARGS )
860 r = Stream_LoadString( stm, unicode, &This->sArgs );
861 TRACE("Working Dir -> %s\n",debugstr_w(This->sArgs));
863 if( FAILED( r ) )
864 goto end;
866 if( hdr.dwFlags & SLDF_HAS_ICONLOCATION )
868 r = Stream_LoadString( stm, unicode, &This->sIcoPath );
869 TRACE("Icon file -> %s\n",debugstr_w(This->sIcoPath));
871 if( FAILED( r ) )
872 goto end;
874 if( hdr.dwFlags & SLDF_HAS_LOGO3ID )
876 r = Stream_LoadAdvertiseInfo( stm, &This->sProduct );
877 TRACE("Product -> %s\n",debugstr_w(This->sProduct));
879 if( FAILED( r ) )
880 goto end;
882 if( hdr.dwFlags & SLDF_HAS_DARWINID )
884 r = Stream_LoadAdvertiseInfo( stm, &This->sComponent );
885 TRACE("Component -> %s\n",debugstr_w(This->sComponent));
887 if( FAILED( r ) )
888 goto end;
890 r = IStream_Read(stm, &zero, sizeof zero, &dwBytesRead);
891 if( FAILED( r ) || zero || dwBytesRead != sizeof zero )
892 ERR("Last word was not zero\n");
894 TRACE("OK\n");
896 pdump (This->pPidl);
898 return S_OK;
899 end:
900 return r;
903 /************************************************************************
904 * Stream_WriteString
906 * Helper function for IPersistStream_Save. Writes a unicode string
907 * with terminating nul byte to a stream, preceded by the its length.
909 static HRESULT Stream_WriteString( IStream* stm, LPCWSTR str )
911 USHORT len = lstrlenW( str ) + 1;
912 DWORD count;
913 HRESULT r;
915 r = IStream_Write( stm, &len, sizeof(len), &count );
916 if( FAILED( r ) )
917 return r;
919 len *= sizeof(WCHAR);
921 r = IStream_Write( stm, str, len, &count );
922 if( FAILED( r ) )
923 return r;
925 return S_OK;
928 /************************************************************************
929 * Stream_WriteLocationInfo
931 * Writes the location info to a stream
933 * FIXME: One day we might want to write the network volume information
934 * and the final path.
935 * Figure out how Windows deals with unicode paths here.
937 static HRESULT Stream_WriteLocationInfo( IStream* stm, LPCWSTR path,
938 volume_info *volume )
940 DWORD total_size, path_size, volume_info_size, label_size, final_path_size;
941 LOCAL_VOLUME_INFO *vol;
942 LOCATION_INFO *loc;
943 LPSTR szLabel, szPath, szFinalPath;
944 ULONG count = 0;
946 TRACE("%p %s %p\n", stm, debugstr_w(path), volume);
948 /* figure out the size of everything */
949 label_size = WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
950 NULL, 0, NULL, NULL );
951 path_size = WideCharToMultiByte( CP_ACP, 0, path, -1,
952 NULL, 0, NULL, NULL );
953 volume_info_size = sizeof *vol + label_size;
954 final_path_size = 1;
955 total_size = sizeof *loc + volume_info_size + path_size + final_path_size;
957 /* create pointers to everything */
958 loc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, total_size);
959 vol = (LOCAL_VOLUME_INFO*) &loc[1];
960 szLabel = (LPSTR) &vol[1];
961 szPath = &szLabel[label_size];
962 szFinalPath = &szPath[path_size];
964 /* fill in the location information header */
965 loc->dwTotalSize = total_size;
966 loc->dwHeaderSize = sizeof (*loc);
967 loc->dwFlags = 1;
968 loc->dwVolTableOfs = sizeof (*loc);
969 loc->dwLocalPathOfs = sizeof (*loc) + volume_info_size;
970 loc->dwNetworkVolTableOfs = 0;
971 loc->dwFinalPathOfs = sizeof (*loc) + volume_info_size + path_size;
973 /* fill in the volume information */
974 vol->dwSize = volume_info_size;
975 vol->dwType = volume->type;
976 vol->dwVolSerial = volume->serial;
977 vol->dwVolLabelOfs = sizeof (*vol);
979 /* copy in the strings */
980 WideCharToMultiByte( CP_ACP, 0, volume->label, -1,
981 szLabel, label_size, NULL, NULL );
982 WideCharToMultiByte( CP_ACP, 0, path, -1,
983 szPath, path_size, NULL, NULL );
984 szFinalPath[0] = 0;
986 return IStream_Write( stm, loc, total_size, &count );
989 static HRESULT Stream_WriteAdvertiseInfo( IStream* stm, LPCWSTR string, DWORD magic )
991 ULONG count;
992 EXP_DARWIN_LINK buffer;
994 TRACE("%p\n",stm);
996 memset( &buffer, 0, sizeof buffer );
997 buffer.dbh.cbSize = sizeof buffer;
998 buffer.dbh.dwSignature = magic;
999 lstrcpynW( buffer.szwDarwinID, string, MAX_PATH );
1000 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer.szDarwinID, MAX_PATH, NULL, NULL );
1002 return IStream_Write( stm, &buffer, buffer.dbh.cbSize, &count );
1005 /************************************************************************
1006 * IPersistStream_Save (IPersistStream)
1008 * FIXME: makes assumptions about byte order
1010 static HRESULT WINAPI IPersistStream_fnSave(
1011 IPersistStream* iface,
1012 IStream* stm,
1013 BOOL fClearDirty)
1015 static const WCHAR wOpen[] = {'o','p','e','n',0};
1017 LINK_HEADER header;
1018 WCHAR exePath[MAX_PATH];
1019 ULONG count;
1020 DWORD zero;
1021 HRESULT r;
1023 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1025 TRACE("%p %p %x\n", This, stm, fClearDirty);
1027 *exePath = '\0';
1029 if (This->sPath)
1031 SHELL_FindExecutable(NULL, This->sPath, wOpen, exePath, MAX_PATH,
1032 NULL, NULL, NULL, NULL);
1034 * windows can create lnk files to executables that do not exist yet
1035 * so if the executable does not exist the just trust the path they
1036 * gave us
1038 if (!*exePath) lstrcpyW(exePath,This->sPath);
1041 memset(&header, 0, sizeof(header));
1042 header.dwSize = sizeof(header);
1043 header.fStartup = This->iShowCmd;
1044 memcpy(&header.MagicGuid, &CLSID_ShellLink, sizeof(header.MagicGuid) );
1046 header.wHotKey = This->wHotKey;
1047 header.nIcon = This->iIcoNdx;
1048 header.dwFlags = SLDF_UNICODE; /* strings are in unicode */
1049 if( This->pPidl )
1050 header.dwFlags |= SLDF_HAS_ID_LIST;
1051 if( This->sPath )
1052 header.dwFlags |= SLDF_HAS_LINK_INFO;
1053 if( This->sDescription )
1054 header.dwFlags |= SLDF_HAS_NAME;
1055 if( This->sWorkDir )
1056 header.dwFlags |= SLDF_HAS_WORKINGDIR;
1057 if( This->sArgs )
1058 header.dwFlags |= SLDF_HAS_ARGS;
1059 if( This->sIcoPath )
1060 header.dwFlags |= SLDF_HAS_ICONLOCATION;
1061 if( This->sProduct )
1062 header.dwFlags |= SLDF_HAS_LOGO3ID;
1063 if( This->sComponent )
1064 header.dwFlags |= SLDF_HAS_DARWINID;
1066 SystemTimeToFileTime ( &This->time1, &header.Time1 );
1067 SystemTimeToFileTime ( &This->time2, &header.Time2 );
1068 SystemTimeToFileTime ( &This->time3, &header.Time3 );
1070 /* write the Shortcut header */
1071 r = IStream_Write( stm, &header, sizeof(header), &count );
1072 if( FAILED( r ) )
1074 ERR("Write failed at %d\n",__LINE__);
1075 return r;
1078 TRACE("Writing pidl \n");
1080 /* write the PIDL to the shortcut */
1081 if( This->pPidl )
1083 r = ILSaveToStream( stm, This->pPidl );
1084 if( FAILED( r ) )
1086 ERR("Failed to write PIDL at %d\n",__LINE__);
1087 return r;
1091 if( This->sPath )
1092 Stream_WriteLocationInfo( stm, exePath, &This->volume );
1094 if( This->sDescription )
1095 r = Stream_WriteString( stm, This->sDescription );
1097 if( This->sPathRel )
1098 r = Stream_WriteString( stm, This->sPathRel );
1100 if( This->sWorkDir )
1101 r = Stream_WriteString( stm, This->sWorkDir );
1103 if( This->sArgs )
1104 r = Stream_WriteString( stm, This->sArgs );
1106 if( This->sIcoPath )
1107 r = Stream_WriteString( stm, This->sIcoPath );
1109 if( This->sProduct )
1110 r = Stream_WriteAdvertiseInfo( stm, This->sProduct, EXP_SZ_ICON_SIG );
1112 if( This->sComponent )
1113 r = Stream_WriteAdvertiseInfo( stm, This->sComponent, EXP_DARWIN_ID_SIG );
1115 /* the last field is a single zero dword */
1116 zero = 0;
1117 r = IStream_Write( stm, &zero, sizeof zero, &count );
1119 return S_OK;
1122 /************************************************************************
1123 * IPersistStream_GetSizeMax (IPersistStream)
1125 static HRESULT WINAPI IPersistStream_fnGetSizeMax(
1126 IPersistStream* iface,
1127 ULARGE_INTEGER* pcbSize)
1129 IShellLinkImpl *This = impl_from_IPersistStream(iface);
1131 TRACE("(%p)\n", This);
1133 return E_NOTIMPL;
1136 static const IPersistStreamVtbl psvt =
1138 IPersistStream_fnQueryInterface,
1139 IPersistStream_fnAddRef,
1140 IPersistStream_fnRelease,
1141 IPersistStream_fnGetClassID,
1142 IPersistStream_fnIsDirty,
1143 IPersistStream_fnLoad,
1144 IPersistStream_fnSave,
1145 IPersistStream_fnGetSizeMax
1148 /**************************************************************************
1149 * IShellLink_Constructor
1151 HRESULT WINAPI IShellLink_Constructor( IUnknown *pUnkOuter,
1152 REFIID riid, LPVOID *ppv )
1154 IShellLinkImpl * sl;
1156 TRACE("unkOut=%p riid=%s\n",pUnkOuter, debugstr_guid(riid));
1158 *ppv = NULL;
1160 if (pUnkOuter)
1161 return CLASS_E_NOAGGREGATION;
1162 sl = LocalAlloc(LMEM_ZEROINIT,sizeof(IShellLinkImpl));
1163 if (!sl)
1164 return E_OUTOFMEMORY;
1166 sl->ref = 1;
1167 sl->lpVtbl = &slvt;
1168 sl->lpvtblw = &slvtw;
1169 sl->lpvtblPersistFile = &pfvt;
1170 sl->lpvtblPersistStream = &psvt;
1171 sl->lpvtblShellLinkDataList = &dlvt;
1172 sl->lpvtblShellExtInit = &eivt;
1173 sl->lpvtblContextMenu = &cmvt;
1174 sl->iShowCmd = SW_SHOWNORMAL;
1175 sl->bDirty = FALSE;
1177 TRACE("(%p)->()\n",sl);
1179 if (IsEqualIID(riid, &IID_IUnknown) ||
1180 IsEqualIID(riid, &IID_IShellLinkA))
1181 *ppv = sl;
1182 else if (IsEqualIID(riid, &IID_IShellLinkW))
1183 *ppv = &(sl->lpvtblw);
1184 else {
1185 LocalFree((HLOCAL)sl);
1186 ERR("E_NOINTERFACE\n");
1187 return E_NOINTERFACE;
1190 return S_OK;
1194 static BOOL SHELL_ExistsFileW(LPCWSTR path)
1196 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(path))
1197 return FALSE;
1198 return TRUE;
1201 /**************************************************************************
1202 * ShellLink_UpdatePath
1203 * update absolute path in sPath using relative path in sPathRel
1205 static HRESULT ShellLink_UpdatePath(LPWSTR sPathRel, LPCWSTR path, LPCWSTR sWorkDir, LPWSTR* psPath)
1207 if (!path || !psPath)
1208 return E_INVALIDARG;
1210 if (!*psPath && sPathRel) {
1211 WCHAR buffer[2*MAX_PATH], abs_path[2*MAX_PATH];
1212 LPWSTR final = NULL;
1214 /* first try if [directory of link file] + [relative path] finds an existing file */
1216 GetFullPathNameW( path, MAX_PATH*2, buffer, &final );
1217 if( !final )
1218 final = buffer;
1219 lstrcpyW(final, sPathRel);
1221 *abs_path = '\0';
1223 if (SHELL_ExistsFileW(buffer)) {
1224 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1225 lstrcpyW(abs_path, buffer);
1226 } else {
1227 /* try if [working directory] + [relative path] finds an existing file */
1228 if (sWorkDir) {
1229 lstrcpyW(buffer, sWorkDir);
1230 lstrcpyW(PathAddBackslashW(buffer), sPathRel);
1232 if (SHELL_ExistsFileW(buffer))
1233 if (!GetFullPathNameW(buffer, MAX_PATH, abs_path, &final))
1234 lstrcpyW(abs_path, buffer);
1238 /* FIXME: This is even not enough - not all shell links can be resolved using this algorithm. */
1239 if (!*abs_path)
1240 lstrcpyW(abs_path, sPathRel);
1242 *psPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(abs_path)+1)*sizeof(WCHAR));
1243 if (!*psPath)
1244 return E_OUTOFMEMORY;
1246 lstrcpyW(*psPath, abs_path);
1249 return S_OK;
1252 /**************************************************************************
1253 * IShellLink_ConstructFromFile
1255 HRESULT WINAPI IShellLink_ConstructFromFile( IUnknown* pUnkOuter, REFIID riid,
1256 LPCITEMIDLIST pidl, LPVOID* ppv)
1258 IShellLinkW* psl;
1260 HRESULT hr = IShellLink_Constructor(NULL, riid, (LPVOID*)&psl);
1262 if (SUCCEEDED(hr)) {
1263 IPersistFile* ppf;
1265 *ppv = NULL;
1267 hr = IShellLinkW_QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
1269 if (SUCCEEDED(hr)) {
1270 WCHAR path[MAX_PATH];
1272 if (SHGetPathFromIDListW(pidl, path))
1273 hr = IPersistFile_Load(ppf, path, 0);
1274 else
1275 hr = E_FAIL;
1277 if (SUCCEEDED(hr))
1278 *ppv = (IUnknown*) psl;
1280 IPersistFile_Release(ppf);
1283 if (!*ppv)
1284 IShellLinkW_Release(psl);
1287 return hr;
1290 /**************************************************************************
1291 * IShellLinkA_QueryInterface
1293 static HRESULT WINAPI IShellLinkA_fnQueryInterface( IShellLinkA * iface, REFIID riid, LPVOID *ppvObj)
1295 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1296 return ShellLink_QueryInterface( This, riid, ppvObj );
1299 /******************************************************************************
1300 * IShellLinkA_AddRef
1302 static ULONG WINAPI IShellLinkA_fnAddRef(IShellLinkA * iface)
1304 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1305 return ShellLink_AddRef( This );
1308 /******************************************************************************
1309 * IShellLinkA_Release
1311 static ULONG WINAPI IShellLinkA_fnRelease(IShellLinkA * iface)
1313 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1314 return ShellLink_Release( This );
1317 static HRESULT WINAPI IShellLinkA_fnGetPath(IShellLinkA * iface, LPSTR pszFile,
1318 INT cchMaxPath, WIN32_FIND_DATAA *pfd, DWORD fFlags)
1320 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1322 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1323 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1325 if (This->sComponent || This->sProduct)
1326 return S_FALSE;
1328 if (cchMaxPath)
1329 pszFile[0] = 0;
1330 if (This->sPath)
1331 WideCharToMultiByte( CP_ACP, 0, This->sPath, -1,
1332 pszFile, cchMaxPath, NULL, NULL);
1334 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1336 return S_OK;
1339 static HRESULT WINAPI IShellLinkA_fnGetIDList(IShellLinkA * iface, LPITEMIDLIST * ppidl)
1341 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1343 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1345 return IShellLinkW_GetIDList((IShellLinkW*)&(This->lpvtblw), ppidl);
1348 static HRESULT WINAPI IShellLinkA_fnSetIDList(IShellLinkA * iface, LPCITEMIDLIST pidl)
1350 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1352 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1354 if (This->pPidl)
1355 ILFree(This->pPidl);
1356 This->pPidl = ILClone (pidl);
1357 This->bDirty = TRUE;
1359 return S_OK;
1362 static HRESULT WINAPI IShellLinkA_fnGetDescription(IShellLinkA * iface, LPSTR pszName,INT cchMaxName)
1364 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1366 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1368 if( cchMaxName )
1369 pszName[0] = 0;
1370 if( This->sDescription )
1371 WideCharToMultiByte( CP_ACP, 0, This->sDescription, -1,
1372 pszName, cchMaxName, NULL, NULL);
1374 return S_OK;
1377 static HRESULT WINAPI IShellLinkA_fnSetDescription(IShellLinkA * iface, LPCSTR pszName)
1379 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1381 TRACE("(%p)->(pName=%s)\n", This, pszName);
1383 HeapFree(GetProcessHeap(), 0, This->sDescription);
1384 This->sDescription = HEAP_strdupAtoW( GetProcessHeap(), 0, pszName);
1385 if ( !This->sDescription )
1386 return E_OUTOFMEMORY;
1388 This->bDirty = TRUE;
1390 return S_OK;
1393 static HRESULT WINAPI IShellLinkA_fnGetWorkingDirectory(IShellLinkA * iface, LPSTR pszDir,INT cchMaxPath)
1395 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1397 TRACE("(%p)->(%p len=%u)\n", This, pszDir, cchMaxPath);
1399 if( cchMaxPath )
1400 pszDir[0] = 0;
1401 if( This->sWorkDir )
1402 WideCharToMultiByte( CP_ACP, 0, This->sWorkDir, -1,
1403 pszDir, cchMaxPath, NULL, NULL);
1405 return S_OK;
1408 static HRESULT WINAPI IShellLinkA_fnSetWorkingDirectory(IShellLinkA * iface, LPCSTR pszDir)
1410 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1412 TRACE("(%p)->(dir=%s)\n",This, pszDir);
1414 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1415 This->sWorkDir = HEAP_strdupAtoW( GetProcessHeap(), 0, pszDir);
1416 if ( !This->sWorkDir )
1417 return E_OUTOFMEMORY;
1419 This->bDirty = TRUE;
1421 return S_OK;
1424 static HRESULT WINAPI IShellLinkA_fnGetArguments(IShellLinkA * iface, LPSTR pszArgs,INT cchMaxPath)
1426 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1428 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1430 if( cchMaxPath )
1431 pszArgs[0] = 0;
1432 if( This->sArgs )
1433 WideCharToMultiByte( CP_ACP, 0, This->sArgs, -1,
1434 pszArgs, cchMaxPath, NULL, NULL);
1436 return S_OK;
1439 static HRESULT WINAPI IShellLinkA_fnSetArguments(IShellLinkA * iface, LPCSTR pszArgs)
1441 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1443 TRACE("(%p)->(args=%s)\n",This, pszArgs);
1445 HeapFree(GetProcessHeap(), 0, This->sArgs);
1446 This->sArgs = HEAP_strdupAtoW( GetProcessHeap(), 0, pszArgs);
1447 if( !This->sArgs )
1448 return E_OUTOFMEMORY;
1450 This->bDirty = TRUE;
1452 return S_OK;
1455 static HRESULT WINAPI IShellLinkA_fnGetHotkey(IShellLinkA * iface, WORD *pwHotkey)
1457 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1459 TRACE("(%p)->(%p)(0x%08x)\n",This, pwHotkey, This->wHotKey);
1461 *pwHotkey = This->wHotKey;
1463 return S_OK;
1466 static HRESULT WINAPI IShellLinkA_fnSetHotkey(IShellLinkA * iface, WORD wHotkey)
1468 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1470 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1472 This->wHotKey = wHotkey;
1473 This->bDirty = TRUE;
1475 return S_OK;
1478 static HRESULT WINAPI IShellLinkA_fnGetShowCmd(IShellLinkA * iface, INT *piShowCmd)
1480 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1482 TRACE("(%p)->(%p)\n",This, piShowCmd);
1483 *piShowCmd = This->iShowCmd;
1484 return S_OK;
1487 static HRESULT WINAPI IShellLinkA_fnSetShowCmd(IShellLinkA * iface, INT iShowCmd)
1489 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1491 TRACE("(%p) %d\n",This, iShowCmd);
1493 This->iShowCmd = iShowCmd;
1494 This->bDirty = TRUE;
1496 return NOERROR;
1499 static HRESULT SHELL_PidlGeticonLocationA(IShellFolder* psf, LPITEMIDLIST pidl, LPSTR pszIconPath, int cchIconPath, int* piIcon)
1501 LPCITEMIDLIST pidlLast;
1503 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1505 if (SUCCEEDED(hr)) {
1506 IExtractIconA* pei;
1508 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, NULL, (LPVOID*)&pei);
1510 if (SUCCEEDED(hr)) {
1511 hr = IExtractIconA_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1513 IExtractIconA_Release(pei);
1516 IShellFolder_Release(psf);
1519 return hr;
1522 static HRESULT WINAPI IShellLinkA_fnGetIconLocation(IShellLinkA * iface, LPSTR pszIconPath,INT cchIconPath,INT *piIcon)
1524 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1526 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1528 pszIconPath[0] = 0;
1529 *piIcon = This->iIcoNdx;
1531 if (This->sIcoPath)
1533 WideCharToMultiByte(CP_ACP, 0, This->sIcoPath, -1, pszIconPath, cchIconPath, NULL, NULL);
1534 return S_OK;
1537 if (This->pPidl || This->sPath)
1539 IShellFolder* pdsk;
1541 HRESULT hr = SHGetDesktopFolder(&pdsk);
1543 if (SUCCEEDED(hr))
1545 /* first look for an icon using the PIDL (if present) */
1546 if (This->pPidl)
1547 hr = SHELL_PidlGeticonLocationA(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1548 else
1549 hr = E_FAIL;
1551 /* if we couldn't find an icon yet, look for it using the file system path */
1552 if (FAILED(hr) && This->sPath)
1554 LPITEMIDLIST pidl;
1556 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1558 if (SUCCEEDED(hr)) {
1559 hr = SHELL_PidlGeticonLocationA(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1561 SHFree(pidl);
1565 IShellFolder_Release(pdsk);
1568 return hr;
1570 return S_OK;
1573 static HRESULT WINAPI IShellLinkA_fnSetIconLocation(IShellLinkA * iface, LPCSTR pszIconPath,INT iIcon)
1575 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1577 TRACE("(%p)->(path=%s iicon=%u)\n",This, pszIconPath, iIcon);
1579 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1580 This->sIcoPath = HEAP_strdupAtoW(GetProcessHeap(), 0, pszIconPath);
1581 if ( !This->sIcoPath )
1582 return E_OUTOFMEMORY;
1584 This->iIcoNdx = iIcon;
1585 This->bDirty = TRUE;
1587 return S_OK;
1590 static HRESULT WINAPI IShellLinkA_fnSetRelativePath(IShellLinkA * iface, LPCSTR pszPathRel, DWORD dwReserved)
1592 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1594 TRACE("(%p)->(path=%s %lx)\n",This, pszPathRel, dwReserved);
1596 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1597 This->sPathRel = HEAP_strdupAtoW(GetProcessHeap(), 0, pszPathRel);
1598 This->bDirty = TRUE;
1600 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1603 static HRESULT WINAPI IShellLinkA_fnResolve(IShellLinkA * iface, HWND hwnd, DWORD fFlags)
1605 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1607 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1609 return IShellLinkW_Resolve( (IShellLinkW*)&(This->lpvtblw), hwnd, fFlags );
1612 static HRESULT WINAPI IShellLinkA_fnSetPath(IShellLinkA * iface, LPCSTR pszFile)
1614 HRESULT r;
1615 LPWSTR str;
1616 IShellLinkImpl *This = (IShellLinkImpl *)iface;
1618 TRACE("(%p)->(path=%s)\n",This, pszFile);
1620 str = HEAP_strdupAtoW(GetProcessHeap(), 0, pszFile);
1621 if( !str )
1622 return E_OUTOFMEMORY;
1624 r = IShellLinkW_SetPath((IShellLinkW*)&(This->lpvtblw), str);
1625 HeapFree( GetProcessHeap(), 0, str );
1627 return r;
1630 /**************************************************************************
1631 * IShellLink Implementation
1634 static const IShellLinkAVtbl slvt =
1636 IShellLinkA_fnQueryInterface,
1637 IShellLinkA_fnAddRef,
1638 IShellLinkA_fnRelease,
1639 IShellLinkA_fnGetPath,
1640 IShellLinkA_fnGetIDList,
1641 IShellLinkA_fnSetIDList,
1642 IShellLinkA_fnGetDescription,
1643 IShellLinkA_fnSetDescription,
1644 IShellLinkA_fnGetWorkingDirectory,
1645 IShellLinkA_fnSetWorkingDirectory,
1646 IShellLinkA_fnGetArguments,
1647 IShellLinkA_fnSetArguments,
1648 IShellLinkA_fnGetHotkey,
1649 IShellLinkA_fnSetHotkey,
1650 IShellLinkA_fnGetShowCmd,
1651 IShellLinkA_fnSetShowCmd,
1652 IShellLinkA_fnGetIconLocation,
1653 IShellLinkA_fnSetIconLocation,
1654 IShellLinkA_fnSetRelativePath,
1655 IShellLinkA_fnResolve,
1656 IShellLinkA_fnSetPath
1660 /**************************************************************************
1661 * IShellLinkW_fnQueryInterface
1663 static HRESULT WINAPI IShellLinkW_fnQueryInterface(
1664 IShellLinkW * iface, REFIID riid, LPVOID *ppvObj)
1666 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1667 return ShellLink_QueryInterface( This, riid, ppvObj );
1670 /******************************************************************************
1671 * IShellLinkW_fnAddRef
1673 static ULONG WINAPI IShellLinkW_fnAddRef(IShellLinkW * iface)
1675 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1676 return ShellLink_AddRef( This );
1679 /******************************************************************************
1680 * IShellLinkW_fnRelease
1682 static ULONG WINAPI IShellLinkW_fnRelease(IShellLinkW * iface)
1684 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1685 return ShellLink_Release( This );
1688 static HRESULT WINAPI IShellLinkW_fnGetPath(IShellLinkW * iface, LPWSTR pszFile,INT cchMaxPath, WIN32_FIND_DATAW *pfd, DWORD fFlags)
1690 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1692 TRACE("(%p)->(pfile=%p len=%u find_data=%p flags=%lu)(%s)\n",
1693 This, pszFile, cchMaxPath, pfd, fFlags, debugstr_w(This->sPath));
1695 if (This->sComponent || This->sProduct)
1696 return S_FALSE;
1698 if (cchMaxPath)
1699 pszFile[0] = 0;
1700 if (This->sPath)
1701 lstrcpynW( pszFile, This->sPath, cchMaxPath );
1703 if (pfd) FIXME("(%p): WIN32_FIND_DATA is not yet filled.\n", This);
1705 return S_OK;
1708 static HRESULT WINAPI IShellLinkW_fnGetIDList(IShellLinkW * iface, LPITEMIDLIST * ppidl)
1710 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1712 TRACE("(%p)->(ppidl=%p)\n",This, ppidl);
1714 if (!This->pPidl)
1715 return S_FALSE;
1716 *ppidl = ILClone(This->pPidl);
1717 return S_OK;
1720 static HRESULT WINAPI IShellLinkW_fnSetIDList(IShellLinkW * iface, LPCITEMIDLIST pidl)
1722 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1724 TRACE("(%p)->(pidl=%p)\n",This, pidl);
1726 if( This->pPidl )
1727 ILFree( This->pPidl );
1728 This->pPidl = ILClone( pidl );
1729 if( !This->pPidl )
1730 return E_FAIL;
1732 This->bDirty = TRUE;
1734 return S_OK;
1737 static HRESULT WINAPI IShellLinkW_fnGetDescription(IShellLinkW * iface, LPWSTR pszName,INT cchMaxName)
1739 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1741 TRACE("(%p)->(%p len=%u)\n",This, pszName, cchMaxName);
1743 pszName[0] = 0;
1744 if( This->sDescription )
1745 lstrcpynW( pszName, This->sDescription, cchMaxName );
1747 return S_OK;
1750 static HRESULT WINAPI IShellLinkW_fnSetDescription(IShellLinkW * iface, LPCWSTR pszName)
1752 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1754 TRACE("(%p)->(desc=%s)\n",This, debugstr_w(pszName));
1756 HeapFree(GetProcessHeap(), 0, This->sDescription);
1757 This->sDescription = HeapAlloc( GetProcessHeap(), 0,
1758 (lstrlenW( pszName )+1)*sizeof(WCHAR) );
1759 if ( !This->sDescription )
1760 return E_OUTOFMEMORY;
1762 lstrcpyW( This->sDescription, pszName );
1763 This->bDirty = TRUE;
1765 return S_OK;
1768 static HRESULT WINAPI IShellLinkW_fnGetWorkingDirectory(IShellLinkW * iface, LPWSTR pszDir,INT cchMaxPath)
1770 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1772 TRACE("(%p)->(%p len %u)\n", This, pszDir, cchMaxPath);
1774 if( cchMaxPath )
1775 pszDir[0] = 0;
1776 if( This->sWorkDir )
1777 lstrcpynW( pszDir, This->sWorkDir, cchMaxPath );
1779 return S_OK;
1782 static HRESULT WINAPI IShellLinkW_fnSetWorkingDirectory(IShellLinkW * iface, LPCWSTR pszDir)
1784 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1786 TRACE("(%p)->(dir=%s)\n",This, debugstr_w(pszDir));
1788 HeapFree(GetProcessHeap(), 0, This->sWorkDir);
1789 This->sWorkDir = HeapAlloc( GetProcessHeap(), 0,
1790 (lstrlenW( pszDir )+1)*sizeof (WCHAR) );
1791 if ( !This->sWorkDir )
1792 return E_OUTOFMEMORY;
1793 lstrcpyW( This->sWorkDir, pszDir );
1794 This->bDirty = TRUE;
1796 return S_OK;
1799 static HRESULT WINAPI IShellLinkW_fnGetArguments(IShellLinkW * iface, LPWSTR pszArgs,INT cchMaxPath)
1801 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1803 TRACE("(%p)->(%p len=%u)\n", This, pszArgs, cchMaxPath);
1805 if( cchMaxPath )
1806 pszArgs[0] = 0;
1807 if( This->sArgs )
1808 lstrcpynW( pszArgs, This->sArgs, cchMaxPath );
1810 return NOERROR;
1813 static HRESULT WINAPI IShellLinkW_fnSetArguments(IShellLinkW * iface, LPCWSTR pszArgs)
1815 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1817 TRACE("(%p)->(args=%s)\n",This, debugstr_w(pszArgs));
1819 HeapFree(GetProcessHeap(), 0, This->sArgs);
1820 This->sArgs = HeapAlloc( GetProcessHeap(), 0,
1821 (lstrlenW( pszArgs )+1)*sizeof (WCHAR) );
1822 if ( !This->sArgs )
1823 return E_OUTOFMEMORY;
1824 lstrcpyW( This->sArgs, pszArgs );
1825 This->bDirty = TRUE;
1827 return S_OK;
1830 static HRESULT WINAPI IShellLinkW_fnGetHotkey(IShellLinkW * iface, WORD *pwHotkey)
1832 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1834 TRACE("(%p)->(%p)\n",This, pwHotkey);
1836 *pwHotkey=This->wHotKey;
1838 return S_OK;
1841 static HRESULT WINAPI IShellLinkW_fnSetHotkey(IShellLinkW * iface, WORD wHotkey)
1843 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1845 TRACE("(%p)->(hotkey=%x)\n",This, wHotkey);
1847 This->wHotKey = wHotkey;
1848 This->bDirty = TRUE;
1850 return S_OK;
1853 static HRESULT WINAPI IShellLinkW_fnGetShowCmd(IShellLinkW * iface, INT *piShowCmd)
1855 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1857 TRACE("(%p)->(%p)\n",This, piShowCmd);
1859 *piShowCmd = This->iShowCmd;
1861 return S_OK;
1864 static HRESULT WINAPI IShellLinkW_fnSetShowCmd(IShellLinkW * iface, INT iShowCmd)
1866 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1868 This->iShowCmd = iShowCmd;
1869 This->bDirty = TRUE;
1871 return S_OK;
1874 static HRESULT SHELL_PidlGeticonLocationW(IShellFolder* psf, LPITEMIDLIST pidl, LPWSTR pszIconPath, int cchIconPath, int* piIcon)
1876 LPCITEMIDLIST pidlLast;
1878 HRESULT hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psf, &pidlLast);
1880 if (SUCCEEDED(hr)) {
1881 IExtractIconW* pei;
1883 hr = IShellFolder_GetUIObjectOf(psf, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW, NULL, (LPVOID*)&pei);
1885 if (SUCCEEDED(hr)) {
1886 hr = IExtractIconW_GetIconLocation(pei, 0, pszIconPath, MAX_PATH, piIcon, NULL);
1888 IExtractIconW_Release(pei);
1891 IShellFolder_Release(psf);
1894 return hr;
1897 static HRESULT WINAPI IShellLinkW_fnGetIconLocation(IShellLinkW * iface, LPWSTR pszIconPath,INT cchIconPath,INT *piIcon)
1899 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1901 TRACE("(%p)->(%p len=%u iicon=%p)\n", This, pszIconPath, cchIconPath, piIcon);
1903 pszIconPath[0] = 0;
1904 *piIcon = This->iIcoNdx;
1906 if (This->sIcoPath)
1908 lstrcpynW(pszIconPath, This->sIcoPath, cchIconPath);
1909 return S_OK;
1912 if (This->pPidl || This->sPath)
1914 IShellFolder* pdsk;
1916 HRESULT hr = SHGetDesktopFolder(&pdsk);
1918 if (SUCCEEDED(hr))
1920 /* first look for an icon using the PIDL (if present) */
1921 if (This->pPidl)
1922 hr = SHELL_PidlGeticonLocationW(pdsk, This->pPidl, pszIconPath, cchIconPath, piIcon);
1923 else
1924 hr = E_FAIL;
1926 /* if we couldn't find an icon yet, look for it using the file system path */
1927 if (FAILED(hr) && This->sPath)
1929 LPITEMIDLIST pidl;
1931 hr = IShellFolder_ParseDisplayName(pdsk, 0, NULL, This->sPath, NULL, &pidl, NULL);
1933 if (SUCCEEDED(hr))
1935 hr = SHELL_PidlGeticonLocationW(pdsk, pidl, pszIconPath, cchIconPath, piIcon);
1937 SHFree(pidl);
1941 IShellFolder_Release(pdsk);
1943 return hr;
1945 return S_OK;
1948 static HRESULT WINAPI IShellLinkW_fnSetIconLocation(IShellLinkW * iface, LPCWSTR pszIconPath,INT iIcon)
1950 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1952 TRACE("(%p)->(path=%s iicon=%u)\n",This, debugstr_w(pszIconPath), iIcon);
1954 HeapFree(GetProcessHeap(), 0, This->sIcoPath);
1955 This->sIcoPath = HeapAlloc( GetProcessHeap(), 0,
1956 (lstrlenW( pszIconPath )+1)*sizeof (WCHAR) );
1957 if ( !This->sIcoPath )
1958 return E_OUTOFMEMORY;
1959 lstrcpyW( This->sIcoPath, pszIconPath );
1961 This->iIcoNdx = iIcon;
1962 This->bDirty = TRUE;
1964 return S_OK;
1967 static HRESULT WINAPI IShellLinkW_fnSetRelativePath(IShellLinkW * iface, LPCWSTR pszPathRel, DWORD dwReserved)
1969 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1971 TRACE("(%p)->(path=%s %lx)\n",This, debugstr_w(pszPathRel), dwReserved);
1973 HeapFree(GetProcessHeap(), 0, This->sPathRel);
1974 This->sPathRel = HeapAlloc( GetProcessHeap(), 0,
1975 (lstrlenW( pszPathRel )+1) * sizeof (WCHAR) );
1976 if ( !This->sPathRel )
1977 return E_OUTOFMEMORY;
1978 lstrcpyW( This->sPathRel, pszPathRel );
1979 This->bDirty = TRUE;
1981 return ShellLink_UpdatePath(This->sPathRel, This->sPath, This->sWorkDir, &This->sPath);
1984 static HRESULT WINAPI IShellLinkW_fnResolve(IShellLinkW * iface, HWND hwnd, DWORD fFlags)
1986 HRESULT hr = S_OK;
1987 BOOL bSuccess;
1989 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
1991 TRACE("(%p)->(hwnd=%p flags=%lx)\n",This, hwnd, fFlags);
1993 /*FIXME: use IResolveShellLink interface */
1995 if (!This->sPath && This->pPidl) {
1996 WCHAR buffer[MAX_PATH];
1998 bSuccess = SHGetPathFromIDListW(This->pPidl, buffer);
2000 if (bSuccess && *buffer) {
2001 This->sPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(buffer)+1)*sizeof(WCHAR));
2002 if (!This->sPath)
2003 return E_OUTOFMEMORY;
2005 lstrcpyW(This->sPath, buffer);
2007 This->bDirty = TRUE;
2008 } else
2009 hr = S_OK; /* don't report an error occurred while just caching information */
2012 if (!This->sIcoPath && This->sPath) {
2013 This->sIcoPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(This->sPath)+1)*sizeof(WCHAR));
2014 if (!This->sIcoPath)
2015 return E_OUTOFMEMORY;
2017 lstrcpyW(This->sIcoPath, This->sPath);
2018 This->iIcoNdx = 0;
2020 This->bDirty = TRUE;
2023 return hr;
2026 static LPWSTR ShellLink_GetAdvertisedArg(LPCWSTR str)
2028 LPWSTR ret;
2029 LPCWSTR p;
2030 DWORD len;
2032 if( !str )
2033 return NULL;
2035 p = strchrW( str, ':' );
2036 if( !p )
2037 return NULL;
2038 len = p - str;
2039 ret = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
2040 if( !ret )
2041 return ret;
2042 memcpy( ret, str, sizeof(WCHAR)*len );
2043 ret[len] = 0;
2044 return ret;
2047 static HRESULT ShellLink_SetAdvertiseInfo(IShellLinkImpl *This, LPCWSTR str)
2049 LPCWSTR szComponent = NULL, szProduct = NULL, p;
2050 WCHAR szGuid[39];
2051 HRESULT r;
2052 GUID guid;
2053 int len;
2055 while( str[0] )
2057 /* each segment must start with two colons */
2058 if( str[0] != ':' || str[1] != ':' )
2059 return E_FAIL;
2061 /* the last segment is just two colons */
2062 if( !str[2] )
2063 break;
2064 str += 2;
2066 /* there must be a colon straight after a guid */
2067 p = strchrW( str, ':' );
2068 if( !p )
2069 return E_FAIL;
2070 len = p - str;
2071 if( len != 38 )
2072 return E_FAIL;
2074 /* get the guid, and check it's validly formatted */
2075 memcpy( szGuid, str, sizeof(WCHAR)*len );
2076 szGuid[len] = 0;
2077 r = CLSIDFromString( szGuid, &guid );
2078 if( r != S_OK )
2079 return r;
2080 str = p + 1;
2082 /* match it up to a guid that we care about */
2083 if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutComponent ) && !szComponent )
2084 szComponent = str;
2085 else if( IsEqualGUID( &guid, &SHELL32_AdvtShortcutProduct ) && !szProduct )
2086 szProduct = str;
2087 else
2088 return E_FAIL;
2090 /* skip to the next field */
2091 str = strchrW( str, ':' );
2092 if( !str )
2093 return E_FAIL;
2096 /* we have to have a component for an advertised shortcut */
2097 if( !szComponent )
2098 return E_FAIL;
2100 This->sComponent = ShellLink_GetAdvertisedArg( szComponent );
2101 This->sProduct = ShellLink_GetAdvertisedArg( szProduct );
2103 TRACE("Component = %s\n", debugstr_w(This->sComponent));
2104 TRACE("Product = %s\n", debugstr_w(This->sProduct));
2106 return S_OK;
2109 static BOOL ShellLink_GetVolumeInfo(LPWSTR path, volume_info *volume)
2111 const int label_sz = sizeof volume->label/sizeof volume->label[0];
2112 WCHAR drive[4] = { path[0], ':', '\\', 0 };
2113 BOOL r;
2115 volume->type = GetDriveTypeW(drive);
2116 r = GetVolumeInformationW(drive, volume->label, label_sz,
2117 &volume->serial, NULL, NULL, NULL, 0);
2118 TRACE("r = %d type %ld serial %08lx name %s\n", r,
2119 volume->type, volume->serial, debugstr_w(volume->label));
2120 return r;
2123 static HRESULT WINAPI IShellLinkW_fnSetPath(IShellLinkW * iface, LPCWSTR pszFile)
2125 IShellLinkImpl *This = impl_from_IShellLinkW(iface);
2126 WCHAR buffer[MAX_PATH];
2127 LPWSTR fname;
2128 HRESULT hr = S_OK;
2130 TRACE("(%p)->(path=%s)\n",This, debugstr_w(pszFile));
2132 HeapFree(GetProcessHeap(), 0, This->sPath);
2133 This->sPath = NULL;
2135 HeapFree(GetProcessHeap(), 0, This->sComponent);
2136 This->sComponent = NULL;
2138 if (This->pPidl)
2139 ILFree(This->pPidl);
2140 This->pPidl = NULL;
2142 if (S_OK != ShellLink_SetAdvertiseInfo( This, pszFile ))
2144 if (*pszFile == '\0')
2145 *buffer = '\0';
2146 else if (!GetFullPathNameW(pszFile, MAX_PATH, buffer, &fname))
2147 return E_FAIL;
2148 else if(!PathFileExistsW(buffer))
2149 hr = S_FALSE;
2151 This->pPidl = SHSimpleIDListFromPathW(pszFile);
2152 ShellLink_GetVolumeInfo(buffer, &This->volume);
2154 This->sPath = HeapAlloc( GetProcessHeap(), 0,
2155 (lstrlenW( buffer )+1) * sizeof (WCHAR) );
2156 if (!This->sPath)
2157 return E_OUTOFMEMORY;
2159 lstrcpyW(This->sPath, buffer);
2161 This->bDirty = TRUE;
2163 return hr;
2166 /**************************************************************************
2167 * IShellLinkW Implementation
2170 static const IShellLinkWVtbl slvtw =
2172 IShellLinkW_fnQueryInterface,
2173 IShellLinkW_fnAddRef,
2174 IShellLinkW_fnRelease,
2175 IShellLinkW_fnGetPath,
2176 IShellLinkW_fnGetIDList,
2177 IShellLinkW_fnSetIDList,
2178 IShellLinkW_fnGetDescription,
2179 IShellLinkW_fnSetDescription,
2180 IShellLinkW_fnGetWorkingDirectory,
2181 IShellLinkW_fnSetWorkingDirectory,
2182 IShellLinkW_fnGetArguments,
2183 IShellLinkW_fnSetArguments,
2184 IShellLinkW_fnGetHotkey,
2185 IShellLinkW_fnSetHotkey,
2186 IShellLinkW_fnGetShowCmd,
2187 IShellLinkW_fnSetShowCmd,
2188 IShellLinkW_fnGetIconLocation,
2189 IShellLinkW_fnSetIconLocation,
2190 IShellLinkW_fnSetRelativePath,
2191 IShellLinkW_fnResolve,
2192 IShellLinkW_fnSetPath
2195 static HRESULT WINAPI
2196 ShellLink_DataList_QueryInterface( IShellLinkDataList* iface, REFIID riid, void** ppvObject)
2198 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2199 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2202 static ULONG WINAPI
2203 ShellLink_DataList_AddRef( IShellLinkDataList* iface )
2205 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2206 return IShellLinkA_AddRef((IShellLinkA*)This);
2209 static ULONG WINAPI
2210 ShellLink_DataList_Release( IShellLinkDataList* iface )
2212 IShellLinkImpl *This = impl_from_IShellLinkDataList(iface);
2213 return ShellLink_Release( This );
2216 static HRESULT WINAPI
2217 ShellLink_AddDataBlock( IShellLinkDataList* iface, void* pDataBlock )
2219 FIXME("\n");
2220 return E_NOTIMPL;
2223 static HRESULT WINAPI
2224 ShellLink_CopyDataBlock( IShellLinkDataList* iface, DWORD dwSig, void** ppDataBlock )
2226 FIXME("\n");
2227 return E_NOTIMPL;
2230 static HRESULT WINAPI
2231 ShellLink_RemoveDataBlock( IShellLinkDataList* iface, DWORD dwSig )
2233 FIXME("\n");
2234 return E_NOTIMPL;
2237 static HRESULT WINAPI
2238 ShellLink_GetFlags( IShellLinkDataList* iface, DWORD* pdwFlags )
2240 FIXME("\n");
2241 return E_NOTIMPL;
2244 static HRESULT WINAPI
2245 ShellLink_SetFlags( IShellLinkDataList* iface, DWORD dwFlags )
2247 FIXME("\n");
2248 return E_NOTIMPL;
2251 static const IShellLinkDataListVtbl dlvt =
2253 ShellLink_DataList_QueryInterface,
2254 ShellLink_DataList_AddRef,
2255 ShellLink_DataList_Release,
2256 ShellLink_AddDataBlock,
2257 ShellLink_CopyDataBlock,
2258 ShellLink_RemoveDataBlock,
2259 ShellLink_GetFlags,
2260 ShellLink_SetFlags
2263 static HRESULT WINAPI
2264 ShellLink_ExtInit_QueryInterface( IShellExtInit* iface, REFIID riid, void** ppvObject )
2266 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2267 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2270 static ULONG WINAPI
2271 ShellLink_ExtInit_AddRef( IShellExtInit* iface )
2273 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2274 return IShellLinkA_AddRef((IShellLinkA*)This);
2277 static ULONG WINAPI
2278 ShellLink_ExtInit_Release( IShellExtInit* iface )
2280 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2281 return ShellLink_Release( This );
2284 /**************************************************************************
2285 * ShellLink implementation of IShellExtInit::Initialize()
2287 * Loads the shelllink from the dataobject the shell is pointing to.
2289 static HRESULT WINAPI
2290 ShellLink_ExtInit_Initialize( IShellExtInit* iface, LPCITEMIDLIST pidlFolder,
2291 IDataObject *pdtobj, HKEY hkeyProgID )
2293 IShellLinkImpl *This = impl_from_IShellExtInit(iface);
2294 FORMATETC format;
2295 STGMEDIUM stgm;
2296 UINT count;
2297 HRESULT r = E_FAIL;
2299 TRACE("%p %p %p %p\n", This, pidlFolder, pdtobj, hkeyProgID );
2301 if( !pdtobj )
2302 return r;
2304 format.cfFormat = CF_HDROP;
2305 format.ptd = NULL;
2306 format.dwAspect = DVASPECT_CONTENT;
2307 format.lindex = -1;
2308 format.tymed = TYMED_HGLOBAL;
2310 if( FAILED( IDataObject_GetData( pdtobj, &format, &stgm ) ) )
2311 return r;
2313 count = DragQueryFileW( stgm.u.hGlobal, -1, NULL, 0 );
2314 if( count == 1 )
2316 LPWSTR path;
2318 count = DragQueryFileW( stgm.u.hGlobal, 0, NULL, 0 );
2319 count++;
2320 path = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
2321 if( path )
2323 IPersistFile *pf = (IPersistFile*) &This->lpvtblPersistFile;
2325 count = DragQueryFileW( stgm.u.hGlobal, 0, path, count );
2326 r = IPersistFile_Load( pf, path, 0 );
2327 HeapFree( GetProcessHeap(), 0, path );
2330 ReleaseStgMedium( &stgm );
2332 return r;
2335 static const IShellExtInitVtbl eivt =
2337 ShellLink_ExtInit_QueryInterface,
2338 ShellLink_ExtInit_AddRef,
2339 ShellLink_ExtInit_Release,
2340 ShellLink_ExtInit_Initialize
2343 static HRESULT WINAPI
2344 ShellLink_ContextMenu_QueryInterface( IContextMenu* iface, REFIID riid, void** ppvObject )
2346 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2347 return IShellLinkA_QueryInterface((IShellLinkA*)This, riid, ppvObject);
2350 static ULONG WINAPI
2351 ShellLink_ContextMenu_AddRef( IContextMenu* iface )
2353 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2354 return IShellLinkA_AddRef((IShellLinkA*)This);
2357 static ULONG WINAPI
2358 ShellLink_ContextMenu_Release( IContextMenu* iface )
2360 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2361 return ShellLink_Release( This );
2364 static HRESULT WINAPI
2365 ShellLink_QueryContextMenu( IContextMenu* iface, HMENU hmenu, UINT indexMenu,
2366 UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
2368 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2370 FIXME("%p %p %u %u %u %u\n", This,
2371 hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags );
2373 return E_NOTIMPL;
2376 static HRESULT WINAPI
2377 ShellLink_InvokeCommand( IContextMenu* iface, LPCMINVOKECOMMANDINFO lpici )
2379 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2381 FIXME("%p %p\n", This, lpici );
2383 return E_NOTIMPL;
2386 static HRESULT WINAPI
2387 ShellLink_GetCommandString( IContextMenu* iface, UINT idCmd, UINT uType,
2388 UINT* pwReserved, LPSTR pszName, UINT cchMax )
2390 IShellLinkImpl *This = impl_from_IContextMenu(iface);
2392 FIXME("%p %u %u %p %p %u\n", This,
2393 idCmd, uType, pwReserved, pszName, cchMax );
2395 return E_NOTIMPL;
2398 static const IContextMenuVtbl cmvt =
2400 ShellLink_ContextMenu_QueryInterface,
2401 ShellLink_ContextMenu_AddRef,
2402 ShellLink_ContextMenu_Release,
2403 ShellLink_QueryContextMenu,
2404 ShellLink_InvokeCommand,
2405 ShellLink_GetCommandString