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
))
62 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
;
221 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
224 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
226 pcbRead
->QuadPart
+= ulAmt
;
227 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
231 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
233 pcbWritten
->QuadPart
+= ulAmt
;
234 if (FAILED(hRet
) || ulAmt
!= 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
;
286 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
289 hRet
= STG_E_INVALIDPOINTER
;
292 memset(&fi
, 0, sizeof(fi
));
293 GetFileInformationByHandle(This
->hFile
, &fi
);
295 if (grfStatFlag
& STATFLAG_NONAME
)
296 lpStat
->pwcsName
= NULL
;
298 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
299 lpStat
->type
= This
->type
;
300 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
301 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
302 lpStat
->mtime
= fi
.ftLastWriteTime
;
303 lpStat
->ctime
= fi
.ftCreationTime
;
304 lpStat
->atime
= fi
.ftLastAccessTime
;
305 lpStat
->grfMode
= This
->dwMode
;
306 lpStat
->grfLocksSupported
= 0;
307 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
308 lpStat
->grfStateBits
= This
->grfStateBits
;
309 lpStat
->reserved
= 0;
314 /*************************************************************************
317 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
319 ISHFileStream
*This
= impl_from_IStream(iface
);
321 TRACE("(%p)\n",This
);
327 static const IStreamVtbl SHLWAPI_fsVTable
=
329 IStream_fnQueryInterface
,
339 IStream_fnLockUnlockRegion
,
340 IStream_fnLockUnlockRegion
,
345 /**************************************************************************
348 * Internal helper: Create and initialise a new file stream object.
350 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
352 ISHFileStream
* fileStream
;
354 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
358 fileStream
->IStream_iface
.lpVtbl
= &SHLWAPI_fsVTable
;
360 fileStream
->hFile
= hFile
;
361 fileStream
->dwMode
= dwMode
;
362 fileStream
->lpszPath
= StrDupW(lpszPath
);
363 fileStream
->type
= 0; /* FIXME */
364 fileStream
->grfStateBits
= 0; /* FIXME */
366 TRACE ("Returning %p\n", fileStream
);
367 return &fileStream
->IStream_iface
;
370 /*************************************************************************
371 * SHCreateStreamOnFileEx [SHLWAPI.@]
373 * Create a stream on a file.
376 * lpszPath [I] Path of file to create stream on
377 * dwMode [I] Mode to create stream in
378 * dwAttributes [I] Attributes of the file
379 * bCreate [I] Whether to create the file if it doesn't exist
380 * lpTemplate [I] Reserved, must be NULL
381 * lppStream [O] Destination for created stream
384 * Success: S_OK. lppStream contains the new stream object
385 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
388 * This function is available in Unicode only.
390 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
391 DWORD dwAttributes
, BOOL bCreate
,
392 IStream
*lpTemplate
, IStream
**lppStream
)
394 DWORD dwAccess
, dwShare
, dwCreate
;
397 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
398 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
400 if (!lpszPath
|| !lppStream
|| lpTemplate
)
406 switch (STGM_ACCESS_MODE(dwMode
))
409 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
412 dwAccess
= GENERIC_WRITE
;
415 dwAccess
= GENERIC_READ
;
422 switch (STGM_SHARE_MODE(dwMode
))
425 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
427 case STGM_SHARE_DENY_READ
:
428 dwShare
= FILE_SHARE_WRITE
;
430 case STGM_SHARE_DENY_WRITE
:
431 dwShare
= FILE_SHARE_READ
;
433 case STGM_SHARE_EXCLUSIVE
:
436 case STGM_SHARE_DENY_NONE
:
437 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
443 switch(STGM_CREATE_MODE(dwMode
))
445 case STGM_FAILIFTHERE
:
446 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
449 dwCreate
= CREATE_ALWAYS
;
455 /* Open HANDLE to file */
456 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
459 if(hFile
== INVALID_HANDLE_VALUE
)
460 return HRESULT_FROM_WIN32(GetLastError());
462 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
467 return E_OUTOFMEMORY
;
472 /*************************************************************************
473 * SHCreateStreamOnFileW [SHLWAPI.@]
475 * See SHCreateStreamOnFileA.
477 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
480 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
482 if (!lpszPath
|| !lppStream
)
485 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
488 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
491 /*************************************************************************
492 * SHCreateStreamOnFileA [SHLWAPI.@]
494 * Create a stream on a file.
497 * lpszPath [I] Path of file to create stream on
498 * dwMode [I] Mode to create stream in
499 * lppStream [O] Destination for created IStream object
502 * Success: S_OK. lppStream contains the new IStream object
503 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
505 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
508 WCHAR szPath
[MAX_PATH
];
510 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
513 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
515 MultiByteToWideChar(CP_ACP
, 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
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
539 TRACE("(%p,%p,%d)\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
SHIsEmptyStream(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(SHIStream_Read(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
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
611 TRACE("(%p,%p,%d)\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
IStream_Reset(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
IStream_Size(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
;