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
;
81 ULONG refCount
= InterlockedIncrement(&This
->ref
);
83 TRACE("(%p)->(ref before=%lu)\n",This
, refCount
- 1);
88 /**************************************************************************
91 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
93 ISHFileStream
*This
= (ISHFileStream
*)iface
;
94 ULONG refCount
= InterlockedDecrement(&This
->ref
);
96 TRACE("(%p)->(ref before=%lu)\n",This
, refCount
+ 1);
100 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
101 LocalFree((HLOCAL
)This
->lpszPath
);
102 CloseHandle(This
->hFile
);
103 HeapFree(GetProcessHeap(), 0, This
);
109 /**************************************************************************
112 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
114 ISHFileStream
*This
= (ISHFileStream
*)iface
;
118 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbRead
);
121 hRet
= STG_E_INVALIDPOINTER
;
122 else if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
124 hRet
= (HRESULT
)GetLastError();
126 hRet
= HRESULT_FROM_WIN32(hRet
);
133 /**************************************************************************
136 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
138 ISHFileStream
*This
= (ISHFileStream
*)iface
;
142 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbWritten
);
145 hRet
= STG_E_INVALIDPOINTER
;
146 else if (!(This
->dwMode
& STGM_WRITE
))
148 else if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
150 hRet
= (HRESULT
)GetLastError();
152 hRet
= HRESULT_FROM_WIN32(hRet
);
155 *pcbWritten
= dwWritten
;
159 /**************************************************************************
162 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
163 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
165 ISHFileStream
*This
= (ISHFileStream
*)iface
;
168 TRACE("(%p,%ld,%ld,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
170 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
171 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
175 pNewPos
->u
.HighPart
= 0;
176 pNewPos
->u
.LowPart
= dwPos
;
181 /**************************************************************************
184 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
186 ISHFileStream
*This
= (ISHFileStream
*)iface
;
188 TRACE("(%p,%ld)\n", This
, libNewSize
.u
.LowPart
);
189 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
193 /**************************************************************************
196 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
197 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
199 ISHFileStream
*This
= (ISHFileStream
*)iface
;
204 TRACE("(%p,%p,%ld,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
207 pcbRead
->QuadPart
= 0;
209 pcbWritten
->QuadPart
= 0;
212 return STG_E_INVALIDPOINTER
;
214 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
217 ulSize
= cb
.QuadPart
;
222 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
225 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
227 pcbRead
->QuadPart
+= ulAmt
;
228 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
232 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
234 pcbWritten
->QuadPart
+= ulAmt
;
235 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
243 /**************************************************************************
246 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
248 ISHFileStream
*This
= (ISHFileStream
*)iface
;
250 TRACE("(%p,%ld)\n", This
, grfCommitFlags
);
251 /* Currently unbuffered: This function is not needed */
255 /**************************************************************************
258 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
260 ISHFileStream
*This
= (ISHFileStream
*)iface
;
262 TRACE("(%p)\n", This
);
266 /**************************************************************************
267 * IStream_fnLockUnlockRegion
269 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
270 ULARGE_INTEGER cb
, DWORD dwLockType
)
272 ISHFileStream
*This
= (ISHFileStream
*)iface
;
273 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
277 /*************************************************************************
280 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
283 ISHFileStream
*This
= (ISHFileStream
*)iface
;
284 BY_HANDLE_FILE_INFORMATION fi
;
287 TRACE("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
290 hRet
= STG_E_INVALIDPOINTER
;
293 memset(&fi
, 0, sizeof(fi
));
294 GetFileInformationByHandle(This
->hFile
, &fi
);
296 if (grfStatFlag
& STATFLAG_NONAME
)
297 lpStat
->pwcsName
= NULL
;
299 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
300 lpStat
->type
= This
->type
;
301 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
302 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
303 lpStat
->mtime
= fi
.ftLastWriteTime
;
304 lpStat
->ctime
= fi
.ftCreationTime
;
305 lpStat
->atime
= fi
.ftLastAccessTime
;
306 lpStat
->grfMode
= This
->dwMode
;
307 lpStat
->grfLocksSupported
= 0;
308 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
309 lpStat
->grfStateBits
= This
->grfStateBits
;
310 lpStat
->reserved
= 0;
315 /*************************************************************************
318 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
320 ISHFileStream
*This
= (ISHFileStream
*)iface
;
322 TRACE("(%p)\n",This
);
328 static struct IStreamVtbl SHLWAPI_fsVTable
=
330 IStream_fnQueryInterface
,
340 IStream_fnLockUnlockRegion
,
341 IStream_fnLockUnlockRegion
,
346 /**************************************************************************
349 * Internal helper: Create and initialise a new file stream object.
351 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
353 ISHFileStream
* fileStream
;
355 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
359 fileStream
->lpVtbl
= &SHLWAPI_fsVTable
;
361 fileStream
->hFile
= hFile
;
362 fileStream
->dwMode
= dwMode
;
363 fileStream
->lpszPath
= StrDupW(lpszPath
);
364 fileStream
->type
= 0; /* FIXME */
365 fileStream
->grfStateBits
= 0; /* FIXME */
367 TRACE ("Returning %p\n", fileStream
);
368 return (IStream
*)fileStream
;
371 /*************************************************************************
372 * SHCreateStreamOnFileEx [SHLWAPI.@]
374 * Create a stream on a file.
377 * lpszPath [I] Path of file to create stream on
378 * dwMode [I] Mode to create stream in
379 * dwAttributes [I] Attributes of the file
380 * bCreate [I] Whether to create the file if it doesn't exist
381 * lpTemplate [I] Reserved, must be NULL
382 * lppStream [O] Destination for created stream
385 * Success: S_OK. lppStream contains the new stream object
386 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
389 * This function is available in Unicode only.
391 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
392 DWORD dwAttributes
, BOOL bCreate
,
393 IStream
*lpTemplate
, IStream
**lppStream
)
395 DWORD dwAccess
, dwShare
, dwCreate
;
398 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
399 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
401 if (!lpszPath
|| !lppStream
|| lpTemplate
)
406 if (dwMode
& ~(STGM_WRITE
|STGM_READWRITE
|STGM_SHARE_DENY_NONE
|STGM_SHARE_DENY_READ
|STGM_CREATE
))
408 WARN("Invalid mode 0x%08lX\n", dwMode
);
413 switch (dwMode
& (STGM_WRITE
|STGM_READWRITE
))
415 case STGM_READWRITE
|STGM_WRITE
:
417 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
420 dwAccess
= GENERIC_WRITE
;
423 dwAccess
= GENERIC_READ
;
428 switch (dwMode
& STGM_SHARE_DENY_READ
)
430 case STGM_SHARE_DENY_READ
:
431 dwShare
= FILE_SHARE_WRITE
;
433 case STGM_SHARE_DENY_WRITE
:
434 dwShare
= FILE_SHARE_READ
;
436 case STGM_SHARE_EXCLUSIVE
:
440 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
443 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
444 if (dwMode
== STGM_FAILIFTHERE
)
445 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
446 else if (dwMode
& STGM_CREATE
)
447 dwCreate
= CREATE_ALWAYS
;
449 dwCreate
= OPEN_ALWAYS
;
451 /* Open HANDLE to file */
452 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
455 if(hFile
== INVALID_HANDLE_VALUE
)
457 HRESULT hRet
= (HRESULT
)GetLastError();
459 hRet
= HRESULT_FROM_WIN32(hRet
);
463 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
468 return E_OUTOFMEMORY
;
473 /*************************************************************************
474 * SHCreateStreamOnFileW [SHLWAPI.@]
476 * See SHCreateStreamOnFileA.
478 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
483 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
485 if (!lpszPath
|| !lppStream
)
488 dwAttr
= GetFileAttributesW(lpszPath
);
489 if (dwAttr
== INVALID_FILE_ATTRIBUTES
)
492 return SHCreateStreamOnFileEx(lpszPath
, dwMode
|STGM_WRITE
, dwAttr
,
493 TRUE
, NULL
, lppStream
);
496 /*************************************************************************
497 * SHCreateStreamOnFileA [SHLWAPI.@]
499 * Create a stream on a file.
502 * lpszPath [I] Path of file to create stream on
503 * dwMode [I] Mode to create stream in
504 * lppStream [O] Destination for created IStream object
507 * Success: S_OK. lppStream contains the new IStream object
508 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
510 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
513 WCHAR szPath
[MAX_PATH
];
515 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
519 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
520 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
523 /*************************************************************************
526 * Call IStream_Read() on a stream.
529 * lpStream [I] IStream object
530 * lpvDest [O] Destination for data read
531 * ulSize [I] Size of data to read
534 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
535 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
536 * number of bytes read does not match.
538 HRESULT WINAPI
SHLWAPI_184(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
543 TRACE("(%p,%p,%ld)\n", lpStream
, lpvDest
, ulSize
);
545 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
547 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
552 /*************************************************************************
555 * Determine if a stream has 0 length.
558 * lpStream [I] IStream object
561 * TRUE: If the stream has 0 length
564 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
569 TRACE("(%p)\n", lpStream
);
571 memset(&statstg
, 0, sizeof(statstg
));
573 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
575 if(statstg
.cbSize
.QuadPart
)
576 bRet
= FALSE
; /* Non-Zero */
582 /* Try to read from the stream */
583 if(SUCCEEDED(SHLWAPI_184(lpStream
, &dwDummy
, sizeof(dwDummy
))))
588 IStream_Seek(lpStream
, zero
, 0, NULL
);
589 bRet
= FALSE
; /* Non-Zero */
595 /*************************************************************************
598 * Call IStream_Write() on a stream.
601 * lpStream [I] IStream object
602 * lpvSrc [I] Source for data to write
603 * ulSize [I] Size of data
606 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
607 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
608 * number of bytes written does not match.
610 HRESULT WINAPI
SHLWAPI_212(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
615 TRACE("(%p,%p,%ld)\n", lpStream
, lpvSrc
, ulSize
);
617 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
619 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
625 /*************************************************************************
628 * Seek to the start of a stream.
631 * lpStream [I] IStream object
634 * Success: S_OK. The current position within the stream is updated
635 * Failure: An HRESULT error code.
637 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
640 TRACE("(%p)\n", lpStream
);
642 return IStream_Seek(lpStream
, zero
, 0, NULL
);
645 /*************************************************************************
648 * Get the size of a stream.
651 * lpStream [I] IStream object
652 * lpulSize [O] Destination for size
655 * Success: S_OK. lpulSize contains the size of the stream.
656 * Failure: An HRESULT error code.
658 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
663 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
665 memset(&statstg
, 0, sizeof(statstg
));
667 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
669 if (SUCCEEDED(hRet
) && lpulSize
)
670 *lpulSize
= statstg
.cbSize
;