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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
27 #define NO_SHLWAPI_REG
28 #define NO_SHLWAPI_PATH
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
34 /* Layout of ISHFileStream object */
46 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
49 /**************************************************************************
50 * IStream_fnQueryInterface
52 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
54 ICOM_THIS(ISHFileStream
, iface
);
56 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
60 if(IsEqualIID(riid
, &IID_IUnknown
) ||
61 IsEqualIID(riid
, &IID_IStream
))
65 IStream_AddRef((IStream
*)*ppvObj
);
71 /**************************************************************************
74 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
76 ICOM_THIS(ISHFileStream
, iface
);
78 TRACE("(%p)\n", This
);
79 return InterlockedIncrement(&This
->ref
);
82 /**************************************************************************
85 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
87 ICOM_THIS(ISHFileStream
, iface
);
90 TRACE("(%p)\n", This
);
92 if (!(ulRet
= InterlockedDecrement(&This
->ref
)))
94 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
95 LocalFree((HLOCAL
)This
->lpszPath
);
96 CloseHandle(This
->hFile
);
97 HeapFree(GetProcessHeap(), 0, This
);
102 /**************************************************************************
105 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
107 ICOM_THIS(ISHFileStream
, iface
);
111 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbRead
);
114 hRet
= STG_E_INVALIDPOINTER
;
115 else if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
117 hRet
= (HRESULT
)GetLastError();
119 hRet
= HRESULT_FROM_WIN32(hRet
);
126 /**************************************************************************
129 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
131 ICOM_THIS(ISHFileStream
, iface
);
135 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbWritten
);
138 hRet
= STG_E_INVALIDPOINTER
;
139 else if (!(This
->dwMode
& STGM_WRITE
))
141 else if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
143 hRet
= (HRESULT
)GetLastError();
145 hRet
= HRESULT_FROM_WIN32(hRet
);
148 *pcbWritten
= dwWritten
;
152 /**************************************************************************
155 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
156 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
158 ICOM_THIS(ISHFileStream
, iface
);
161 TRACE("(%p,%ld,%ld,%p)\n", This
, dlibMove
.s
.LowPart
, dwOrigin
, pNewPos
);
163 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
164 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.s
.LowPart
, NULL
, dwOrigin
);
168 pNewPos
->s
.HighPart
= 0;
169 pNewPos
->s
.LowPart
= dwPos
;
174 /**************************************************************************
177 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
179 ICOM_THIS(ISHFileStream
, iface
);
181 TRACE("(%p,%ld)\n", This
, libNewSize
.s
.LowPart
);
182 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
186 /**************************************************************************
189 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
190 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
192 ICOM_THIS(ISHFileStream
, iface
);
197 TRACE("(%p,%p,%ld,%p,%p)\n", This
, pstm
, cb
.s
.LowPart
, pcbRead
, pcbWritten
);
200 pcbRead
->QuadPart
= 0;
202 pcbWritten
->QuadPart
= 0;
205 return STG_E_INVALIDPOINTER
;
207 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
210 ulSize
= cb
.QuadPart
;
215 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
218 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
220 pcbRead
->QuadPart
+= ulAmt
;
221 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
225 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
227 pcbWritten
->QuadPart
+= ulAmt
;
228 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
236 /**************************************************************************
239 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
241 ICOM_THIS(ISHFileStream
, iface
);
243 TRACE("(%p,%ld)\n", This
, grfCommitFlags
);
244 /* Currently unbuffered: This function is not needed */
248 /**************************************************************************
251 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
253 ICOM_THIS(ISHFileStream
, iface
);
255 TRACE("(%p)\n", This
);
259 /**************************************************************************
260 * IStream_fnLockUnlockRegion
262 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
263 ULARGE_INTEGER cb
, DWORD dwLockType
)
265 ICOM_THIS(ISHFileStream
, iface
);
266 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.s
.LowPart
, cb
.s
.LowPart
, dwLockType
);
270 /*************************************************************************
273 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
276 ICOM_THIS(ISHFileStream
, iface
);
277 BY_HANDLE_FILE_INFORMATION fi
;
280 TRACE("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
283 hRet
= STG_E_INVALIDPOINTER
;
286 memset(&fi
, 0, sizeof(fi
));
287 GetFileInformationByHandle(This
->hFile
, &fi
);
289 if (grfStatFlag
& STATFLAG_NONAME
)
290 lpStat
->pwcsName
= NULL
;
292 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
293 lpStat
->type
= This
->type
;
294 lpStat
->cbSize
.s
.LowPart
= fi
.nFileSizeLow
;
295 lpStat
->cbSize
.s
.HighPart
= fi
.nFileSizeHigh
;
296 lpStat
->mtime
= fi
.ftLastWriteTime
;
297 lpStat
->ctime
= fi
.ftCreationTime
;
298 lpStat
->atime
= fi
.ftLastAccessTime
;
299 lpStat
->grfMode
= This
->dwMode
;
300 lpStat
->grfLocksSupported
= 0;
301 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
302 lpStat
->grfStateBits
= This
->grfStateBits
;
303 lpStat
->reserved
= 0;
308 /*************************************************************************
311 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
313 ICOM_THIS(ISHFileStream
, iface
);
315 TRACE("(%p)\n",This
);
321 static struct ICOM_VTABLE(IStream
) SHLWAPI_fsVTable
=
323 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
324 IStream_fnQueryInterface
,
334 IStream_fnLockUnlockRegion
,
335 IStream_fnLockUnlockRegion
,
340 /**************************************************************************
343 * Internal helper: Create and initialise a new file stream object.
345 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
347 ISHFileStream
* fileStream
;
349 fileStream
= (ISHFileStream
*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
353 ICOM_VTBL(fileStream
) = &SHLWAPI_fsVTable
;
355 fileStream
->hFile
= hFile
;
356 fileStream
->dwMode
= dwMode
;
357 fileStream
->lpszPath
= StrDupW(lpszPath
);
358 fileStream
->type
= 0; /* FIXME */
359 fileStream
->grfStateBits
= 0; /* FIXME */
361 TRACE ("Returning %p\n", fileStream
);
362 return (IStream
*)fileStream
;
365 /*************************************************************************
366 * SHCreateStreamOnFileEx [SHLWAPI.@]
368 * Create a stream on a file.
371 * lpszPath [I] Path of file to create stream on
372 * dwMode [I] Mode to create stream in
373 * dwAttributes [I] Attributes of the file
374 * bCreate [I] Whether to create the file if it doesn't exist
375 * lpTemplate [I] Reserved, must be NULL
376 * lppStream [O] Destination for created stream
379 * Success: S_OK. lppStream contains the new stream object
380 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
383 * This function is available in Unicode only.
385 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
386 DWORD dwAttributes
, BOOL bCreate
,
387 IStream
*lpTemplate
, IStream
**lppStream
)
389 DWORD dwAccess
, dwShare
, dwCreate
;
392 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
393 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
395 if (!lpszPath
|| !lppStream
|| lpTemplate
)
400 if (dwMode
& ~(STGM_WRITE
|STGM_READWRITE
|STGM_SHARE_DENY_NONE
|STGM_SHARE_DENY_READ
|STGM_CREATE
))
402 WARN("Invalid mode 0x%08lX\n", dwMode
);
407 switch (dwMode
& (STGM_WRITE
|STGM_READWRITE
))
409 case STGM_READWRITE
|STGM_WRITE
:
411 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
414 dwAccess
= GENERIC_WRITE
;
417 dwAccess
= GENERIC_READ
;
422 switch (dwMode
& STGM_SHARE_DENY_READ
)
424 case STGM_SHARE_DENY_READ
:
425 dwShare
= FILE_SHARE_WRITE
;
427 case STGM_SHARE_DENY_WRITE
:
428 dwShare
= FILE_SHARE_READ
;
430 case STGM_SHARE_EXCLUSIVE
:
434 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
437 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
438 if (dwMode
== STGM_FAILIFTHERE
)
439 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
440 else if (dwMode
& STGM_CREATE
)
441 dwCreate
= CREATE_ALWAYS
;
443 dwCreate
= OPEN_ALWAYS
;
445 /* Open HANDLE to file */
446 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
449 if(hFile
== INVALID_HANDLE_VALUE
)
451 HRESULT hRet
= (HRESULT
)GetLastError();
453 hRet
= HRESULT_FROM_WIN32(hRet
);
457 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
462 return E_OUTOFMEMORY
;
467 /*************************************************************************
468 * SHCreateStreamOnFileW [SHLWAPI.@]
470 * See SHCreateStreamOnFileA.
472 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
477 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
479 if (!lpszPath
|| !lppStream
)
482 dwAttr
= GetFileAttributesW(lpszPath
);
486 return SHCreateStreamOnFileEx(lpszPath
, dwMode
|STGM_WRITE
, dwAttr
,
487 TRUE
, NULL
, lppStream
);
490 /*************************************************************************
491 * SHCreateStreamOnFileA [SHLWAPI.@]
493 * Create a stream on a file.
496 * lpszPath [I] Path of file to create stream on
497 * dwMode [I] Mode to create stream in
498 * lppStream [O] Destination for created IStream object
501 * Success: S_OK. lppStream contains the new IStream object
502 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
504 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
507 WCHAR szPath
[MAX_PATH
];
509 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
513 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
514 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
517 /*************************************************************************
520 * Call IStream_Read() on a stream.
523 * lpStream [I] IStream object
524 * lpvDest [O] Destination for data read
525 * ulSize [I] Size of data to read
528 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
529 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
530 * number of bytes read does not match.
532 HRESULT WINAPI
SHLWAPI_184(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
537 TRACE("(%p,%p,%ld)\n", lpStream
, lpvDest
, ulSize
);
539 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
541 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
546 /*************************************************************************
549 * Determine if a stream has 0 length.
552 * lpStream [I] IStream object
555 * TRUE: If the stream has 0 length
558 BOOL WINAPI
SHLWAPI_166(IStream
*lpStream
)
563 TRACE("(%p)\n", lpStream
);
565 memset(&statstg
, 0, sizeof(statstg
));
567 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
569 if(statstg
.cbSize
.QuadPart
)
570 bRet
= FALSE
; /* Non-Zero */
576 /* Try to read from the stream */
577 if(SUCCEEDED(SHLWAPI_184(lpStream
, &dwDummy
, sizeof(dwDummy
))))
582 IStream_Seek(lpStream
, zero
, 0, NULL
);
583 bRet
= FALSE
; /* Non-Zero */
589 /*************************************************************************
592 * Call IStream_Write() on a stream.
595 * lpStream [I] IStream object
596 * lpvSrc [I] Source for data to write
597 * ulSize [I] Size of data
600 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
601 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
602 * number of bytes written does not match.
604 HRESULT WINAPI
SHLWAPI_212(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
609 TRACE("(%p,%p,%ld)\n", lpStream
, lpvSrc
, ulSize
);
611 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
613 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
619 /*************************************************************************
622 * Seek to the start of a stream.
625 * lpStream [I] IStream object
628 * Success: S_OK. The current position within the stream is updated
629 * Failure: An HRESULT error code.
631 HRESULT WINAPI
SHLWAPI_213(IStream
*lpStream
)
634 TRACE("(%p)\n", lpStream
);
636 return IStream_Seek(lpStream
, zero
, 0, NULL
);
639 /*************************************************************************
642 * Get the size of a stream.
645 * lpStream [I] IStream object
646 * lpulSize [O] Destination for size
649 * Success: S_OK. lpulSize contains the size of the stream.
650 * Failure: An HRESULT error code.
652 HRESULT WINAPI
SHLWAPI_214(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
657 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
659 memset(&statstg
, 0, sizeof(statstg
));
661 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
663 if (SUCCEEDED(hRet
) && lpulSize
)
664 *lpulSize
= statstg
.cbSize
;