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
25 #define NO_SHLWAPI_REG
26 #define NO_SHLWAPI_PATH
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32 /* Layout of ISHFileStream object */
44 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
47 /**************************************************************************
48 * IStream_fnQueryInterface
50 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
52 ICOM_THIS(ISHFileStream, iface);
54 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
58 if(IsEqualIID(riid, &IID_IUnknown) ||
59 IsEqualIID(riid, &IID_IStream))
63 IStream_AddRef((IStream*)*ppvObj);
69 /**************************************************************************
72 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
74 ICOM_THIS(ISHFileStream, iface);
76 TRACE("(%p)\n", This);
77 return InterlockedIncrement(&This->ref);
80 /**************************************************************************
83 static ULONG WINAPI IStream_fnRelease(IStream *iface)
85 ICOM_THIS(ISHFileStream, iface);
88 TRACE("(%p)\n", This);
90 if (!(ulRet = InterlockedDecrement(&This->ref)))
92 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
93 LocalFree((HLOCAL)This->lpszPath);
94 CloseHandle(This->hFile);
95 HeapFree(GetProcessHeap(), 0, This);
100 /**************************************************************************
103 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
105 ICOM_THIS(ISHFileStream, iface);
109 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbRead);
112 hRet = STG_E_INVALIDPOINTER;
113 else if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
115 hRet = (HRESULT)GetLastError();
117 hRet = HRESULT_FROM_WIN32(hRet);
124 /**************************************************************************
127 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
129 ICOM_THIS(ISHFileStream, iface);
133 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbWritten);
136 hRet = STG_E_INVALIDPOINTER;
137 else if (!(This->dwMode & STGM_WRITE))
139 else if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
141 hRet = (HRESULT)GetLastError();
143 hRet = HRESULT_FROM_WIN32(hRet);
146 *pcbWritten = dwWritten;
150 /**************************************************************************
153 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
154 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
156 ICOM_THIS(ISHFileStream, iface);
159 TRACE("(%p,%ld,%ld,%p)\n", This, dlibMove.s.LowPart, dwOrigin, pNewPos);
161 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
162 dwPos = SetFilePointer(This->hFile, dlibMove.s.LowPart, NULL, dwOrigin);
166 pNewPos->s.HighPart = 0;
167 pNewPos->s.LowPart = dwPos;
172 /**************************************************************************
175 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
177 ICOM_THIS(ISHFileStream, iface);
179 TRACE("(%p,%ld)\n", This, libNewSize.s.LowPart);
180 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
184 /**************************************************************************
187 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
188 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
190 ICOM_THIS(ISHFileStream, iface);
195 TRACE("(%p,%p,%ld,%p,%p)\n", This, pstm, cb.s.LowPart, pcbRead, pcbWritten);
198 pcbRead->QuadPart = 0;
200 pcbWritten->QuadPart = 0;
203 return STG_E_INVALIDPOINTER;
205 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
208 ulSize = cb.QuadPart;
213 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
216 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
218 pcbRead->QuadPart += ulAmt;
219 if (FAILED(hRet) || ulAmt != ulLeft)
223 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
225 pcbWritten->QuadPart += ulAmt;
226 if (FAILED(hRet) || ulAmt != ulLeft)
234 /**************************************************************************
237 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
239 ICOM_THIS(ISHFileStream, iface);
241 TRACE("(%p,%ld)\n", This, grfCommitFlags);
242 /* Currently unbuffered: This function is not needed */
246 /**************************************************************************
249 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
251 ICOM_THIS(ISHFileStream, iface);
253 TRACE("(%p)\n", This);
257 /**************************************************************************
258 * IStream_fnLockUnlockRegion
260 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
261 ULARGE_INTEGER cb, DWORD dwLockType)
263 ICOM_THIS(ISHFileStream, iface);
264 TRACE("(%p,%ld,%ld,%ld)\n", This, libOffset.s.LowPart, cb.s.LowPart, dwLockType);
268 /*************************************************************************
271 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
274 ICOM_THIS(ISHFileStream, iface);
275 BY_HANDLE_FILE_INFORMATION fi;
278 TRACE("(%p,%p,%ld)\n", This, lpStat, grfStatFlag);
281 hRet = STG_E_INVALIDPOINTER;
284 memset(&fi, 0, sizeof(fi));
285 GetFileInformationByHandle(This->hFile, &fi);
287 if (grfStatFlag & STATFLAG_NONAME)
288 lpStat->pwcsName = NULL;
290 lpStat->pwcsName = StrDupW(This->lpszPath);
291 lpStat->type = This->type;
292 lpStat->cbSize.s.LowPart = fi.nFileSizeLow;
293 lpStat->cbSize.s.HighPart = fi.nFileSizeHigh;
294 lpStat->mtime = fi.ftLastWriteTime;
295 lpStat->ctime = fi.ftCreationTime;
296 lpStat->atime = fi.ftLastAccessTime;
297 lpStat->grfMode = This->dwMode;
298 lpStat->grfLocksSupported = 0;
299 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
300 lpStat->grfStateBits = This->grfStateBits;
301 lpStat->reserved = 0;
306 /*************************************************************************
309 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
311 ICOM_THIS(ISHFileStream, iface);
313 TRACE("(%p)\n",This);
319 static struct ICOM_VTABLE(IStream) SHLWAPI_fsVTable =
321 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
322 IStream_fnQueryInterface,
332 IStream_fnLockUnlockRegion,
333 IStream_fnLockUnlockRegion,
338 /**************************************************************************
341 * Internal helper: Create and initialise a new file stream object.
343 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
345 ISHFileStream* fileStream;
347 fileStream = (ISHFileStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
351 ICOM_VTBL(fileStream) = &SHLWAPI_fsVTable;
353 fileStream->hFile = hFile;
354 fileStream->dwMode = dwMode;
355 fileStream->lpszPath = StrDupW(lpszPath);
356 fileStream->type = 0; /* FIXME */
357 fileStream->grfStateBits = 0; /* FIXME */
359 TRACE ("Returning %p\n", fileStream);
360 return (IStream *)fileStream;
363 /*************************************************************************
364 * SHCreateStreamOnFileEx [SHLWAPI.@]
366 * Create a stream on a file.
369 * lpszPath [I] Path of file to create stream on
370 * dwMode [I] Mode to create stream in
371 * dwAttributes [I] Attributes of the file
372 * bCreate [I] Whether to create the file if it doesn't exist
373 * lpTemplate [I] Reserved, must be NULL
374 * lppStream [O] Destination for created stream
377 * Success: S_OK. lppStream contains the new stream object
378 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
381 * This function is available in Unicode only.
383 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
384 DWORD dwAttributes, BOOL bCreate,
385 IStream *lpTemplate, IStream **lppStream)
387 DWORD dwAccess, dwShare, dwCreate;
390 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
391 dwAttributes, bCreate, lpTemplate, lppStream);
393 if (!lpszPath || !lppStream || lpTemplate)
398 if (dwMode & ~(STGM_WRITE|STGM_READWRITE|STGM_SHARE_DENY_NONE|STGM_SHARE_DENY_READ|STGM_CREATE))
400 WARN("Invalid mode 0x%08lX\n", dwMode);
405 switch (dwMode & (STGM_WRITE|STGM_READWRITE))
407 case STGM_READWRITE|STGM_WRITE:
409 dwAccess = GENERIC_READ|GENERIC_WRITE;
412 dwAccess = GENERIC_WRITE;
415 dwAccess = GENERIC_READ;
420 switch (dwMode & STGM_SHARE_DENY_READ)
422 case STGM_SHARE_DENY_READ:
423 dwShare = FILE_SHARE_WRITE;
425 case STGM_SHARE_DENY_WRITE:
426 dwShare = FILE_SHARE_READ;
428 case STGM_SHARE_EXCLUSIVE:
432 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
435 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
436 if (dwMode == STGM_FAILIFTHERE)
437 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
438 else if (dwMode & STGM_CREATE)
439 dwCreate = CREATE_ALWAYS;
441 dwCreate = OPEN_ALWAYS;
443 /* Open HANDLE to file */
444 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
445 dwAttributes, (HANDLE)0);
447 if(hFile == INVALID_HANDLE_VALUE)
449 HRESULT hRet = (HRESULT)GetLastError();
451 hRet = HRESULT_FROM_WIN32(hRet);
455 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
460 return E_OUTOFMEMORY;
465 /*************************************************************************
466 * SHCreateStreamOnFileW [SHLWAPI.@]
468 * See SHCreateStreamOnFileA.
470 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
475 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
477 if (!lpszPath || !lppStream)
480 dwAttr = GetFileAttributesW(lpszPath);
484 return SHCreateStreamOnFileEx(lpszPath, dwMode|STGM_WRITE, dwAttr,
485 TRUE, NULL, lppStream);
488 /*************************************************************************
489 * SHCreateStreamOnFileA [SHLWAPI.@]
491 * Create a stream on a file.
494 * lpszPath [I] Path of file to create stream on
495 * dwMode [I] Mode to create stream in
496 * lppStream [O] Destination for created stream
499 * Success: S_OK. lppStream contains the new stream object
500 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
502 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
505 WCHAR szPath[MAX_PATH];
507 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
511 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
512 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
515 /*************************************************************************
518 * Call IStream::Read on a Stream.
521 * lpStream [I] IStream object
522 * lpvDest [O] Destination for data read
523 * ulSize [I] Size of data to read
526 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
527 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
528 * number of bytes read does not match.
530 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
535 TRACE("(%p,%p,%ld)\n", lpStream, lpvDest, ulSize);
537 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
539 if (SUCCEEDED(hRet) && ulRead != ulSize)
544 /*************************************************************************
547 * Determine if a stream has 0 length.
550 * lpStream [I] IStream object
553 * TRUE: If the stream has 0 length
556 BOOL WINAPI SHLWAPI_166(IStream *lpStream)
561 TRACE("(%p)\n", lpStream);
563 memset(&statstg, 0, sizeof(statstg));
565 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
567 if(statstg.cbSize.QuadPart)
568 bRet = FALSE; /* Non-Zero */
574 /* Try to read from the stream */
575 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
580 IStream_Seek(lpStream, zero, 0, NULL);
581 bRet = FALSE; /* Non-Zero */
587 /*************************************************************************
590 * Call IStream::Write on a Stream.
593 * lpStream [I] IStream object
594 * lpvSrc [I] Source for data to write
595 * ulSize [I] Size of data
598 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
599 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
600 * number of bytes written does not match.
602 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
607 TRACE("(%p,%p,%ld)\n", lpStream, lpvSrc, ulSize);
609 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
611 if (SUCCEEDED(hRet) && ulWritten != ulSize)
617 /*************************************************************************
620 * Seek to the start of a stream.
623 * lpStream [I] IStream object
626 * Success: S_OK. The current position within the stream is updated
627 * Failure: An HRESULT error code.
629 HRESULT WINAPI SHLWAPI_213(IStream *lpStream)
632 TRACE("(%p)\n", lpStream);
634 return IStream_Seek(lpStream, zero, 0, NULL);
637 /*************************************************************************
640 * Get the size of a Stream.
643 * lpStream [I] IStream object
644 * lpulSize [O] Destination for size
647 * Success: S_OK. lpulSize contains the size of the stream.
648 * Failure: An HRESULT error code.
650 HRESULT WINAPI SHLWAPI_214(IStream *lpStream, ULARGE_INTEGER* lpulSize)
655 TRACE("(%p,%p)\n", lpStream, lpulSize);
657 memset(&statstg, 0, sizeof(statstg));
659 hRet = IStream_Stat(lpStream, &statstg, 1);
661 if (SUCCEEDED(hRet) && lpulSize)
662 *lpulSize = statstg.cbSize;