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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
29 #define NO_SHLWAPI_REG
30 #define NO_SHLWAPI_PATH
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
36 /* Layout of ISHFileStream object */
48 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
51 /**************************************************************************
52 * IStream_fnQueryInterface
54 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
56 ISHFileStream
*This
= (ISHFileStream
*)iface
;
58 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
62 if(IsEqualIID(riid
, &IID_IUnknown
) ||
63 IsEqualIID(riid
, &IID_IStream
))
67 IStream_AddRef((IStream
*)*ppvObj
);
73 /**************************************************************************
76 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
78 ISHFileStream
*This
= (ISHFileStream
*)iface
;
80 TRACE("(%p)\n", This
);
81 return InterlockedIncrement(&This
->ref
);
84 /**************************************************************************
87 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
89 ISHFileStream
*This
= (ISHFileStream
*)iface
;
92 TRACE("(%p)\n", This
);
94 if (!(ulRet
= InterlockedDecrement(&This
->ref
)))
96 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
97 LocalFree((HLOCAL
)This
->lpszPath
);
98 CloseHandle(This
->hFile
);
99 HeapFree(GetProcessHeap(), 0, This
);
104 /**************************************************************************
107 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
109 ISHFileStream
*This
= (ISHFileStream
*)iface
;
113 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbRead
);
116 hRet
= STG_E_INVALIDPOINTER
;
117 else if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
119 hRet
= (HRESULT
)GetLastError();
121 hRet
= HRESULT_FROM_WIN32(hRet
);
128 /**************************************************************************
131 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
133 ISHFileStream
*This
= (ISHFileStream
*)iface
;
137 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbWritten
);
140 hRet
= STG_E_INVALIDPOINTER
;
141 else if (!(This
->dwMode
& STGM_WRITE
))
143 else if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
145 hRet
= (HRESULT
)GetLastError();
147 hRet
= HRESULT_FROM_WIN32(hRet
);
150 *pcbWritten
= dwWritten
;
154 /**************************************************************************
157 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
158 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
160 ISHFileStream
*This
= (ISHFileStream
*)iface
;
163 TRACE("(%p,%ld,%ld,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
165 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
166 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
170 pNewPos
->u
.HighPart
= 0;
171 pNewPos
->u
.LowPart
= dwPos
;
176 /**************************************************************************
179 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
181 ISHFileStream
*This
= (ISHFileStream
*)iface
;
183 TRACE("(%p,%ld)\n", This
, libNewSize
.u
.LowPart
);
184 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
188 /**************************************************************************
191 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
192 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
194 ISHFileStream
*This
= (ISHFileStream
*)iface
;
199 TRACE("(%p,%p,%ld,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
202 pcbRead
->QuadPart
= 0;
204 pcbWritten
->QuadPart
= 0;
207 return STG_E_INVALIDPOINTER
;
209 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
212 ulSize
= cb
.QuadPart
;
217 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
220 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
222 pcbRead
->QuadPart
+= ulAmt
;
223 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
227 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
229 pcbWritten
->QuadPart
+= ulAmt
;
230 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
238 /**************************************************************************
241 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
243 ISHFileStream
*This
= (ISHFileStream
*)iface
;
245 TRACE("(%p,%ld)\n", This
, grfCommitFlags
);
246 /* Currently unbuffered: This function is not needed */
250 /**************************************************************************
253 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
255 ISHFileStream
*This
= (ISHFileStream
*)iface
;
257 TRACE("(%p)\n", This
);
261 /**************************************************************************
262 * IStream_fnLockUnlockRegion
264 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
265 ULARGE_INTEGER cb
, DWORD dwLockType
)
267 ISHFileStream
*This
= (ISHFileStream
*)iface
;
268 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
272 /*************************************************************************
275 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
278 ISHFileStream
*This
= (ISHFileStream
*)iface
;
279 BY_HANDLE_FILE_INFORMATION fi
;
282 TRACE("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
285 hRet
= STG_E_INVALIDPOINTER
;
288 memset(&fi
, 0, sizeof(fi
));
289 GetFileInformationByHandle(This
->hFile
, &fi
);
291 if (grfStatFlag
& STATFLAG_NONAME
)
292 lpStat
->pwcsName
= NULL
;
294 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
295 lpStat
->type
= This
->type
;
296 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
297 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
298 lpStat
->mtime
= fi
.ftLastWriteTime
;
299 lpStat
->ctime
= fi
.ftCreationTime
;
300 lpStat
->atime
= fi
.ftLastAccessTime
;
301 lpStat
->grfMode
= This
->dwMode
;
302 lpStat
->grfLocksSupported
= 0;
303 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
304 lpStat
->grfStateBits
= This
->grfStateBits
;
305 lpStat
->reserved
= 0;
310 /*************************************************************************
313 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
315 ISHFileStream
*This
= (ISHFileStream
*)iface
;
317 TRACE("(%p)\n",This
);
323 static struct IStreamVtbl SHLWAPI_fsVTable
=
325 IStream_fnQueryInterface
,
335 IStream_fnLockUnlockRegion
,
336 IStream_fnLockUnlockRegion
,
341 /**************************************************************************
344 * Internal helper: Create and initialise a new file stream object.
346 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
348 ISHFileStream
* fileStream
;
350 fileStream
= (ISHFileStream
*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
354 fileStream
->lpVtbl
= &SHLWAPI_fsVTable
;
356 fileStream
->hFile
= hFile
;
357 fileStream
->dwMode
= dwMode
;
358 fileStream
->lpszPath
= StrDupW(lpszPath
);
359 fileStream
->type
= 0; /* FIXME */
360 fileStream
->grfStateBits
= 0; /* FIXME */
362 TRACE ("Returning %p\n", fileStream
);
363 return (IStream
*)fileStream
;
366 /*************************************************************************
367 * SHCreateStreamOnFileEx [SHLWAPI.@]
369 * Create a stream on a file.
372 * lpszPath [I] Path of file to create stream on
373 * dwMode [I] Mode to create stream in
374 * dwAttributes [I] Attributes of the file
375 * bCreate [I] Whether to create the file if it doesn't exist
376 * lpTemplate [I] Reserved, must be NULL
377 * lppStream [O] Destination for created stream
380 * Success: S_OK. lppStream contains the new stream object
381 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
384 * This function is available in Unicode only.
386 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
387 DWORD dwAttributes
, BOOL bCreate
,
388 IStream
*lpTemplate
, IStream
**lppStream
)
390 DWORD dwAccess
, dwShare
, dwCreate
;
393 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
394 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
396 if (!lpszPath
|| !lppStream
|| lpTemplate
)
401 if (dwMode
& ~(STGM_WRITE
|STGM_READWRITE
|STGM_SHARE_DENY_NONE
|STGM_SHARE_DENY_READ
|STGM_CREATE
))
403 WARN("Invalid mode 0x%08lX\n", dwMode
);
408 switch (dwMode
& (STGM_WRITE
|STGM_READWRITE
))
410 case STGM_READWRITE
|STGM_WRITE
:
412 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
415 dwAccess
= GENERIC_WRITE
;
418 dwAccess
= GENERIC_READ
;
423 switch (dwMode
& STGM_SHARE_DENY_READ
)
425 case STGM_SHARE_DENY_READ
:
426 dwShare
= FILE_SHARE_WRITE
;
428 case STGM_SHARE_DENY_WRITE
:
429 dwShare
= FILE_SHARE_READ
;
431 case STGM_SHARE_EXCLUSIVE
:
435 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
438 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
439 if (dwMode
== STGM_FAILIFTHERE
)
440 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
441 else if (dwMode
& STGM_CREATE
)
442 dwCreate
= CREATE_ALWAYS
;
444 dwCreate
= OPEN_ALWAYS
;
446 /* Open HANDLE to file */
447 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
450 if(hFile
== INVALID_HANDLE_VALUE
)
452 HRESULT hRet
= (HRESULT
)GetLastError();
454 hRet
= HRESULT_FROM_WIN32(hRet
);
458 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
463 return E_OUTOFMEMORY
;
468 /*************************************************************************
469 * SHCreateStreamOnFileW [SHLWAPI.@]
471 * See SHCreateStreamOnFileA.
473 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
478 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
480 if (!lpszPath
|| !lppStream
)
483 dwAttr
= GetFileAttributesW(lpszPath
);
484 if (dwAttr
== INVALID_FILE_ATTRIBUTES
)
487 return SHCreateStreamOnFileEx(lpszPath
, dwMode
|STGM_WRITE
, dwAttr
,
488 TRUE
, 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,%ld,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
514 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
515 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
518 /*************************************************************************
521 * Call IStream_Read() on a stream.
524 * lpStream [I] IStream object
525 * lpvDest [O] Destination for data read
526 * ulSize [I] Size of data to read
529 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
530 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
531 * number of bytes read does not match.
533 HRESULT WINAPI
SHLWAPI_184(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
538 TRACE("(%p,%p,%ld)\n", lpStream
, lpvDest
, ulSize
);
540 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
542 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
547 /*************************************************************************
550 * Determine if a stream has 0 length.
553 * lpStream [I] IStream object
556 * TRUE: If the stream has 0 length
559 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
564 TRACE("(%p)\n", lpStream
);
566 memset(&statstg
, 0, sizeof(statstg
));
568 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
570 if(statstg
.cbSize
.QuadPart
)
571 bRet
= FALSE
; /* Non-Zero */
577 /* Try to read from the stream */
578 if(SUCCEEDED(SHLWAPI_184(lpStream
, &dwDummy
, sizeof(dwDummy
))))
583 IStream_Seek(lpStream
, zero
, 0, NULL
);
584 bRet
= FALSE
; /* Non-Zero */
590 /*************************************************************************
593 * Call IStream_Write() on a stream.
596 * lpStream [I] IStream object
597 * lpvSrc [I] Source for data to write
598 * ulSize [I] Size of data
601 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
602 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
603 * number of bytes written does not match.
605 HRESULT WINAPI
SHLWAPI_212(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
610 TRACE("(%p,%p,%ld)\n", lpStream
, lpvSrc
, ulSize
);
612 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
614 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
620 /*************************************************************************
623 * Seek to the start of a stream.
626 * lpStream [I] IStream object
629 * Success: S_OK. The current position within the stream is updated
630 * Failure: An HRESULT error code.
632 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
635 TRACE("(%p)\n", lpStream
);
637 return IStream_Seek(lpStream
, zero
, 0, NULL
);
640 /*************************************************************************
643 * Get the size of a stream.
646 * lpStream [I] IStream object
647 * lpulSize [O] Destination for size
650 * Success: S_OK. lpulSize contains the size of the stream.
651 * Failure: An HRESULT error code.
653 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
658 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
660 memset(&statstg
, 0, sizeof(statstg
));
662 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
664 if (SUCCEEDED(hRet
) && lpulSize
)
665 *lpulSize
= statstg
.cbSize
;