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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
29 #define NO_SHLWAPI_REG
30 #define NO_SHLWAPI_PATH
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /* Layout of ISHFileStream object */
48 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
51 /**************************************************************************
52 * IStream_fnQueryInterface
54 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
56 ICOM_THIS(ISHFileStream, iface);
58 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
62 if(IsEqualIID(riid, &IID_IUnknown) ||
63 IsEqualIID(riid, &IID_IStream))
67 IStream_AddRef((IStream*)*ppvObj);
73 /**************************************************************************
76 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
78 ICOM_THIS(ISHFileStream, iface);
80 TRACE("(%p)\n", This);
81 return InterlockedIncrement(&This->ref);
84 /**************************************************************************
87 static ULONG WINAPI IStream_fnRelease(IStream *iface)
89 ICOM_THIS(ISHFileStream, iface);
92 TRACE("(%p)\n", This);
94 if (!(ulRet = InterlockedDecrement(&This->ref)))
96 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
97 LocalFree((HLOCAL)This->lpszPath);
98 CloseHandle(This->hFile);
99 HeapFree(GetProcessHeap(), 0, This);
104 /**************************************************************************
107 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
109 ICOM_THIS(ISHFileStream, iface);
113 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbRead);
116 hRet = STG_E_INVALIDPOINTER;
117 else if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
119 hRet = (HRESULT)GetLastError();
121 hRet = HRESULT_FROM_WIN32(hRet);
128 /**************************************************************************
131 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
133 ICOM_THIS(ISHFileStream, iface);
137 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbWritten);
140 hRet = STG_E_INVALIDPOINTER;
141 else if (!(This->dwMode & STGM_WRITE))
143 else if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
145 hRet = (HRESULT)GetLastError();
147 hRet = HRESULT_FROM_WIN32(hRet);
150 *pcbWritten = dwWritten;
154 /**************************************************************************
157 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
158 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
160 ICOM_THIS(ISHFileStream, iface);
163 TRACE("(%p,%ld,%ld,%p)\n", This, dlibMove.s.LowPart, dwOrigin, pNewPos);
165 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
166 dwPos = SetFilePointer(This->hFile, dlibMove.s.LowPart, NULL, dwOrigin);
170 pNewPos->s.HighPart = 0;
171 pNewPos->s.LowPart = dwPos;
176 /**************************************************************************
179 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
181 ICOM_THIS(ISHFileStream, iface);
183 TRACE("(%p,%ld)\n", This, libNewSize.s.LowPart);
184 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
188 /**************************************************************************
191 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
192 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
194 ICOM_THIS(ISHFileStream, iface);
199 TRACE("(%p,%p,%ld,%p,%p)\n", This, pstm, cb.s.LowPart, pcbRead, pcbWritten);
202 pcbRead->QuadPart = 0;
204 pcbWritten->QuadPart = 0;
207 return STG_E_INVALIDPOINTER;
209 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
212 ulSize = cb.QuadPart;
217 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
220 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
222 pcbRead->QuadPart += ulAmt;
223 if (FAILED(hRet) || ulAmt != ulLeft)
227 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
229 pcbWritten->QuadPart += ulAmt;
230 if (FAILED(hRet) || ulAmt != ulLeft)
238 /**************************************************************************
241 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
243 ICOM_THIS(ISHFileStream, iface);
245 TRACE("(%p,%ld)\n", This, grfCommitFlags);
246 /* Currently unbuffered: This function is not needed */
250 /**************************************************************************
253 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
255 ICOM_THIS(ISHFileStream, iface);
257 TRACE("(%p)\n", This);
261 /**************************************************************************
262 * IStream_fnLockUnlockRegion
264 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
265 ULARGE_INTEGER cb, DWORD dwLockType)
267 ICOM_THIS(ISHFileStream, iface);
268 TRACE("(%p,%ld,%ld,%ld)\n", This, libOffset.s.LowPart, cb.s.LowPart, dwLockType);
272 /*************************************************************************
275 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
278 ICOM_THIS(ISHFileStream, iface);
279 BY_HANDLE_FILE_INFORMATION fi;
282 TRACE("(%p,%p,%ld)\n", This, lpStat, grfStatFlag);
285 hRet = STG_E_INVALIDPOINTER;
288 memset(&fi, 0, sizeof(fi));
289 GetFileInformationByHandle(This->hFile, &fi);
291 if (grfStatFlag & STATFLAG_NONAME)
292 lpStat->pwcsName = NULL;
294 lpStat->pwcsName = StrDupW(This->lpszPath);
295 lpStat->type = This->type;
296 lpStat->cbSize.s.LowPart = fi.nFileSizeLow;
297 lpStat->cbSize.s.HighPart = fi.nFileSizeHigh;
298 lpStat->mtime = fi.ftLastWriteTime;
299 lpStat->ctime = fi.ftCreationTime;
300 lpStat->atime = fi.ftLastAccessTime;
301 lpStat->grfMode = This->dwMode;
302 lpStat->grfLocksSupported = 0;
303 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
304 lpStat->grfStateBits = This->grfStateBits;
305 lpStat->reserved = 0;
310 /*************************************************************************
313 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
315 ICOM_THIS(ISHFileStream, iface);
317 TRACE("(%p)\n",This);
323 static struct ICOM_VTABLE(IStream) SHLWAPI_fsVTable =
325 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
326 IStream_fnQueryInterface,
336 IStream_fnLockUnlockRegion,
337 IStream_fnLockUnlockRegion,
342 /**************************************************************************
345 * Internal helper: Create and initialise a new file stream object.
347 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
349 ISHFileStream* fileStream;
351 fileStream = (ISHFileStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
355 fileStream->lpVtbl = &SHLWAPI_fsVTable;
357 fileStream->hFile = hFile;
358 fileStream->dwMode = dwMode;
359 fileStream->lpszPath = StrDupW(lpszPath);
360 fileStream->type = 0; /* FIXME */
361 fileStream->grfStateBits = 0; /* FIXME */
363 TRACE ("Returning %p\n", fileStream);
364 return (IStream *)fileStream;
367 /*************************************************************************
368 * SHCreateStreamOnFileEx [SHLWAPI.@]
370 * Create a stream on a file.
373 * lpszPath [I] Path of file to create stream on
374 * dwMode [I] Mode to create stream in
375 * dwAttributes [I] Attributes of the file
376 * bCreate [I] Whether to create the file if it doesn't exist
377 * lpTemplate [I] Reserved, must be NULL
378 * lppStream [O] Destination for created stream
381 * Success: S_OK. lppStream contains the new stream object
382 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
385 * This function is available in Unicode only.
387 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
388 DWORD dwAttributes, BOOL bCreate,
389 IStream *lpTemplate, IStream **lppStream)
391 DWORD dwAccess, dwShare, dwCreate;
394 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
395 dwAttributes, bCreate, lpTemplate, lppStream);
397 if (!lpszPath || !lppStream || lpTemplate)
402 if (dwMode & ~(STGM_WRITE|STGM_READWRITE|STGM_SHARE_DENY_NONE|STGM_SHARE_DENY_READ|STGM_CREATE))
404 WARN("Invalid mode 0x%08lX\n", dwMode);
409 switch (dwMode & (STGM_WRITE|STGM_READWRITE))
411 case STGM_READWRITE|STGM_WRITE:
413 dwAccess = GENERIC_READ|GENERIC_WRITE;
416 dwAccess = GENERIC_WRITE;
419 dwAccess = GENERIC_READ;
424 switch (dwMode & STGM_SHARE_DENY_READ)
426 case STGM_SHARE_DENY_READ:
427 dwShare = FILE_SHARE_WRITE;
429 case STGM_SHARE_DENY_WRITE:
430 dwShare = FILE_SHARE_READ;
432 case STGM_SHARE_EXCLUSIVE:
436 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
439 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
440 if (dwMode == STGM_FAILIFTHERE)
441 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
442 else if (dwMode & STGM_CREATE)
443 dwCreate = CREATE_ALWAYS;
445 dwCreate = OPEN_ALWAYS;
447 /* Open HANDLE to file */
448 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
451 if(hFile == INVALID_HANDLE_VALUE)
453 HRESULT hRet = (HRESULT)GetLastError();
455 hRet = HRESULT_FROM_WIN32(hRet);
459 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
464 return E_OUTOFMEMORY;
469 /*************************************************************************
470 * SHCreateStreamOnFileW [SHLWAPI.@]
472 * See SHCreateStreamOnFileA.
474 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
479 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
481 if (!lpszPath || !lppStream)
484 dwAttr = GetFileAttributesW(lpszPath);
488 return SHCreateStreamOnFileEx(lpszPath, dwMode|STGM_WRITE, dwAttr,
489 TRUE, NULL, lppStream);
492 /*************************************************************************
493 * SHCreateStreamOnFileA [SHLWAPI.@]
495 * Create a stream on a file.
498 * lpszPath [I] Path of file to create stream on
499 * dwMode [I] Mode to create stream in
500 * lppStream [O] Destination for created IStream object
503 * Success: S_OK. lppStream contains the new IStream object
504 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
506 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
509 WCHAR szPath[MAX_PATH];
511 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
515 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
516 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
519 /*************************************************************************
522 * Call IStream_Read() on a stream.
525 * lpStream [I] IStream object
526 * lpvDest [O] Destination for data read
527 * ulSize [I] Size of data to read
530 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
531 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
532 * number of bytes read does not match.
534 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
539 TRACE("(%p,%p,%ld)\n", lpStream, lpvDest, ulSize);
541 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
543 if (SUCCEEDED(hRet) && ulRead != ulSize)
548 /*************************************************************************
551 * Determine if a stream has 0 length.
554 * lpStream [I] IStream object
557 * TRUE: If the stream has 0 length
560 BOOL WINAPI SHLWAPI_166(IStream *lpStream)
565 TRACE("(%p)\n", lpStream);
567 memset(&statstg, 0, sizeof(statstg));
569 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
571 if(statstg.cbSize.QuadPart)
572 bRet = FALSE; /* Non-Zero */
578 /* Try to read from the stream */
579 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
584 IStream_Seek(lpStream, zero, 0, NULL);
585 bRet = FALSE; /* Non-Zero */
591 /*************************************************************************
594 * Call IStream_Write() on a stream.
597 * lpStream [I] IStream object
598 * lpvSrc [I] Source for data to write
599 * ulSize [I] Size of data
602 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
603 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
604 * number of bytes written does not match.
606 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
611 TRACE("(%p,%p,%ld)\n", lpStream, lpvSrc, ulSize);
613 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
615 if (SUCCEEDED(hRet) && ulWritten != ulSize)
621 /*************************************************************************
624 * Seek to the start of a stream.
627 * lpStream [I] IStream object
630 * Success: S_OK. The current position within the stream is updated
631 * Failure: An HRESULT error code.
633 HRESULT WINAPI SHLWAPI_213(IStream *lpStream)
636 TRACE("(%p)\n", lpStream);
638 return IStream_Seek(lpStream, zero, 0, NULL);
641 /*************************************************************************
644 * Get the size of a stream.
647 * lpStream [I] IStream object
648 * lpulSize [O] Destination for size
651 * Success: S_OK. lpulSize contains the size of the stream.
652 * Failure: An HRESULT error code.
654 HRESULT WINAPI SHLWAPI_214(IStream *lpStream, ULARGE_INTEGER* lpulSize)
659 TRACE("(%p,%p)\n", lpStream, lpulSize);
661 memset(&statstg, 0, sizeof(statstg));
663 hRet = IStream_Stat(lpStream, &statstg, 1);
665 if (SUCCEEDED(hRet) && lpulSize)
666 *lpulSize = statstg.cbSize;