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
23 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
24 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
25 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
27 /* Layout of ISHFileStream object */
30 IStream IStream_iface
;
39 static inline ISHFileStream
*impl_from_IStream(IStream
*iface
)
41 return CONTAINING_RECORD(iface
, ISHFileStream
, IStream_iface
);
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 ISHFileStream
*This
= impl_from_IStream(iface
);
54 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
58 if(IsEqualIID(riid
, &IID_IUnknown
) ||
59 IsEqualIID(riid
, &IID_IStream
))
61 IStream_AddRef(iface
);
68 /**************************************************************************
71 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
73 ISHFileStream
*This
= impl_from_IStream(iface
);
74 ULONG refCount
= InterlockedIncrement(&This
->ref
);
76 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
81 /**************************************************************************
84 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
86 ISHFileStream
*This
= impl_from_IStream(iface
);
87 ULONG refCount
= InterlockedDecrement(&This
->ref
);
89 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
93 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
94 LocalFree(This
->lpszPath
);
95 CloseHandle(This
->hFile
);
96 HeapFree(GetProcessHeap(), 0, This
);
102 /**************************************************************************
105 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
107 ISHFileStream
*This
= impl_from_IStream(iface
);
110 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbRead
);
112 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
114 WARN("error %d reading file\n", GetLastError());
122 /**************************************************************************
125 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
127 ISHFileStream
*This
= impl_from_IStream(iface
);
130 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbWritten
);
132 switch (STGM_ACCESS_MODE(This
->dwMode
))
138 return STG_E_ACCESSDENIED
;
141 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
142 return HRESULT_FROM_WIN32(GetLastError());
145 *pcbWritten
= dwWritten
;
149 /**************************************************************************
152 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
153 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
155 ISHFileStream
*This
= impl_from_IStream(iface
);
158 TRACE("(%p,%d,%d,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
160 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
161 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
162 if( dwPos
== INVALID_SET_FILE_POINTER
)
163 return HRESULT_FROM_WIN32(GetLastError());
167 pNewPos
->u
.HighPart
= 0;
168 pNewPos
->u
.LowPart
= dwPos
;
173 /**************************************************************************
176 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
178 ISHFileStream
*This
= impl_from_IStream(iface
);
180 TRACE("(%p,%d)\n", This
, libNewSize
.u
.LowPart
);
182 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
183 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
186 if( ! SetEndOfFile( This
->hFile
) )
192 /**************************************************************************
195 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
196 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
198 ISHFileStream
*This
= impl_from_IStream(iface
);
203 TRACE("(%p,%p,%d,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
206 pcbRead
->QuadPart
= 0;
208 pcbWritten
->QuadPart
= 0;
213 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
216 ulSize
= cb
.QuadPart
;
219 ULONG ulLeft
, ulRead
, ulWritten
;
221 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
224 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulRead
);
225 if (FAILED(hRet
) || ulRead
== 0)
228 pcbRead
->QuadPart
+= ulRead
;
231 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulRead
, &ulWritten
);
233 pcbWritten
->QuadPart
+= ulWritten
;
234 if (FAILED(hRet
) || ulWritten
!= ulLeft
)
242 /**************************************************************************
245 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
247 ISHFileStream
*This
= impl_from_IStream(iface
);
249 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
250 /* Currently unbuffered: This function is not needed */
254 /**************************************************************************
257 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
259 ISHFileStream
*This
= impl_from_IStream(iface
);
261 TRACE("(%p)\n", This
);
265 /**************************************************************************
266 * IStream_fnLockUnlockRegion
268 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
269 ULARGE_INTEGER cb
, DWORD dwLockType
)
271 ISHFileStream
*This
= impl_from_IStream(iface
);
272 TRACE("(%p,%d,%d,%d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
276 /*************************************************************************
279 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
282 ISHFileStream
*This
= impl_from_IStream(iface
);
283 BY_HANDLE_FILE_INFORMATION fi
;
285 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
288 return STG_E_INVALIDPOINTER
;
290 memset(&fi
, 0, sizeof(fi
));
291 GetFileInformationByHandle(This
->hFile
, &fi
);
293 if (grfStatFlag
& STATFLAG_NONAME
)
294 lpStat
->pwcsName
= NULL
;
296 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
297 lpStat
->type
= This
->type
;
298 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
299 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
300 lpStat
->mtime
= fi
.ftLastWriteTime
;
301 lpStat
->ctime
= fi
.ftCreationTime
;
302 lpStat
->atime
= fi
.ftLastAccessTime
;
303 lpStat
->grfMode
= This
->dwMode
;
304 lpStat
->grfLocksSupported
= 0;
305 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
306 lpStat
->grfStateBits
= This
->grfStateBits
;
307 lpStat
->reserved
= 0;
312 /*************************************************************************
315 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
317 ISHFileStream
*This
= impl_from_IStream(iface
);
319 TRACE("(%p)\n",This
);
325 static const IStreamVtbl SHLWAPI_fsVTable
=
327 IStream_fnQueryInterface
,
337 IStream_fnLockUnlockRegion
,
338 IStream_fnLockUnlockRegion
,
343 /**************************************************************************
346 * Internal helper: Create and initialise a new file stream object.
348 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
350 ISHFileStream
*fileStream
;
352 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
353 if (!fileStream
) return NULL
;
355 fileStream
->IStream_iface
.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 &fileStream
->IStream_iface
;
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,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
395 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
397 if (!lpszPath
|| !lppStream
|| lpTemplate
)
403 switch (STGM_ACCESS_MODE(dwMode
))
407 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
410 dwAccess
= GENERIC_READ
;
417 switch (STGM_SHARE_MODE(dwMode
))
420 case STGM_SHARE_DENY_NONE
:
421 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
423 case STGM_SHARE_DENY_READ
:
424 dwShare
= FILE_SHARE_WRITE
;
426 case STGM_SHARE_DENY_WRITE
:
427 dwShare
= FILE_SHARE_READ
;
429 case STGM_SHARE_EXCLUSIVE
:
436 switch(STGM_CREATE_MODE(dwMode
))
438 case STGM_FAILIFTHERE
:
439 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
442 dwCreate
= CREATE_ALWAYS
;
448 /* Open HANDLE to file */
449 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
452 if(hFile
== INVALID_HANDLE_VALUE
)
453 return HRESULT_FROM_WIN32(GetLastError());
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
,
473 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
475 if (!lpszPath
|| !lppStream
)
478 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
481 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
484 /*************************************************************************
485 * SHCreateStreamOnFileA [SHLWAPI.@]
487 * Create a stream on a file.
490 * lpszPath [I] Path of file to create stream on
491 * dwMode [I] Mode to create stream in
492 * lppStream [O] Destination for created IStream object
495 * Success: S_OK. lppStream contains the new IStream object
496 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
498 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
501 WCHAR szPath
[MAX_PATH
];
503 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
506 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
508 MultiByteToWideChar(CP_ACP
, 0, lpszPath
, -1, szPath
, MAX_PATH
);
509 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
512 /*************************************************************************
515 * Call IStream_Read() on a stream.
518 * lpStream [I] IStream object
519 * lpvDest [O] Destination for data read
520 * ulSize [I] Size of data to read
523 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
524 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
525 * number of bytes read does not match.
527 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
532 TRACE("(%p,%p,%d)\n", lpStream
, lpvDest
, ulSize
);
534 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
536 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
541 /*************************************************************************
544 * Determine if a stream has 0 length.
547 * lpStream [I] IStream object
550 * TRUE: If the stream has 0 length
553 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
558 TRACE("(%p)\n", lpStream
);
560 memset(&statstg
, 0, sizeof(statstg
));
562 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
564 if(statstg
.cbSize
.QuadPart
)
565 bRet
= FALSE
; /* Non-Zero */
571 /* Try to read from the stream */
572 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
577 IStream_Seek(lpStream
, zero
, 0, NULL
);
578 bRet
= FALSE
; /* Non-Zero */
584 /*************************************************************************
587 * Call IStream_Write() on a stream.
590 * lpStream [I] IStream object
591 * lpvSrc [I] Source for data to write
592 * ulSize [I] Size of data
595 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
596 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
597 * number of bytes written does not match.
599 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
604 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
606 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
608 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
614 /*************************************************************************
617 * Seek to the start of a stream.
620 * lpStream [I] IStream object
623 * Success: S_OK. The current position within the stream is updated
624 * Failure: An HRESULT error code.
626 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
629 TRACE("(%p)\n", lpStream
);
631 return IStream_Seek(lpStream
, zero
, 0, NULL
);
634 /*************************************************************************
637 * Get the size of a stream.
640 * lpStream [I] IStream object
641 * lpulSize [O] Destination for size
644 * Success: S_OK. lpulSize contains the size of the stream.
645 * Failure: An HRESULT error code.
647 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
652 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
654 memset(&statstg
, 0, sizeof(statstg
));
656 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
658 if (SUCCEEDED(hRet
) && lpulSize
)
659 *lpulSize
= statstg
.cbSize
;