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
30 #define NO_SHLWAPI_REG
31 #define NO_SHLWAPI_PATH
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
37 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
38 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
39 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
41 /* Layout of ISHFileStream object */
44 IStream IStream_iface
;
53 static inline ISHFileStream
*impl_from_IStream(IStream
*iface
)
55 return CONTAINING_RECORD(iface
, ISHFileStream
, IStream_iface
);
58 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
61 /**************************************************************************
62 * IStream_fnQueryInterface
64 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
66 ISHFileStream
*This
= impl_from_IStream(iface
);
68 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
72 if(IsEqualIID(riid
, &IID_IUnknown
) ||
73 IsEqualIID(riid
, &IID_IStream
))
75 IStream_AddRef(iface
);
82 /**************************************************************************
85 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
87 ISHFileStream
*This
= impl_from_IStream(iface
);
88 ULONG refCount
= InterlockedIncrement(&This
->ref
);
90 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
95 /**************************************************************************
98 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
100 ISHFileStream
*This
= impl_from_IStream(iface
);
101 ULONG refCount
= InterlockedDecrement(&This
->ref
);
103 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
107 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
108 LocalFree(This
->lpszPath
);
109 CloseHandle(This
->hFile
);
110 HeapFree(GetProcessHeap(), 0, This
);
116 /**************************************************************************
119 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
121 ISHFileStream
*This
= impl_from_IStream(iface
);
124 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbRead
);
126 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
128 WARN("error %d reading file\n", GetLastError());
133 return dwRead
== cb
? S_OK
: S_FALSE
;
136 /**************************************************************************
139 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
141 ISHFileStream
*This
= impl_from_IStream(iface
);
144 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbWritten
);
146 switch (STGM_ACCESS_MODE(This
->dwMode
))
152 return STG_E_ACCESSDENIED
;
155 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
156 return HRESULT_FROM_WIN32(GetLastError());
159 *pcbWritten
= dwWritten
;
163 /**************************************************************************
166 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
167 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
169 ISHFileStream
*This
= impl_from_IStream(iface
);
172 TRACE("(%p,%d,%d,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
174 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
175 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
176 if( dwPos
== INVALID_SET_FILE_POINTER
)
177 return HRESULT_FROM_WIN32(GetLastError());
181 pNewPos
->u
.HighPart
= 0;
182 pNewPos
->u
.LowPart
= dwPos
;
187 /**************************************************************************
190 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
192 ISHFileStream
*This
= impl_from_IStream(iface
);
194 TRACE("(%p,%d)\n", This
, libNewSize
.u
.LowPart
);
196 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
197 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
200 if( ! SetEndOfFile( This
->hFile
) )
206 /**************************************************************************
209 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
210 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
212 ISHFileStream
*This
= impl_from_IStream(iface
);
217 TRACE("(%p,%p,%d,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
220 pcbRead
->QuadPart
= 0;
222 pcbWritten
->QuadPart
= 0;
227 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
230 ulSize
= cb
.QuadPart
;
233 ULONG ulLeft
, ulRead
, ulWritten
;
235 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
238 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulRead
);
239 if (FAILED(hRet
) || ulRead
== 0)
242 pcbRead
->QuadPart
+= ulRead
;
245 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulRead
, &ulWritten
);
247 pcbWritten
->QuadPart
+= ulWritten
;
248 if (FAILED(hRet
) || ulWritten
!= ulLeft
)
256 /**************************************************************************
259 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
261 ISHFileStream
*This
= impl_from_IStream(iface
);
263 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
264 /* Currently unbuffered: This function is not needed */
268 /**************************************************************************
271 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
273 ISHFileStream
*This
= impl_from_IStream(iface
);
275 TRACE("(%p)\n", This
);
279 /**************************************************************************
280 * IStream_fnLockUnlockRegion
282 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
283 ULARGE_INTEGER cb
, DWORD dwLockType
)
285 ISHFileStream
*This
= impl_from_IStream(iface
);
286 TRACE("(%p,%d,%d,%d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
290 /*************************************************************************
293 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
296 ISHFileStream
*This
= impl_from_IStream(iface
);
297 BY_HANDLE_FILE_INFORMATION fi
;
299 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
302 return STG_E_INVALIDPOINTER
;
304 memset(&fi
, 0, sizeof(fi
));
305 GetFileInformationByHandle(This
->hFile
, &fi
);
307 if (grfStatFlag
& STATFLAG_NONAME
)
308 lpStat
->pwcsName
= NULL
;
310 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
311 lpStat
->type
= This
->type
;
312 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
313 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
314 lpStat
->mtime
= fi
.ftLastWriteTime
;
315 lpStat
->ctime
= fi
.ftCreationTime
;
316 lpStat
->atime
= fi
.ftLastAccessTime
;
317 lpStat
->grfMode
= This
->dwMode
;
318 lpStat
->grfLocksSupported
= 0;
319 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
320 lpStat
->grfStateBits
= This
->grfStateBits
;
321 lpStat
->reserved
= 0;
326 /*************************************************************************
329 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
331 ISHFileStream
*This
= impl_from_IStream(iface
);
333 TRACE("(%p)\n",This
);
339 static const IStreamVtbl SHLWAPI_fsVTable
=
341 IStream_fnQueryInterface
,
351 IStream_fnLockUnlockRegion
,
352 IStream_fnLockUnlockRegion
,
357 /**************************************************************************
360 * Internal helper: Create and initialise a new file stream object.
362 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
364 ISHFileStream
*fileStream
;
366 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
367 if (!fileStream
) return NULL
;
369 fileStream
->IStream_iface
.lpVtbl
= &SHLWAPI_fsVTable
;
371 fileStream
->hFile
= hFile
;
372 fileStream
->dwMode
= dwMode
;
373 fileStream
->lpszPath
= StrDupW(lpszPath
);
374 fileStream
->type
= 0; /* FIXME */
375 fileStream
->grfStateBits
= 0; /* FIXME */
377 TRACE ("Returning %p\n", fileStream
);
378 return &fileStream
->IStream_iface
;
381 /*************************************************************************
382 * SHCreateStreamOnFileEx [SHLWAPI.@]
384 * Create a stream on a file.
387 * lpszPath [I] Path of file to create stream on
388 * dwMode [I] Mode to create stream in
389 * dwAttributes [I] Attributes of the file
390 * bCreate [I] Whether to create the file if it doesn't exist
391 * lpTemplate [I] Reserved, must be NULL
392 * lppStream [O] Destination for created stream
395 * Success: S_OK. lppStream contains the new stream object
396 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
399 * This function is available in Unicode only.
401 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
402 DWORD dwAttributes
, BOOL bCreate
,
403 IStream
*lpTemplate
, IStream
**lppStream
)
405 DWORD dwAccess
, dwShare
, dwCreate
;
408 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
409 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
411 if (!lpszPath
|| !lppStream
|| lpTemplate
)
417 switch (STGM_ACCESS_MODE(dwMode
))
421 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
424 dwAccess
= GENERIC_READ
;
431 switch (STGM_SHARE_MODE(dwMode
))
434 case STGM_SHARE_DENY_NONE
:
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
:
450 switch(STGM_CREATE_MODE(dwMode
))
452 case STGM_FAILIFTHERE
:
453 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
456 dwCreate
= CREATE_ALWAYS
;
462 /* Open HANDLE to file */
463 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
466 if(hFile
== INVALID_HANDLE_VALUE
)
467 return HRESULT_FROM_WIN32(GetLastError());
469 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
474 return E_OUTOFMEMORY
;
479 /*************************************************************************
480 * SHCreateStreamOnFileW [SHLWAPI.@]
482 * See SHCreateStreamOnFileA.
484 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
487 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
489 if (!lpszPath
|| !lppStream
)
492 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
495 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
498 /*************************************************************************
499 * SHCreateStreamOnFileA [SHLWAPI.@]
501 * Create a stream on a file.
504 * lpszPath [I] Path of file to create stream on
505 * dwMode [I] Mode to create stream in
506 * lppStream [O] Destination for created IStream object
509 * Success: S_OK. lppStream contains the new IStream object
510 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
512 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
515 WCHAR szPath
[MAX_PATH
];
517 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
520 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
522 MultiByteToWideChar(CP_ACP
, 0, lpszPath
, -1, szPath
, MAX_PATH
);
523 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
526 /*************************************************************************
529 * Call IStream_Read() on a stream.
532 * lpStream [I] IStream object
533 * lpvDest [O] Destination for data read
534 * ulSize [I] Size of data to read
537 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
538 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
539 * number of bytes read does not match.
541 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
546 TRACE("(%p,%p,%d)\n", lpStream
, lpvDest
, ulSize
);
548 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
550 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
555 /*************************************************************************
558 * Determine if a stream has 0 length.
561 * lpStream [I] IStream object
564 * TRUE: If the stream has 0 length
567 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
572 TRACE("(%p)\n", lpStream
);
574 memset(&statstg
, 0, sizeof(statstg
));
576 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
578 if(statstg
.cbSize
.QuadPart
)
579 bRet
= FALSE
; /* Non-Zero */
585 /* Try to read from the stream */
586 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
591 IStream_Seek(lpStream
, zero
, 0, NULL
);
592 bRet
= FALSE
; /* Non-Zero */
598 /*************************************************************************
601 * Call IStream_Write() on a stream.
604 * lpStream [I] IStream object
605 * lpvSrc [I] Source for data to write
606 * ulSize [I] Size of data
609 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
610 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
611 * number of bytes written does not match.
613 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
618 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
620 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
622 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
628 /*************************************************************************
631 * Seek to the start of a stream.
634 * lpStream [I] IStream object
637 * Success: S_OK. The current position within the stream is updated
638 * Failure: An HRESULT error code.
640 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
643 TRACE("(%p)\n", lpStream
);
645 return IStream_Seek(lpStream
, zero
, 0, NULL
);
648 /*************************************************************************
651 * Get the size of a stream.
654 * lpStream [I] IStream object
655 * lpulSize [O] Destination for size
658 * Success: S_OK. lpulSize contains the size of the stream.
659 * Failure: An HRESULT error code.
661 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
666 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
668 memset(&statstg
, 0, sizeof(statstg
));
670 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
672 if (SUCCEEDED(hRet
) && lpulSize
)
673 *lpulSize
= statstg
.cbSize
;