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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
31 #define NO_SHLWAPI_REG
32 #define NO_SHLWAPI_PATH
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
38 /* Layout of ISHFileStream object */
50 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
53 /**************************************************************************
54 * IStream_fnQueryInterface
56 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
58 ISHFileStream
*This
= (ISHFileStream
*)iface
;
60 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
64 if(IsEqualIID(riid
, &IID_IUnknown
) ||
65 IsEqualIID(riid
, &IID_IStream
))
69 IStream_AddRef((IStream
*)*ppvObj
);
75 /**************************************************************************
78 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
80 ISHFileStream
*This
= (ISHFileStream
*)iface
;
82 TRACE("(%p)\n", This
);
83 return InterlockedIncrement(&This
->ref
);
86 /**************************************************************************
89 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
91 ISHFileStream
*This
= (ISHFileStream
*)iface
;
94 TRACE("(%p)\n", This
);
96 if (!(ulRet
= InterlockedDecrement(&This
->ref
)))
98 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
99 LocalFree((HLOCAL
)This
->lpszPath
);
100 CloseHandle(This
->hFile
);
101 HeapFree(GetProcessHeap(), 0, This
);
106 /**************************************************************************
109 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
111 ISHFileStream
*This
= (ISHFileStream
*)iface
;
115 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbRead
);
118 hRet
= STG_E_INVALIDPOINTER
;
119 else if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
121 hRet
= (HRESULT
)GetLastError();
123 hRet
= HRESULT_FROM_WIN32(hRet
);
130 /**************************************************************************
133 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
135 ISHFileStream
*This
= (ISHFileStream
*)iface
;
139 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbWritten
);
142 hRet
= STG_E_INVALIDPOINTER
;
143 else if (!(This
->dwMode
& STGM_WRITE
))
145 else if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
147 hRet
= (HRESULT
)GetLastError();
149 hRet
= HRESULT_FROM_WIN32(hRet
);
152 *pcbWritten
= dwWritten
;
156 /**************************************************************************
159 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
160 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
162 ISHFileStream
*This
= (ISHFileStream
*)iface
;
165 TRACE("(%p,%ld,%ld,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
167 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
168 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
172 pNewPos
->u
.HighPart
= 0;
173 pNewPos
->u
.LowPart
= dwPos
;
178 /**************************************************************************
181 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
183 ISHFileStream
*This
= (ISHFileStream
*)iface
;
185 TRACE("(%p,%ld)\n", This
, libNewSize
.u
.LowPart
);
186 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
190 /**************************************************************************
193 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
194 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
196 ISHFileStream
*This
= (ISHFileStream
*)iface
;
201 TRACE("(%p,%p,%ld,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
204 pcbRead
->QuadPart
= 0;
206 pcbWritten
->QuadPart
= 0;
209 return STG_E_INVALIDPOINTER
;
211 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
214 ulSize
= cb
.QuadPart
;
219 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
222 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
224 pcbRead
->QuadPart
+= ulAmt
;
225 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
229 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
231 pcbWritten
->QuadPart
+= ulAmt
;
232 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
240 /**************************************************************************
243 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
245 ISHFileStream
*This
= (ISHFileStream
*)iface
;
247 TRACE("(%p,%ld)\n", This
, grfCommitFlags
);
248 /* Currently unbuffered: This function is not needed */
252 /**************************************************************************
255 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
257 ISHFileStream
*This
= (ISHFileStream
*)iface
;
259 TRACE("(%p)\n", This
);
263 /**************************************************************************
264 * IStream_fnLockUnlockRegion
266 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
267 ULARGE_INTEGER cb
, DWORD dwLockType
)
269 ISHFileStream
*This
= (ISHFileStream
*)iface
;
270 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
274 /*************************************************************************
277 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
280 ISHFileStream
*This
= (ISHFileStream
*)iface
;
281 BY_HANDLE_FILE_INFORMATION fi
;
284 TRACE("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
287 hRet
= 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
= (ISHFileStream
*)iface
;
319 TRACE("(%p)\n",This
);
325 static struct 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
= (ISHFileStream
*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
356 fileStream
->lpVtbl
= &SHLWAPI_fsVTable
;
358 fileStream
->hFile
= hFile
;
359 fileStream
->dwMode
= dwMode
;
360 fileStream
->lpszPath
= StrDupW(lpszPath
);
361 fileStream
->type
= 0; /* FIXME */
362 fileStream
->grfStateBits
= 0; /* FIXME */
364 TRACE ("Returning %p\n", fileStream
);
365 return (IStream
*)fileStream
;
368 /*************************************************************************
369 * SHCreateStreamOnFileEx [SHLWAPI.@]
371 * Create a stream on a file.
374 * lpszPath [I] Path of file to create stream on
375 * dwMode [I] Mode to create stream in
376 * dwAttributes [I] Attributes of the file
377 * bCreate [I] Whether to create the file if it doesn't exist
378 * lpTemplate [I] Reserved, must be NULL
379 * lppStream [O] Destination for created stream
382 * Success: S_OK. lppStream contains the new stream object
383 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
386 * This function is available in Unicode only.
388 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
389 DWORD dwAttributes
, BOOL bCreate
,
390 IStream
*lpTemplate
, IStream
**lppStream
)
392 DWORD dwAccess
, dwShare
, dwCreate
;
395 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
396 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
398 if (!lpszPath
|| !lppStream
|| lpTemplate
)
403 if (dwMode
& ~(STGM_WRITE
|STGM_READWRITE
|STGM_SHARE_DENY_NONE
|STGM_SHARE_DENY_READ
|STGM_CREATE
))
405 WARN("Invalid mode 0x%08lX\n", dwMode
);
410 switch (dwMode
& (STGM_WRITE
|STGM_READWRITE
))
412 case STGM_READWRITE
|STGM_WRITE
:
414 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
417 dwAccess
= GENERIC_WRITE
;
420 dwAccess
= GENERIC_READ
;
425 switch (dwMode
& STGM_SHARE_DENY_READ
)
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
:
437 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
440 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
441 if (dwMode
== STGM_FAILIFTHERE
)
442 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
443 else if (dwMode
& STGM_CREATE
)
444 dwCreate
= CREATE_ALWAYS
;
446 dwCreate
= OPEN_ALWAYS
;
448 /* Open HANDLE to file */
449 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
452 if(hFile
== INVALID_HANDLE_VALUE
)
454 HRESULT hRet
= (HRESULT
)GetLastError();
456 hRet
= HRESULT_FROM_WIN32(hRet
);
460 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
465 return E_OUTOFMEMORY
;
470 /*************************************************************************
471 * SHCreateStreamOnFileW [SHLWAPI.@]
473 * See SHCreateStreamOnFileA.
475 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
480 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
482 if (!lpszPath
|| !lppStream
)
485 dwAttr
= GetFileAttributesW(lpszPath
);
486 if (dwAttr
== INVALID_FILE_ATTRIBUTES
)
489 return SHCreateStreamOnFileEx(lpszPath
, dwMode
|STGM_WRITE
, dwAttr
,
490 TRUE
, NULL
, lppStream
);
493 /*************************************************************************
494 * SHCreateStreamOnFileA [SHLWAPI.@]
496 * Create a stream on a file.
499 * lpszPath [I] Path of file to create stream on
500 * dwMode [I] Mode to create stream in
501 * lppStream [O] Destination for created IStream object
504 * Success: S_OK. lppStream contains the new IStream object
505 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
507 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
510 WCHAR szPath
[MAX_PATH
];
512 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
516 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
517 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
520 /*************************************************************************
523 * Call IStream_Read() on a stream.
526 * lpStream [I] IStream object
527 * lpvDest [O] Destination for data read
528 * ulSize [I] Size of data to read
531 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
532 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
533 * number of bytes read does not match.
535 HRESULT WINAPI
SHLWAPI_184(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
540 TRACE("(%p,%p,%ld)\n", lpStream
, lpvDest
, ulSize
);
542 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
544 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
549 /*************************************************************************
552 * Determine if a stream has 0 length.
555 * lpStream [I] IStream object
558 * TRUE: If the stream has 0 length
561 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
566 TRACE("(%p)\n", lpStream
);
568 memset(&statstg
, 0, sizeof(statstg
));
570 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
572 if(statstg
.cbSize
.QuadPart
)
573 bRet
= FALSE
; /* Non-Zero */
579 /* Try to read from the stream */
580 if(SUCCEEDED(SHLWAPI_184(lpStream
, &dwDummy
, sizeof(dwDummy
))))
585 IStream_Seek(lpStream
, zero
, 0, NULL
);
586 bRet
= FALSE
; /* Non-Zero */
592 /*************************************************************************
595 * Call IStream_Write() on a stream.
598 * lpStream [I] IStream object
599 * lpvSrc [I] Source for data to write
600 * ulSize [I] Size of data
603 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
604 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
605 * number of bytes written does not match.
607 HRESULT WINAPI
SHLWAPI_212(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
612 TRACE("(%p,%p,%ld)\n", lpStream
, lpvSrc
, ulSize
);
614 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
616 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
622 /*************************************************************************
625 * Seek to the start of a stream.
628 * lpStream [I] IStream object
631 * Success: S_OK. The current position within the stream is updated
632 * Failure: An HRESULT error code.
634 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
637 TRACE("(%p)\n", lpStream
);
639 return IStream_Seek(lpStream
, zero
, 0, NULL
);
642 /*************************************************************************
645 * Get the size of a stream.
648 * lpStream [I] IStream object
649 * lpulSize [O] Destination for size
652 * Success: S_OK. lpulSize contains the size of the stream.
653 * Failure: An HRESULT error code.
655 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
660 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
662 memset(&statstg
, 0, sizeof(statstg
));
664 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
666 if (SUCCEEDED(hRet
) && lpulSize
)
667 *lpulSize
= statstg
.cbSize
;