2 * SHLWAPI IStream functions
4 * Copyright 2002 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
31 #define NO_SHLWAPI_REG
32 #define NO_SHLWAPI_PATH
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
38 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
39 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
40 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
42 /* Layout of ISHFileStream object */
45 const IStreamVtbl
*lpVtbl
;
54 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
57 /**************************************************************************
58 * IStream_fnQueryInterface
60 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
62 ISHFileStream
*This
= (ISHFileStream
*)iface
;
64 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
68 if(IsEqualIID(riid
, &IID_IUnknown
) ||
69 IsEqualIID(riid
, &IID_IStream
))
72 IStream_AddRef(iface
);
78 /**************************************************************************
81 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
83 ISHFileStream
*This
= (ISHFileStream
*)iface
;
84 ULONG refCount
= InterlockedIncrement(&This
->ref
);
86 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
91 /**************************************************************************
94 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
96 ISHFileStream
*This
= (ISHFileStream
*)iface
;
97 ULONG refCount
= InterlockedDecrement(&This
->ref
);
99 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
103 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
104 LocalFree((HLOCAL
)This
->lpszPath
);
105 CloseHandle(This
->hFile
);
106 HeapFree(GetProcessHeap(), 0, This
);
112 /**************************************************************************
115 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
117 ISHFileStream
*This
= (ISHFileStream
*)iface
;
120 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbRead
);
122 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
124 WARN("error %d reading file\n", GetLastError());
132 /**************************************************************************
135 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
137 ISHFileStream
*This
= (ISHFileStream
*)iface
;
140 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbWritten
);
142 switch (STGM_ACCESS_MODE(This
->dwMode
))
148 return STG_E_ACCESSDENIED
;
151 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
152 return HRESULT_FROM_WIN32(GetLastError());
155 *pcbWritten
= dwWritten
;
159 /**************************************************************************
162 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
163 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
165 ISHFileStream
*This
= (ISHFileStream
*)iface
;
168 TRACE("(%p,%d,%d,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
170 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
171 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
172 if( dwPos
== INVALID_SET_FILE_POINTER
)
173 return HRESULT_FROM_WIN32(GetLastError());
177 pNewPos
->u
.HighPart
= 0;
178 pNewPos
->u
.LowPart
= dwPos
;
183 /**************************************************************************
186 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
188 ISHFileStream
*This
= (ISHFileStream
*)iface
;
190 TRACE("(%p,%d)\n", This
, libNewSize
.u
.LowPart
);
192 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
193 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
196 if( ! SetEndOfFile( This
->hFile
) )
202 /**************************************************************************
205 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
206 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
208 ISHFileStream
*This
= (ISHFileStream
*)iface
;
213 TRACE("(%p,%p,%d,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
216 pcbRead
->QuadPart
= 0;
218 pcbWritten
->QuadPart
= 0;
223 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
226 ulSize
= cb
.QuadPart
;
231 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
234 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
236 pcbRead
->QuadPart
+= ulAmt
;
237 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
241 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
243 pcbWritten
->QuadPart
+= ulAmt
;
244 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
252 /**************************************************************************
255 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
257 ISHFileStream
*This
= (ISHFileStream
*)iface
;
259 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
260 /* Currently unbuffered: This function is not needed */
264 /**************************************************************************
267 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
269 ISHFileStream
*This
= (ISHFileStream
*)iface
;
271 TRACE("(%p)\n", This
);
275 /**************************************************************************
276 * IStream_fnLockUnlockRegion
278 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
279 ULARGE_INTEGER cb
, DWORD dwLockType
)
281 ISHFileStream
*This
= (ISHFileStream
*)iface
;
282 TRACE("(%p,%d,%d,%d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
286 /*************************************************************************
289 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
292 ISHFileStream
*This
= (ISHFileStream
*)iface
;
293 BY_HANDLE_FILE_INFORMATION fi
;
296 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
299 hRet
= STG_E_INVALIDPOINTER
;
302 memset(&fi
, 0, sizeof(fi
));
303 GetFileInformationByHandle(This
->hFile
, &fi
);
305 if (grfStatFlag
& STATFLAG_NONAME
)
306 lpStat
->pwcsName
= NULL
;
308 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
309 lpStat
->type
= This
->type
;
310 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
311 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
312 lpStat
->mtime
= fi
.ftLastWriteTime
;
313 lpStat
->ctime
= fi
.ftCreationTime
;
314 lpStat
->atime
= fi
.ftLastAccessTime
;
315 lpStat
->grfMode
= This
->dwMode
;
316 lpStat
->grfLocksSupported
= 0;
317 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
318 lpStat
->grfStateBits
= This
->grfStateBits
;
319 lpStat
->reserved
= 0;
324 /*************************************************************************
327 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
329 ISHFileStream
*This
= (ISHFileStream
*)iface
;
331 TRACE("(%p)\n",This
);
337 static const IStreamVtbl SHLWAPI_fsVTable
=
339 IStream_fnQueryInterface
,
349 IStream_fnLockUnlockRegion
,
350 IStream_fnLockUnlockRegion
,
355 /**************************************************************************
358 * Internal helper: Create and initialise a new file stream object.
360 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
362 ISHFileStream
* fileStream
;
364 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
368 fileStream
->lpVtbl
= &SHLWAPI_fsVTable
;
370 fileStream
->hFile
= hFile
;
371 fileStream
->dwMode
= dwMode
;
372 fileStream
->lpszPath
= StrDupW(lpszPath
);
373 fileStream
->type
= 0; /* FIXME */
374 fileStream
->grfStateBits
= 0; /* FIXME */
376 TRACE ("Returning %p\n", fileStream
);
377 return (IStream
*)fileStream
;
380 /*************************************************************************
381 * SHCreateStreamOnFileEx [SHLWAPI.@]
383 * Create a stream on a file.
386 * lpszPath [I] Path of file to create stream on
387 * dwMode [I] Mode to create stream in
388 * dwAttributes [I] Attributes of the file
389 * bCreate [I] Whether to create the file if it doesn't exist
390 * lpTemplate [I] Reserved, must be NULL
391 * lppStream [O] Destination for created stream
394 * Success: S_OK. lppStream contains the new stream object
395 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
398 * This function is available in Unicode only.
400 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
401 DWORD dwAttributes
, BOOL bCreate
,
402 IStream
*lpTemplate
, IStream
**lppStream
)
404 DWORD dwAccess
, dwShare
, dwCreate
;
407 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
408 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
410 if (!lpszPath
|| !lppStream
|| lpTemplate
)
416 switch (STGM_ACCESS_MODE(dwMode
))
419 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
422 dwAccess
= GENERIC_WRITE
;
425 dwAccess
= GENERIC_READ
;
432 switch (STGM_SHARE_MODE(dwMode
))
435 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
437 case STGM_SHARE_DENY_READ
:
438 dwShare
= FILE_SHARE_WRITE
;
440 case STGM_SHARE_DENY_WRITE
:
441 dwShare
= FILE_SHARE_READ
;
443 case STGM_SHARE_EXCLUSIVE
:
446 case STGM_SHARE_DENY_NONE
:
447 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
453 switch(STGM_CREATE_MODE(dwMode
))
455 case STGM_FAILIFTHERE
:
456 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
459 dwCreate
= CREATE_ALWAYS
;
465 /* Open HANDLE to file */
466 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
469 if(hFile
== INVALID_HANDLE_VALUE
)
470 return HRESULT_FROM_WIN32(GetLastError());
472 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
477 return E_OUTOFMEMORY
;
482 /*************************************************************************
483 * SHCreateStreamOnFileW [SHLWAPI.@]
485 * See SHCreateStreamOnFileA.
487 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
490 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
492 if (!lpszPath
|| !lppStream
)
495 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
498 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
501 /*************************************************************************
502 * SHCreateStreamOnFileA [SHLWAPI.@]
504 * Create a stream on a file.
507 * lpszPath [I] Path of file to create stream on
508 * dwMode [I] Mode to create stream in
509 * lppStream [O] Destination for created IStream object
512 * Success: S_OK. lppStream contains the new IStream object
513 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
515 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
518 WCHAR szPath
[MAX_PATH
];
520 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
523 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
525 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
526 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
529 /*************************************************************************
532 * Call IStream_Read() on a stream.
535 * lpStream [I] IStream object
536 * lpvDest [O] Destination for data read
537 * ulSize [I] Size of data to read
540 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
541 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
542 * number of bytes read does not match.
544 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
549 TRACE("(%p,%p,%d)\n", lpStream
, lpvDest
, ulSize
);
551 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
553 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
558 /*************************************************************************
561 * Determine if a stream has 0 length.
564 * lpStream [I] IStream object
567 * TRUE: If the stream has 0 length
570 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
575 TRACE("(%p)\n", lpStream
);
577 memset(&statstg
, 0, sizeof(statstg
));
579 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
581 if(statstg
.cbSize
.QuadPart
)
582 bRet
= FALSE
; /* Non-Zero */
588 /* Try to read from the stream */
589 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
594 IStream_Seek(lpStream
, zero
, 0, NULL
);
595 bRet
= FALSE
; /* Non-Zero */
601 /*************************************************************************
604 * Call IStream_Write() on a stream.
607 * lpStream [I] IStream object
608 * lpvSrc [I] Source for data to write
609 * ulSize [I] Size of data
612 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
613 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
614 * number of bytes written does not match.
616 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
621 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
623 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
625 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
631 /*************************************************************************
634 * Seek to the start of a stream.
637 * lpStream [I] IStream object
640 * Success: S_OK. The current position within the stream is updated
641 * Failure: An HRESULT error code.
643 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
646 TRACE("(%p)\n", lpStream
);
648 return IStream_Seek(lpStream
, zero
, 0, NULL
);
651 /*************************************************************************
654 * Get the size of a stream.
657 * lpStream [I] IStream object
658 * lpulSize [O] Destination for size
661 * Success: S_OK. lpulSize contains the size of the stream.
662 * Failure: An HRESULT error code.
664 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
669 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
671 memset(&statstg
, 0, sizeof(statstg
));
673 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
675 if (SUCCEEDED(hRet
) && lpulSize
)
676 *lpulSize
= statstg
.cbSize
;